So, in my game you have two sides. One side only has one player (Hide and seek type thing).
I’m trying to make a system where the server generates a random number between 0 and the amount of players that there are. Unfortunately I can’t get the result to synchronize. Here is my code…
public void StartServer()
{
// Generates random number by taking the amount of players currently connected...
ServerPN = Random.Range(0, m_numberofplayers);
// Calls the map change (Start of the round)
networkView.RPC("ChangeMap",RPCMode.Others, m_selectedMapName);
ChangeMap(m_selectedMapName);
}
[RPC]
void ChangeMap(string mapName)
{
Network.isMessageQueueRunning = false; //Turn on all rpcs until loaded the map
Application.LoadLevel(mapName);
// Sets the clientside value (Used in the GUI debug) to the random number...
ClientPN = ServerPN;
}
Unfortunately I’ve gone about loads of crazy solutions to the point where I’m butchering my netcode out of desperation. If somebody could help me I would be eternally grateful, if I have made a stupid mistake please point me in the right direction with pseudo code of some kind. Thank you everybody!
The m_mapselectedMapName has nothing really to do with the random number. I’m just nestling the ‘ClientPN = ServerPN’ inside the changemap value. How would I go about sending the random number to the client? Making an RPC and passing it as a parameter?
EDIT:
public void StartServer()
{
// Generates random number by taking the amount of players currently connected...
ServerPN = Random.Range(0, 10);
networkView.RPC("ChangeMap",RPCMode.Others, m_selectedMapName);
networkView.RPC("SetPN", RPCMode.AllBuffered, ServerPN);
// Calls the map change (Start of the round)
ChangeMap(m_selectedMapName);
}
[RPC]
void ChangeMap(string mapName)
{
Network.isMessageQueueRunning = false; //Turn on all rpcs until loaded the map
Application.LoadLevel(mapName);
// Sets the clientside value (Used in the GUI debug) to the random number...
}
[RPC]
void SetPN(int ServerPN)
{
ClientPN = ServerPN;
}
Still gives nothing. I’m probably making a really stupid mistake, network code isn’t my strong suit. I also get this error now:
RPC call failed because the function ‘SetPN’ does not exist in any script attached to GameSetup
If you want to use a separate RPC then that’s fine too but you probably want to send the PN RPC before the ChangeMap RPC, as it looks like your ChangeMap RPC requires ServerPN to be correctly set already.
The error you received implies that you don’t have the right components attached on one end of the connection. To send an RPC, both ends of the connection need to have a component attached with the right RPC method defined. It doesn’t have to be exactly the same method (e.g. you might have a separate component on the server) but the signatures need to match.
Then whatever receives this RPC will receive the ServerPN value along with the other data, and you can do what you like with it.
If you have any doubt over whether this RPC is getting received then put some Debug.Log calls in, and enable network debugging too which will print a lot of diagnostic information which can help track down why an RPC is not being received.
Networking is hard to get right, and Unity’s API doesn’t help much. The best thing you can do is try to keep the stuff you’re networking as simple as you can.
I shall try this when I get home. If I’m generating the random number using if (Network.isServer) then it should still pass through fine shouldn’t it?
EDIT: Applied a solution that gave me both the same random number on both machines. Could have been chance so went to test it to find that I had overloaded the masterserver. I shall report back later