Search Unity

How to maintain and sync player data?

Discussion in 'Multiplayer' started by iret, Aug 11, 2009.

  1. iret

    iret

    Joined:
    Aug 19, 2008
    Posts:
    18
    Hi everybody,

    maybe this is a very stupid question, but I'm struggling with this for 2 days now.

    My problem seems to be simple:

    I have a c# class with player data in it (think of score, ammo, player name and so on). Let's call it PlayerData for now. And there is a List<PlayerData>.

    When starting a network game, my server is creating a first entry to this list for it's local player. No problem so far. ;)

    Now I want to do the following:

    - Add an entry to my playerlist every time a new client connects to server.

    - Sync values of PlayerData when client or server has changed something (I maintain a Changed-Flag for this purpose). After connecting a client has to set it's username, later server may change scores for example.

    - Finally, remove PlayerData from list when a client disconnets from server.

    I did the trick moving serialized PlayerData using RPCs from client to server and vice versa. But I'm not sure, if there isn't a better way of doing this?

    I encountered another problem when a client is disconnecting: I hold corresponding NetworkPlayer in every PlayerData, so I thought it would be an easy task to remove old PlayerData from list - just looping through list and removing every entry for disconnected NetworkPlayer. But it doesn't work for some reason.

    Well, basically I would be happy with some kind of unique NetworkPlayer-ID, that enables me to link my PlayerData objects to a specific NetworkPlayer without need of storing whole NetworkPlayer (or it's reference, what may cause my above problems).

    I read in some forum posts, that player.ToString() (talking of NetworkPlayer, of course) should gimme some global ID. But when checking out, Debug.Log(player.ToString()); prints "0" for client and server every time I connect (via LAN).

    How did you do networked playerlists in your projects?

    Btw, is there a reason, why Unity's MasterServer is ignoring my limitation to max 4 connections (in Network.InitializeServer and Network.maxConnections)? Hostlist keeps saying that my game would accept up to 5 connections. Interesting, should be one less. ;)

    Thank you all in advance! :)

    Christian
     
  2. iret

    iret

    Joined:
    Aug 19, 2008
    Posts:
    18
    Player list problem solved, I changed the following:

    - One list with my player data (name, power etc.)
    - One list with just NetworkPlayer and player ID

    This way I send player data objects without NetworkPlayers over RPC. I do this only when something in my player list has changed or a new client connects.

    When somebody disconnects from server I search for his NetworkPlayer in second list, take the player id and remove corresponding player data item from first list.

    At time of posting my original question I had NetworkPlayer as a part of player data. So it was stored in same list and got serialized to send it over RPC. After deserializing I wasn't able to compare a stored NetworkPlayer against the ones I got in network events (e.g. OnPlayerDisconnected). Result was a player list mess. ;)

    Now my NetworkPlayer objects never got serialized and everything works fine. :)

    But my hostlist keeps saying one max player more than I told Network.InitializeServer. I'm going nuts.

    bye
    Christian