What is the best way to do this?

Options
Hello, I have 2 players, each one can before searching game, select his player and edit it to his liking. My question is, when instantiating the prefab that the player selected, how do I send the edited data of that prefab? That is, if the player edited the clothes, how did he send his type of clothes online? I had thought, serialize with photon and send the class, but I don't know if it's the best. Any advice? Thanks.
-Nicolas

Comments

  • S_Oliver
    Options
    Well, how do you save those changes before instantiating the Player?
  • To save the player data, I use a Json serialization, after saving it, I use a "DontDestroyOnLoad". When the scene changes, i can use a "RaiseEvent" for the player from the previous scene or a PhotonNetwork.instantiate locally with its new features, but how do you get that information that was previously edited the other player? Thanks
  • S_Oliver
    Options

    How you persist the data locally is up to you. But if you use json to generate a look for a player object why not just sending the json as string to all player, each player than could setup stuff. Its like a state you have to sync
    Imagine you got a shopping list with some stuff on it everytime you go to the mall you take the list with you and buy the same stuff, if you change something on the list, no problem since you always have the list to lookup.

  • Thank you for your time, your help is really useful. But I don't understand, since I'm a little new in photons, how do I send the json string to the network? with rpc and then deserialize it?
  • NicoProner
    Options
    Thank you, I know the basics, use rpc and raiseevent, but I was wondering if you could give me an example, send a json string for rpc and deserialize it.
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Alright.
    [System.Serializable]
    	public class CharacterData
    	{
    		public Color Color;
    		public float Size;
    		public int SomeOtherStuff = 3;
    	}
    
    	private CharacterData data = new CharacterData();
    
    	public void OnCharacterChanged()
    	{
    		var dataAsJson = JsonUtility.ToJson(data);
    		photonView.RPC("CharacterDataUpdate",RpcTarget.Others,dataAsJson);
    	}
    	
    	
    	[PunRPC]
    	private void CharacterDataUpdate(string data)
    	{
    		var charData = JsonUtility.FromJson<CharacterData>(data);
    		ApplyChanges(charData);
    	}
    
    	private void ApplyChanges(CharacterData data)
    	{
    		//do stuff with it
    	}
    

    Thats just example code. But it should give you an idea.
  • NicoProner
    Options
    Thanks, I managed to do it.