Entity-component system and networking

Started by
8 comments, last by AgentC 10 years ago

Got some problems with coming up with how the state replication mechanics should work in a "entity component system"-design.

I'm using the C++ port of artemis from vinova. No problems there, works great so far. For networking I'm using enet and I have a basic system up and going. I can send a "entity created with these components"-message, and the client creates a new entity which is identified by a unique ID(sent with the "entity created"-message).

All this is good, but the problem comes when I want to synchronize states across the network. How should I implement that on the server? Should there be a system that loops over and creates a packet for all the states that needs syncing? How should I implement this on the client?

My ideas so far;

Server --

Since the server only sends states for object close to client I am currently;

Loop over all entites:

1. Check with all players, is the entity close?

1a. If so, does it need syncing?

1b. Yes; send a packet with the new state.

Client --

No idea..

If anyone have some tips, ideas, or could share how they solved this it would be great.

Advertisement

Your client shouldn't be sending state to the server. The client should just send inputs from the player. The server validates the inputs, applies them to the player's entity (on the server) and then sends the state back to the client (as well as other nearby clients). Your client should immediately apply the input locally and then make corrections based on the returned state from the server to keep the client in sync. Anyway, this is how my system works and it works well for me.

The question has been asked and written about many times. What have you searched for?

The networking forum FAQ item #12 has several links with answers.

Also, immediately the 1500 archers article (mentioned in the FAQ) comes to mind, as do the "Tale of Two Desyncs" articles (not in the networking FAQ).

I think you misunderstood. I'm not wondering how to structure my packets or what to put in them. I'm wondering how I should handle the data on the client side.

Whats the cleanest way from receive data to processing it and updating entities?

What my clients send and doesn't send is more suited in a new topic I think.

I have just been over this same issue, although my game features a small number of Networked Entities which all need to know about each other constantly ( It is a FPS Based Arena Game ).

As I don't have to deal with Area based Syncing it becomes much simpler but this is essentially the way I do it.

I have a Component on the server version of the Entity that essentially sends out a Sync Update Packet after a specified elapsed time, which is currently about half a second.

On the Client the Packet is Received and stuffed into an Entity Update Message that is sent via the Entity Messaging System to the Entity with the Unique ID and there is another component that basically waits for the Entity Update Messages and then applies them to the Entities Data, whether that is by interpolating the movement or just setting some flags i.e. Casting a Spell.

This is a pretty simple method and seems to work for me, although this one component does need to know about a lot of the Entities Data.

An alternative method would be to create a component that does the same thing but for each set of Data for the Entity.

So If you have Damage Data and Position Data, you could create a PositionSyncUpdate Component and a DamageDataSyncUpdate Component. This is much more fitting but involves creating a lot of Components. This method would be better if your Entities change their Components a lot and is much more reusable. I may potentially change to this method at some point.

Oh as a note, in my system a Component is a class that defines one set of functionality on sets of Data.

Hope this helps somewhat and I haven't completely missed the point :)

I have just been over this same issue, although my game features a small number of Networked Entities which all need to know about each other constantly ( It is a FPS Based Arena Game ).

As I don't have to deal with Area based Syncing it becomes much simpler but this is essentially the way I do it.

I have a Component on the server version of the Entity that essentially sends out a Sync Update Packet after a specified elapsed time, which is currently about half a second.

On the Client the Packet is Received and stuffed into an Entity Update Message that is sent via the Entity Messaging System to the Entity with the Unique ID and there is another component that basically waits for the Entity Update Messages and then applies them to the Entities Data, whether that is by interpolating the movement or just setting some flags i.e. Casting a Spell.

This is a pretty simple method and seems to work for me, although this one component does need to know about a lot of the Entities Data.

An alternative method would be to create a component that does the same thing but for each set of Data for the Entity.

So If you have Damage Data and Position Data, you could create a PositionSyncUpdate Component and a DamageDataSyncUpdate Component. This is much more fitting but involves creating a lot of Components. This method would be better if your Entities change their Components a lot and is much more reusable. I may potentially change to this method at some point.

Oh as a note, in my system a Component is a class that defines one set of functionality on sets of Data.

Hope this helps somewhat and I haven't completely missed the point smile.png

But how does the component access the networking part of the application? How does the server component access NetworkServer?

How do you handle it on the client end? Do you read the package and then send it directly to the component on the given entity?

I usually add a writePacket and readPacket method to the networked objects. The objects can also mark themselves for a network update. After the game loop the server will loop through all objects to check if they're marked, and if so, it will give the object the chance to write data by calling the writePacket method. This is obviously a very simple explained but you get the idea, I just hope I interpreted your question correctly.

But how does the component access the networking part of the application? How does the server component access NetworkServer?

How do you handle it on the client end? Do you read the package and then send it directly to the component on the given entity?

In my Implementation the Component for sending the Network Update Packets has access to the Network Server via one of it's data members.

1. So within that Component the only thing that is happening is all the data for that entity being packaged up into a Network Packet and then Calling the Network Server BroadcastPacket function to send the Packet to the clients.

2. On the Client I receive all Packets through one PacketHandler Class which then takes the raw Packet and Creates a Message with just the relevant data and sends the Message to the specified Entity.

So the Raw Packet Data comes across with things like Packet ID, Timestamp, Entity Unique ID and Position Data. This is then created into an Update Message which will have the Position Data and sent to the Entity using the Unique ID. This way the component will only have the information that it needs i.e. the Position Data.

This is only the way I'm currently doing it and it may not suite your specific needs but hopefully it's a good basis :)

I think you misunderstood. I'm not wondering how to structure my packets or what to put in them. I'm wondering how I should handle the data on the client side.

Whats the cleanest way from receive data to processing it and updating entities?

What my clients send and doesn't send is more suited in a new topic I think.

The two are very intimately related.

How you handle the data on either side is entirely dependent on what you put in them.

The network is just a way to transmit data from point to point. In many ways it is the same as a file system or the same as an event system. One side writes data and the other side reads it.

Your client receives information. Exactly how you deal with that information is unique to your app and your protocol. Maybe you take a packet-based system and dump the events directly on a communication bus to be picked up by any interested listeners. Maybe you have a giant if/else tree or switch statement and handle each one directly. Maybe you plug the results directly into some stream reading system. Maybe you do some combination of all of them. The usage will depend entirely on what data you are sending.

I've used two kinds of systems for sending and receiving network data. Nowadays I prefer the latter for its genericity.

1) like suggested above, manually implement writeMessage & readMessage functions into every component. For network use, this typically needs bitfields to optimally tell what has changed, because a network message might not modify nearly all attributes of a component at once.

2) implement an attribute introspection system for components. For each component type you will be able to enumerate their attributes and with some suitable template code or "variant" variable mechanism, can loop the attributes generically for any component to process network writes and reads, and keep track of whether a value has changed since last update and actually needs to be sent. For example a 3D transform component might have 3 attributes: position (Vector3), rotation (Quaternion), scale (Vector3)

Generally, you could think of the network client as an external "editor" acting over your scene, ie. change that entity's position, spawn that entity, delete that entity, according to instructions from the server. By thinking this way, the entities or components don't even need to know there's networking going on, rather the server or client accesses them.

You probably need interpolation or extrapolation too so it's not exactly that simple, but those could be handled by a dedicated system.

This topic is closed to new replies.

Advertisement