How to set customProperties in masterClient for all connected clients ?

Options
Hi everybody!,
I`ve been using PUN for 2 weeks now, Im doing a gunbound style game and Im having trouble using player customProperties. One for "carType" and another for "spawnPos".
For what I see, I can SetCustomProperties in masterClient on OnCreatedRoom () (for masterClient) and on public void OnPhotonPlayerConnected( PhotonPlayer newPlayer ) for each new player.
This seams to work,
I can get masterClient customProperties in masterClient device, and also customProperties of the other players.
But I CANT get the updated customProperties values set by masterClient from player devices.

Each player instantiate his own vehicle:

void OnJoinedRoom()
{
Debug.Log("OnJoinedRoom()Connected to Room");

int playerNumber = (int)PhotonNetwork.player.customProperties["spawnPos"]; //this is line 162 on error.

var playerObj = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(0,30f,0), Quaternion.identity, 0);

playerObj.transform.position = new Vector3(playerSpawnXpos[playerNumber],3f,0);
}

If I run the Unity editor as the second player I got this error:

NullReferenceException: Object reference not set to an instance of an object
NetworkManager.OnJoinedRoom () (at Assets/Scripts/NetworkManager.cs:162)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2113)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1914)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:12

So, I tried getting the customProperty using TryGetValue instead:

int playerNumber = 0;
object hashTableValue;
if (PhotonNetwork.player.customProperties.TryGetValue("spawn", out hashTableValue))
{
Debug.Log("OnPhotonPlayerPropertiesChanged()spawn value: " + (int)hashTableValue);
playerNumber = (int)hashTableValue;
// return (int)hashTableValue;
}
else
{
Debug.Log("key is not in hashtable");
}

When I run the Unity editor as second player, I got message "key is not in hashtable".

How can I set customProperties in masterClient so that I can retrieve those values in each client?,
I need this to set player location on map.

Comments

  • agarrote
    Options
    Up, Anybody there?,
  • vadim
    Options
    To make that properties set by master are updated on local client, handle OnPhotonPlayerPropertiesChanged message.
  • Tobias
    Options
    To set properties, use Room.SetCustomProperties() or PhotonPlayer.SetCustomProperties().
    You can read the properties from the Hashtable but it won't sync them, when you just change the Hashtable's values.
    Maybe that's it?
    See Custom Properties.
  • agarrote
    agarrote
    edited May 2016
    Options
    Hi vadim and Tobias:
    I see, So I can PhotonPlayer.SetCustomProperties() on masterClient for himself and remote clients, but it doesnt mean it will be sync at that moment right?, I can only check that in OnPhotonPlayerPropertiesChanged() from each remote client ?.

    I need remote clients to get their position BEFORE they get instantiated, position is read using:
    (int)PhotonNetwork.player.customProperties["key"] .

    How can I make sure data is updated before using it?, I noticed remote client calls OnPhotonPlayerPropertiesChanged() twice, one just after entering room another time later after being instantiated. Second time has the right values. Any ideas please ?

    UPDATE:
    It seams OnPhotonPlayerPropertiesChanged() gets called twice on remote client because there are already two calls to PhotonPlayer.SetCustomProperties() from masterClient?, one for himself and second for remote client?.
    So console shows:
    OnPhotonPlayerPropertiesChanged() first call (data from first photon player whos data is changed(masterClient).

    Remote client trying to get customProperties(cant read hashtable).

    OnPhotonPlayerPropertiesChanged() second call with data for remote player.
    So data is not ready when is needed. am I on the righ path?,
  • jeanfabre
    Options
    Hi,

    Maybe a good way out of this would be to instantiate but with everything turn off ( colliders, mesh renderers etc), and then once you catch the position, proceed further.

    You could also yield maybe, it could be just a matter of waiting for the next frame for everything to be available properly.

    Bye,

    Jean