- Home /
[PUN] Get variables from server via RPC OnJoinedRoom()
Hello. I write a prophunt game and i want to spawn as a hunter when there are props in the game. I make a room with a build and try to join to it after. When i join, the master server sends an RPC to all to Sync the Counters (propCount, hunterCount). The problem is that i get the variables from the server after i spawned, so i spawn as a prop. How to wait for RPC to finish before i spawn?
void OnPhotonPlayerConnected(PhotonPlayer player)
{
Debug.Log("OnPhotonPlayerConnected() " + player.name);
if (PhotonNetwork.isMasterClient)
{
photonView.RPC("UpdateCounts", PhotonTargets.AllBuffered, playerCount, propCount, hunterCount);
}
}
[RPC]
void UpdateCounts(int pc, int prc, int hc)
{
playerCount = pc;
propCount = prc;
hunterCount = hc;
print("i got the info from server: " + propCount);
}
void OnJoinedRoom()
{
photonView.RPC("UpdatePlayerCount", PhotonTargets.AllBuffered, true);
print("what i see: " + propCount);
if (propCount == 0)
{
photonView.RPC("UpdateTeamCount", PhotonTargets.AllBuffered, true, true);
PhotonNetwork.Instantiate(propPrefab.name, propSpawnPoints[0].position, Quaternion.identity, 0);
}
else
{
photonView.RPC("UpdateTeamCount", PhotonTargets.AllBuffered, true, false);
PhotonNetwork.Instantiate(hunterPrefab.name, hunterSpawnPoints[0].position, Quaternion.identity, 0);
}
}
Answer by 334499p · Apr 02, 2015 at 06:32 PM
This is a really simple fix. Just create 2 booleans, one to check if the rpc has been received and one to check if the player has joined the room. Create an Update() function that checks for when the rpc check and player join booleans are true. When both are, just execute the code that is in your OnJoinedRoom() function.