0
\$\begingroup\$

Maybe I'm asking dumb question, but this problems bothers me...

So I have my networked game in Unity (client), and server written in C# (without Unity). I Use ENet library for networking. Most of networked components are written by myself (lobby, components etc.).

Server doesn't know about Unity entities like players, obstacles etc. In most cases I send message ("hide object with id 4") to the server, and server pass that message to other clients where they applied message information.

What my problem is, how to assign proper IDs to networked objects, that will be the same across all players?

What I do is that:

  1. On all clients I get all components with specific network component (e.g. NetworkRigidbody)
  2. I sort all of objects using special comparer which compares based on component name with all parents appended to it
  3. ID is just an index in sorted array

Above algorithm works, but I have a feeling that in normal games that problem is handled in other manner.

I searched over Mirror, MLAPI and other libraries source code and I couldn't find how entity id is generated, especially when server is not Unity application.

\$\endgroup\$
0
\$\begingroup\$

A simpler method is to set a free ID on creation and send to other clients "create entity of type foo with ID 4 and parameters x,y,z,bar"

There is a danger of collision when selecting IDs for which you need a mitigation strategy. Either by error message and some kind of recovery or pre-agreeing a pool of IDs for each entity creator which can grow as needed.

Fixing the ID on creation also gives you the guarantee that the ID for a specific entity can remain the same for as long as it is alive.

\$\endgroup\$
  • \$\begingroup\$ I understand, thank you! But what about if I have on my scene 10 objects to network already? Each client has the same scene. There is a possibility that on build time I could assign IDs (or on creation time in editor), so every client with that build would have the same id. Is it correct assumption? \$\endgroup\$ – Klocek Aug 23 at 7:16
  • \$\begingroup\$ Make 1 client responsible for creating the scene and assigning the IDs. The other clients then join that scene. \$\endgroup\$ – ratchet freak Aug 23 at 7:55
  • \$\begingroup\$ Thank you for all your help! So, I understand that client (who created the scene) should serialize scene and send to all other clients, and then client should load that scene? \$\endgroup\$ – Klocek Aug 23 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.