- Home /
Server RPC
Why doesn't this run on the server?
function OnDisconnectedFromServer() {
var pos = player.transform.position;
networkView.RPC("Save", RPCMode.Server, PlayerPrefs.GetString("UserID"), pos);
}
@RPC
function Save(UID : String, pos : Vector3) {
Debug.Log("Why doesn't this get executed on the player that runs the server?");
Debug.Log(UID + "'s location is: " + pos);
}
I read a couple of times that the server can't send RPC calls to itself, does this include sending an RPC call from one pc to another on the same network?
Answer by AyAMrau · Aug 27, 2014 at 12:56 PM
OnDisconnectedFromServer() is called when the connection to server was somehow terminated, therefore you can't send it any messages.
Instead you can detect on server side that someone disconnected by using OnPlayerDisconnected()
Yes, but the client needs to send their data to the server so it can be saved. Any suggestions on how I can do this?
Well, since you are not expecting to be disconnected, the only way is to give the server regular updates and make it assume that the most recent data is to be used in case of disconnect.
Then if a player reconnects you may need to do adjustments so the data on both sides matches.
Do you have any suggestions on how to get the players username when he disconnects?
You can write OnPlayerDisconnected() method as:
function OnPlayerDisconnected(player : NetworkPlayer)
and unity will automatically pass the argument when the function is called. The NetworkPlayer struct contains some data about the player and is also passed to other network messages such as OnPlayerConnected(), which means the server can store which player is associated with a specific NetworkPlayer. From there you can work out which player was disconnected.
Your answer
Follow this Question
Related Questions
Networking between projects: how to send ViewIDs? 1 Answer
accessing to one array from another script 2 Answers
Separating Server and Client Code 0 Answers
RPC not being called 1 Answer