I have a game engine using an entity-component system (ECS). I serialize the data contained in the ECS by having a system that registers serializers for different component types. I want to create networking,= using authoritative peer-to-peer, where I still have a server, but it acts only as the traffic center, and does not run the game logic. The idea is that specific entities are synchronized across client instances.

However, there is a problem with this. While I can easily serialize my data, I don't know how to synchronize it over a network, or load it from a file. The problem lies in how my system communicates; how do I tell which entity is which, on the other end? How do I let the receiving side know which entity the sent data was meant for?

My entity ids are currently generated as sequential numbers, but it feels very unreliable to use them, as there could be a time when the numbers are not synchronized. For example, one might create a decal entity sooner, or the order in which they are created could be different. This also makes seeded random GUIDs unusable.

What system could I use to identify my entities, when sending data over network or storing them in file?


1 Answer 1


I dont think i know enough details, because im afraid i dont understand the situation correctly. However, if i understand it you are trying to sync entity IDs over network so multiple games on different computers would have same entity IDs.

Since you are dealing with networking you must have a server and it should be the server's job handling the IDs. So for example when an entity is created the client should send a request for new id to server(would also have to send the type and initializing component data) and server would then send the id, type and serialized component data to all the clients except the first one which would only get the id, so that they would all get to know about the new entity(this would apply for bullets for example when the player shoots a bullet the bullet would be created on player's computer and so on ).

It would be different regarding the world. AI, map generation, etc. should work on the server. Any kind of world entity would be created on server because all the clients must have the same world experience.

Regarding the file reading / writing of the component data it should again be the server's job doing so. This means that for example when the server would start it would read the files and initialize the components and then the clients would send requests for intialization data that the server read from files. Each client would confirm that they recieved the data and when they would all confirm server would start the game and so on.

All in all make sure that server controls the IDs and be careful with situations like coop mechanics where the server would actually be one of the clients so then that one client doesnt need to run a seperate server program but rather just open a server within the game and use it for its own game as well(it would simply share its game data with other clients and would recieve events from other clients).

I made up the exaples stated above on spot and i do not know if those are best solutions and i do know they are not accurate and they are not complete. However, i wanted to support the idea with examples, so it really depends on you how you will actually implement this idea, if you ever will that is.

I hope i didnt misunderstand the problem or that i didnt forget anything important regarding it and i hope i helped. I wish you the best of luck with the engine :)

EDIT I totally missed the point here with this answer(for some reason i totally missed the p2p part).

As you have p2p you want to sync IDs every time an entity is created / removed. When entity is created for example just sync the new ID and data. Now with this approach you would have to do that for every entity and it could hit the performace dramatically. You dont want to sync every entity out there, so what you could do is that you logically divide entities into sync and non-sync entites and have those groups generate seperate IDs for their own entities. You could also approach this by still having all the entities with same ID generation and just create a hash table for sync entities. There are probably some better ideas, since I just made these up on spot, but this should give you an idea to think about. The latter is imo better since you dont have to optimize the core of your ECS but you can just add the hash table elsewhere(p2p network module or sth), it is also better regarding the performance.

When saving to files you should just save the hash table as well and you are done.

I hope I helped this time and that I didnt miss the point again. Again, good luck :)

  • \$\begingroup\$ I've added a paragraph to my question. Its architecture is similar to peer-to-peer, the main thing being the clients are independent, just synchronized. \$\endgroup\$
    – akaltar
    Nov 2, 2016 at 2:29
  • \$\begingroup\$ Im sorry, I have missed the p2p part for some stupid reason ._. . Well i think you could just sync when each entity is created / removed. When 1 creates an entity for example it then sends the data to all others and so forth. This way you can use any kind of ID generation and they will still all have the same IDs. Other games like RTS games just have a host peer which controls the gameplay. I dont know however how good this approach is regarding the performance \$\endgroup\$ Nov 2, 2016 at 10:12
  • \$\begingroup\$ Also, if you are making a game engine I suggest you implement both server-client and p2p approach and let enough freedom to the user to use both as he/she wants to. Just suggesting, because imo it depends on the type of game as to which type one would use so you imo cant just make p2p approach so generic that any kind of game could be made with it and that it would still have decent performance. \$\endgroup\$ Nov 2, 2016 at 10:17
  • \$\begingroup\$ You missed it last time, because it wasn't there, I edited it in. Your answer follows my thinking but misses the problem I am having. I'll add an example in my question. And it also made me realize a possible solution. \$\endgroup\$
    – akaltar
    Nov 2, 2016 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .