Variable doesn't sync using RPC

Options
I'm building a multiplayer game that needs to know how many players are in the game to assign the player number when someone new joins, spawn him in a certain place, etc.

This is how the Inspector of the gameManager looks like:



Those are the PunRPC methods that I call to get and set the variable of players on the NetworkPlayerManager class:

public int numPlayer;

[PunRPC]
public void GetPlayers()
{
GetComponent().setNumOfPlayers(numPlayer);
}

[PunRPC]
public void SetPlayers(int num)
{
numPlayer = num;
}



And her's how I call them when a new player is Instantiated on the GameManager class:

GetComponent().RPC("GetPlayers", PhotonTargets.AllViaServer);

m_Tanks.m_Instance =
PhotonNetwork.Instantiate("Tank", m_Spawns[numOfPlayers].position, m_Spawns[numOfPlayers].rotation,0) as GameObject;

numOfPlayers += 1;

m_Tanks.m_PlayerNumber = numOfPlayers;

GetComponent().RPC("SetPlayers", PhotonTargets.AllViaServer, numOfPlayers);


Using this method, once one player enters the game, the value stays in 1 and it doesn't change. It's like the variable doesn't sync, and for every new Instance, it's set to be the player 1.

I've tried to use OnPhotonSerializeView too to make the sync, but it didn't do nothing using:

if (stream.isWriting)
{
stream.SendNext(numPlayer);
}
else
{
numPlayer = (int)stream.ReceiveNext();
}


I think that I have an error of concept on how to synchronise variables. I've searched and looked, but I haven't find what I'm doing wrong.

Comments

  • Did you ever get this resolved? I'm having the same issue and would like to know how you fixed it...thanks.
  • UjiPhoton
    Options
    Yes and no.

    In photon you can't sync a variable just saying "sync this variable".

    You either use OnPhotonSerializeView, and continuously pass the string of data, or you use an RPC call.

    My solution was using a RPC call. I initialise independently on each player the number of players to 0, and every time that a player joins the room, call a RPC call that the only things that does is increase in 1 the number of players.
    By the moment It's the only way to have a variable "sync" across the players.

    I really hope that in the future they implement a SyncVar function, method or whatever, like Unity Network. It's the only flaw I could find in Photon.