sync variable question
Options
Hi,
I''m very confused with syncing a variable using a network script. The following script is attached to a gameobject (table and chairs). The "spotlights" code has a simple key function that changes the "seat" to a number (1 to 10). This seat should be synced to all players. But this only works for the first player (MASTER) in the game, when i try to change the seat with the second player, it seems that the "lights.seat" will be overwritten by the "activeSeat" from the other player.
Anyone has a clue why this aint work?
Thanks
I''m very confused with syncing a variable using a network script. The following script is attached to a gameobject (table and chairs). The "spotlights" code has a simple key function that changes the "seat" to a number (1 to 10). This seat should be synced to all players. But this only works for the first player (MASTER) in the game, when i try to change the seat with the second player, it seems that the "lights.seat" will be overwritten by the "activeSeat" from the other player.
using UnityEngine; using System.Collections; public class tableNetwork : Photon.MonoBehaviour { spotlights lights; private int seat = 1; private int activeSeat = 1; void Awake() { lights = GetComponent<spotlights>(); } void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) { stream.SendNext(lights.seat); } else { activeSeat = (int)stream.ReceiveNext(); } } void Update () { if (!photonView.isMine) { if (lights.seat != activeSeat) { lights.seat = activeSeat; } } } }
Anyone has a clue why this aint work?
Thanks
0
Comments
-
As far as I can see the seat is never changed on the masterclient (probably left out..?).
The rest of the code should work AFAIK.
I tested this and this works:int seat = 1; int activeSeat = 0; void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) { stream.SendNext(seat); } else { activeSeat = (int)stream.ReceiveNext(); } } void Update() { if (!photonView.isMine) { if (seat != activeSeat) { seat = activeSeat; Debug.Log("nonMC: new seat "+seat); AddGameChatMessage("nonMC: new seat " + seat, false); } } else if(PhotonNetwork.isMasterClient) { seat = (int)(Time.time / 5); Debug.Log(seat); } }
However, something like this should really be send via an RPC. It's inefficient to rely on OnSerialize for this. Especially if you have tens/hundreds of these lights/tables?0