Persistent Data in Multiplayer?

Hello all, I’ve recently begun researching persistent data for saving/loading variables etc in Unity, but I’ve noticed a weird issue when trying to get this working with Unity’s improved UNET. After following the live tutorial found below, I’ve got the jist of how it all works:

The problem comes to syncing it over a network. So for starters, I’ve created a simple InputField that takes texts and creates a name for a player object. It is then sent to a persistent GameObject that floats around the game to send data over. The code for the data object looks like this at the moment:

using UnityEngine;
using System.Collections;
//using UnityEngine.Networking;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class Data : MonoBehaviour {

    public static Data data;

    public string playerName;

    void Awake ()
    {
        if(data == null)
        {
            DontDestroyOnLoad(gameObject);
            data = this;
        }
        else if(data != this)
        {
            Destroy(gameObject);
        }
    }
   
    public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

        PlayerData playerData = new PlayerData();
        playerData.playerName = playerName;

        bf.Serialize(file, playerData);
        file.Close();
    }

    public void Load()
    {
        if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            PlayerData playerData = (PlayerData)bf.Deserialize(file);
            file.Close();

            playerData.playerName = playerName;
        }
    }

    [Serializable]
    class PlayerData
    {
        public string playerName;
    }
}

It’s almost verbatim to the tutorial for testing purposes. It’s also worth noting that I took off the NetworkIdentity component and commented out all NetworkBehaviour code snippets as well for testing purposes. Basically what happens is that when joined within a lobby, the player names of both players appears to be whatever the local player used as their name (i.e. player X says both players as “X” and player Z sees both as “Z”). This issue happened with or without the network components/code.

Not really sure what’s going on right now, to be quite honest. I don’t have many leads, and I’m currently just fiddling with things to find out what’s going on. If anyone has any idea what’s happening, please feel free to share with me! I can post any specific code snippets or inspector screens that may be needed.

One last thing to note is that to my knowledge, it shouldn’t be an error in regards to the network code itself, as I’ve been successfully testing out my project for a good couple of months now and everything works fine with the UNET integration. I’ve just got a problem with this persistent data “overwrite”, if that makes any sense.

When you receive the data over the net do you create a new playerdata object & assign the remote players name to it(do you also call the save function on it,in which case do you modify the save files name). You could also create a List of PlayerData to keep track of everyones different names?.

Hmm, I do NOT create a new player data object for my other players over the net. Everyone just has their own local version, so that might be the issue. I’m still a little unsure of how I would access the remote player’s data. Would I just network my Data GameObject and then try to have one present in my scene for each player? Also this may sound dumb, but having my Data script as a static variable shouldn’t have any problems in multiplayer with multiple Data GameObjects, no? I’ll toy with that idea in a few hours after I get out of college

Yes,a person would need to synchronize the other players names over the connection,you would only have to sync the other remote users names over the net,an example would be: http://docs.unity3d.com/Manual/UNetPlayers.html (simply replace it with a string which represents the local players name) & change the appropriate text,if I am not mistaken. If you are making a turn based game you could also modify your save & load functions to keep local copies of the data to safe bandwidth.Concerning the data GameObjects ,there should only ever be one instance of it in the local players game,if you would like to keep track of all players in your data script then simply add a List of type PlayerData to your class & make sure to add them to that list when you receive the other remote players names from the server,hope this is at least somewhat helpful to you

I actually took a simle route and just made an RPC

[ClientRpc]
    public void RpcPlayerName()
    {
        if (isLocalPlayer)
            playerName = Data.data.playerName;

it started off just as a simple statement in the Start function, but it had some issues. I’m testing it in my player’s Update function for now until I get it working.

So the current problem is that the names only appear for player 2 (client player), while player 1 only has their own name shown. My guess is due to the isLocalPlayer logic. Any suggestions?

Update: messed around with this a little more, and I’m assuming that the client just isn’t syncing. Suggestions?

//if players are objects of the server use ClientRpc else use Command if local http://docs.unity3d.com/Manual/UNetActions.html
[ClientRpc]
    public void RpcPlayerName(string name)
    {
        if (isLocalPlayer){
            playerName = Data.data.playerName;}
        else{
            playername = name;
        }
      


    }

Also here is some tutorials which will help you understand the multiplayer code better

Yea, I actually got everything working shortly after I had the idea for an RPC. I’m quite familiar with them, and I feel kind of dumb for not doing it in the first place. Thanks a bunch for the help though!