Server Side Persistent Data

Hello every1,

I wanted to know how you guys are implementing server side persistent data on your game servers, keep in mind am asking about the concepts not the code.

Saving player position, inventory , etc… then giving all that data back to the “SAME” client when it happends to connect again?

How could the server know who is the client connected so it can fetch that client information if existent?

*Making a random ID when the game first loads and saving it as a player pref or something like that so i can tell the servers who i am every time?
*Is there a persistent random unity/pc id i can fetch from the system that i could use?
* Should i implement something that could be easily replaced into Steam account system in the future? what do u think?

PD: Games that use binary files for storing data on the server side, and dont have any sort of ‘account’ system, how to give unique identifiers to clients that install the game… (Talking about PC only, if could make any difference)
Thanks aloot for your feedback, am killing my brain looking into ways of doing this…

I am not really experienced in that, so I might be wrong, however I am fairly experienced programmer (not pro, just experienced - at least in my own thinking), and I am thinking about those solutions, depending on preference, and the game itself:
I like to store data in XML. LINQ to XML makes it sweet and easy. And also it’s then easy to look into it and edit if needed.
Also, there’s binary serialization. Ofc, BinaryFormatter is slow and makes big files, but I never ran into need to make own serialization yet.
Sometimes I use external database with access through PHP. Not perfect, but again, I never needed it more than just prototyping.

And if you don’t want to register users in normal manner, you can always go with ‘random’ ID. Just don’t make it truly random - either use auto-increasing on server side, or use hashes, like MD5 hash of current System.DateTime or something.

About how to give it - if you generate it server-side (which imo would be recommended, but ofc it depends on your needs) - client sends generation request, server generates, and sends back the ID.
If client generates - simply generate and send to server.
Either way, both client and server would store it - server probably in files, while client could go with playerprefs or something. Again, depends on your needs.
Then server could load the info into class or whatever you like, every time that client starts and sends it’s ID. Then you could lookup data for that client using LINQ.

I’ll give example of my (prototype) way to store currently connected clients:

namespace SpaceGame
{
  public sealed class Player
  {
  public readonly int ID;
  public readonly NetworkConnection Connection;
  public readonly int AuthCode;

  public bool RespawnAllowed { get; set; }
  public string Name { get; set; }

  public Player(NetworkConnection connection, string name)
  {
  this.ID = connection.connectionId;
  this.Connection = connection;
  this.Name = name;

  this.AuthCode = GenerateAuthCode();
  }

  public Player(NetworkConnection connection)
  : this(connection, null) { }

  private int GenerateAuthCode()
  {
  return (DateTime.Now.ToString() + ID.ToString() + Connection.address).GetHashCode();
  }
  }

public class PlayerCollection : HashSet<Player>
  {
  public Player Find(NetworkConnection conn)
  {
  return this.Where((p) => (p.Connection == conn)).FirstOrDefault();
  }

  public Player Find(int id)
  {
  return this.Where((p) => (p.ID == id)).FirstOrDefault();
  }

  public Player Find(string name)
  {
  return this.Where((p) => (p.Name == name)).FirstOrDefault();
  }

  public IEnumerable<Player> FindAll(string name)
  {
  return this.Where((p) => (p.Name == name));
  }

  public IEnumerable<Player> FilterNameContains(string name)
  {
  string lName = name.ToLower();
  return this.Where((p) => (p.Name.ToLower().Contains(lName)));
  }

  public IEnumerable<Player> FilterNameStartsWith(string name)
  {
  string lName = name.ToLower();
  return this.Where((p) => (p.Name.ToLower().StartsWith(lName)));
  }
  }
}

Ofc, as said, I am not that experienced in networking solutions, I more like have ideas and some general programming knowledge. So others might give you different solutions, etc.
But I like sharing knowledge if someone wants to learn. :smile:

1 Like

These ideas and prototype are pretty darn good for my needs, thanks dude: