I'm developing an multiplayer game "from scratch" (not using a big Engine-Suite like Unity but the lower-level libgdx).
Currently I've got a NetworkManager which handles the sending of messages on server side and receiving those with subsequently calling methods on client site.
At the moment the sync between figures works already once they are correct configured and spawned on both ends. Now I'm currently think about the spawning of those objects.
Approach No.1: My first approach was that every time the Server spawned a new (e.g.) player, it will subsequently send a "SpawnObjectMessage"-Object (holding all necessary type and configuration values) to all clients. Clients will than spawn a new player-figure. Now the synchronization works.
- I have concerns with this solution that individual messages could be lost (due to network problems, for example),
- or what happens if a client closes the program in the middle of the game and then starts again and all figures have to be reloaded (as already said : The server only sends the message once as soon as it spawns a player itself).
Approach No.2: The server hold a "model" of all figures (he does this anyway due to some other functionality) and sync this with all clients. The "model" here is a list of objects each one describing values of a figure: The
- prefab/class/type the object is
- if it is spawned of or just created on server side
- is it controlled by a player and possibly the PlayerID
- ... The server will send the complete List (or only the changed entries) to all clients every time some changes occur. The clients will override their local list and subsequently iterate over it and spawn objects if they could not find a corresponding counterpart. If a new Player enters the game or a client restarts the game, the server will resend the complete list to the new client.
- This solution seems a little bit "reactive"
- It maybe not very intuitive
- Maybe I'm overseeing something what will bring problems
Which approach is more common or what concerns are you aware of in such projects?
Since @milk asked how Updates works here is an additional explanation how this will work: The server will send continously update messages (e.g. "UpdatePositionMessage") for every Player-Objects exiting/spawned on server side. Server will keep doing so no matter if clients has successfully spawnend the corresponding counterpart object or not. Clients will simply ignore updating objects which can't be found. This update-mechanism is used in both approaches described above.