How do I access a value of the masterclient and then apply it to a certain script on every player?

So I want to be able to sync a variable (a seed) from a hashtable across the network and then apply that seed to every other player's World script which handles the seed, but I do not know how to use the functions that Photon use to handle this kind of stuff, anyways, here's my script:

using UnityEngine; using System.Collections; using Hashtable = ExitGames.Client.Photon.Hashtable; public class NetworkPlayer : Photon.MonoBehaviour { Vector3 realPosition = Vector3.zero; Quaternion realRotation = Quaternion.identity; public Hashtable myValues = new Hashtable(); void Start () { myValues.Add(1, World.currentWorld.seed.ToString()); Debug.Log("Hashtable seed = " + myValues[1].ToString()); } void Update () { if(PhotonNetwork.isMasterClient == false) { } if(PhotonNetwork.isMasterClient == true) { PhotonNetwork.player.SetCustomProperties(myValues); //Is this code below right? I want to be able to print out the seed of the customProperties of my own player so I can verify that it is indeed working properly //Debug.Log("Custom Property Seed = " + PhotonNetwork.player.customProperties[1].ToString()); } if(photonView.isMine) { //Do nothing } else { transform.position = Vector3.Lerp (transform.position, realPosition, 0.2f); transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, 0.2f); } } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if(stream.isWriting) { stream.SendNext(transform.position); stream.SendNext(transform.rotation); } if(stream.isReading) { realPosition = (Vector3)stream.ReceiveNext(); realRotation = (Quaternion)stream.ReceiveNext(); } } }

Answers

  • If you want to sync a seed, it sounds like you won't change it often. You can do that much better by using Custom Properties.
    See: https://doc.photonengine.com/en/pun/current/tutorials/synchronization-and-state
  • Tobias said:

    If you want to sync a seed, it sounds like you won't change it often. You can do that much better by using Custom Properties.
    See: https://doc.photonengine.com/en/pun/current/tutorials/synchronization-and-state

    Right, I've now made a hashtable for my seed value, but how do I access it?

    Here's what I've come up with so far:

    if(PhotonNetwork.isMasterClient == false) { seed = PhotonNetwork.room.CacheProperties(Hashtable() {{"seed"}}); }
  • Did you read that linked page?
    "All updates take a moment to distribute but all clients will update room.customProperties and player.customProperties accordingly."

    You need to check isMasterClient == true.
  • Tobias said:

    Did you read that linked page?
    "All updates take a moment to distribute but all clients will update room.customProperties and player.customProperties accordingly."

    You need to check isMasterClient == true.

    I checked it and I understand what you mean, but I need to access the new variable, so if I update the seed, then I want to be able to change my own seed if I'm not the masterclient but I don't know how to access that specific variable from room.customProperties.
  • There is no new variable. It's a key in room.customProperties.
    If more than one player have to set a seed, each needs a separate key.
    Of any client can replace the seed, either can use room.SetCustomProperties() and put in a Hashtable with the key to update and the new value.
  • Tobias said:

    There is no new variable. It's a key in room.customProperties.
    If more than one player have to set a seed, each needs a separate key.
    Of any client can replace the seed, either can use room.SetCustomProperties() and put in a Hashtable with the key to update and the new value.

    Okay, how would one access one of those keys?
  • Hello, you could also have used an RPC method for that. The master client establish the seeds, and call the RPC for all the players.

    Cheers,
    Don T.
  • @dontonka: That only works if either no one joins later on or if you use a buffered RPC.
    As it's not related to some GameObject (or PhotonView), I would prefer the Custom Property.