- Home /
Client's RPC call doesn't excute on server without any error log.
Hello,
I'm trying to implement an authoritative server with my game.
When my client finished loading a level, i want it to call a function in server to notify the server that it has finished loading a level. After receiving RPC call from client, server would do a RPC call on client to spawn a new hero on client. i wrote my code like this: (those 2 functions is on the same script file).
void OnLevelWasLoaded(int level)
{
Debug.Log ("OnLevelWasLoaded. level = " + level);
if (Network.isClient)
{
Debug.Log ("Call OnLevelLoaded on Server");
networkView.RPC ("OnLevelLoaded", RPCMode.Server, new object[] {networkView.owner});
}
else
{
int i = Random.Range(0, spawnSetups.Count - 1);
spawnInfo spawnSetup = spawnSetups[i];
SpawnPlayer(spawnSetup.position, spawnSetup.rotation);
playerCount++;
playerIPs.Add(networkView.owner.ipAddress);
}
}
[RPC]
void OnLevelLoaded(NetworkPlayer player)
{
Debug.Log ("OnLevelLoaded");
//Call the next step, in this case, spawn new instance of player
int i = Random.Range (0, spawnSetups.Count - 1);
spawnInfo spawnSetup = spawnSetups [i];
networkView.RPC ("SpawnPlayer", player, spawnSetup.position, spawnSetup.rotation);
}
I don't have 2 computer so i run 2 unity instances on a same computer, one is the server and other is the client who connects to server with:
Network.Connect("127.0.0.1", 25000);
The bug is onLevelLoaded isn't excuted on server. When testing, i see client has the log:
Call OnLevelLoaded on Server
But on the server, i don't see the log:
OnLevelLoaded
Server don't have any error log when testing.
Please help me with this bug. Please excuse my English too :).
Answer by Bunny83 · Nov 05, 2014 at 03:28 AM
Are you sure that you have RunInBackground set to true? Either in the player settings or by setting Application.runInBackground to true in a script. Otherwise the other instance can't react to any kind of network communication until it got refocused again.
Next thing is you send "networkView.owner" to the server. Are you sure you (the client) actually owns that networkview? NetworkViews that are stored in scenes belong to the server. If you want to send your own NetworkPlayer to the server, use Network.player. However it's actually not necessary to send the NetworkPlayer manually since that information is available for every RPC. Just add the implicit NetworkMessageInfo parameter as last parameter. It will be "filled" by the system automatically. It also doesn't increase the bandwidth. See the examples in the manual
I have already added Application.runInBackground = true in my script, and my hero which belongs to server is updated to client perfectly.
I make a prefab which contains NetworkView component, then i use Network.instantiate like this:
void OnServerInitialized()
{
Debug.Log ("Server Initialized.");
Debug.Log ("IP address:" + Network.player.ipAddress + ". Port: " + Network.player.port);
Debug.Log ("External IP address:" + Network.player.externalIP + ". External port: " + Network.player.externalPort);
//Create a networked network manager.
Game$$anonymous$$anager.network$$anonymous$$anager = new GameObject("Network$$anonymous$$anager");
Game$$anonymous$$anager.network$$anonymous$$anager.hideFlags = HideFlags.HideInHierarchy;
Game$$anonymous$$anager.network$$anonymous$$anager = Network.Instantiate (network$$anonymous$$anagerPrefab, Vector3.zero, Quaternion.identity, 0) as GameObject;
Debug.Log("network$$anonymous$$anager = " + Game$$anonymous$$anager.network$$anonymous$$anager == null);
//load multiplayer level for server.
NetworkLevelLoader.Instance.LoadLevel ("$$anonymous$$ultiPlayer", levelPrefix);
}
I tried to add network$$anonymous$$anager object via hierarchy but this bug happens in that case so i tried to do it like this.
All RPC from server to client work normally. For example:
void OnPlayerConnected ( NetworkPlayer player ) {
Debug.Log ("OnPlayerConnected. playerCount= " + playerCount);
if (playerCount < $$anonymous$$axNumberOfPlayer)
{
networkView.RPC ("LoadLevel", player, new object[] {"$$anonymous$$ultiPlayer", Game$$anonymous$$anager.levelPrefix});
playerCount++;
playerIPs.Add(player.ipAddress);
}
else
{
Network.CloseConnection(player, true);
}
}
[RPC]
void LoadLevel(string levelname, int levelPrefix)
{
Debug.Log ("LoadLevel");
NetworkLevelLoader.Instance.LoadLevel (levelname, levelPrefix);
}
These functions are called just before OnLevelWasLoaded.
Your answer
Follow this Question
Related Questions
2 Servers in one build 2 Answers
Should I worry about making an authorative server for an online multiplayer WebGL game? 0 Answers
Multiplayer jitter on client 0 Answers
Create single server for multi-platform game (pc, mobile, etc) 2 Answers
Is there anyway to disable clients GameObject instance from Server or Vice-versa? 1 Answer