Need some help with networking entity-component system

Started by
11 comments, last by Telanor 10 years, 8 months ago
Currently I have an entity-component system where code will run on the server to spawn an entity from a factory method, which then sends a packet out to the clients and they create the entity locally using much of the same code. This worked fine for basic types of entities, like an arrow being shot out.

Now I'm working on adding various different types of special arrows, in this case an exploding one. What I planned to do was hook up an event that fires when the arrow collides which would trigger an explosion. Client side, this would just display some particle effects, while server side it would run the damage calculations and then send that out.

The problem is the code that says "spawn exploding arrow" runs only on the server, since that's where entities need to be spawned. So while the server can easily hook up that event, the client is just receiving a generic "create arrow" packet. I see two possible solutions to this, but I'm not sure which is better, or if there's perhaps a better solution.

Solution 1 would be to make a new set of factory functions with a custom packet for each type of arrow or whatever I happen to need. So instead of just "create arrow" and hooking in custom events, it'd be "create explosive arrow", "create water arrow", etc (of course I'm talking about entities that differ in the code they run rather than merely stat differences). I'm not particularly fond of this as it involves creating different packets for every single type of arrow (and whatever other entities) and possibly some other duplicate work.

Solution 2 would be to not bother trying to get the code to run on the client side and instead make another packet that the server sends out that says "hey client, create some particles here". This is probably a neater solution but I think it could lead to the explosion being delayed.

So, any thoughts on which is preferable? Or is there a better solution?
Advertisement

I would add some packets that allow you to make an entity from the ground up if needed. Things like an add component packet and such. Then you could create the arrow and then just add the exploding effect as a component.

1.

Why does the "create <blah>" function (and packet) not take configuration information?

I e, "create arrow" "include the exploding component."

enum Bool { True, False, FileNotFound };
How would the receiving end know what to do with the exploding component? Does the exploding component's constructor examine the entity object it's being attached to, find the Projectile component and hook itself up to the Collided event? That seems like a bit too much interconnection. Does that mean someone has to make a "mid air explosion" component now if they want to have the arrow explode on a different event?

The system doesn't have the ability to serialize a whole entity across the network, only the update state, because of complications like this. I wasn't sure how to deal with serializing these one-time setup things (such as hooking up events), so it was designed to send only the needed info, like position, rotation, owner and entity ID, then call most of the same code the server just ran using that info. Clearly this has a bit of a flexibility problem though, as I'm now realizing.

> only the needed info

> exploding component is needed info

> no one cares about your entity-component design mumbojumbo

Send needed info. Simple. xD

Send needed info. Simple. xD


And what info do you send in order to hook up events?

What happens on the server when you create this entity? Why isn't this information also available on the client?

enum Bool { True, False, FileNotFound };
Well the server creates the entity, assigns it an ID, creates and attaches all the components, and sets all the data values depending on the variables passed to the factory function. It can easily set up whatever event hooks it needs to, either in the factory function or the calling code itself. At the end of the factory function, it creates a packet with the needed data and sends it to the clients. On the client side, during startup, the factory functions are registered in a dictionary that translates the entityType ID into a function call. When a packet with that entityType ID is received, the function is called, which then reads the packet and runs most of the same factory code the server just ran using the values read from the packet.

So if I were to make a separate factory + packet for every single entity and all its "functional variations", then the client would indeed have all the info it needs. But that seems like a less-than-ideal design to me. With the way the system is currently, the calling code on the client side won't even have access to the entity. Using "shoot bullet" as an example action: client side, the code for that action would just say "create some smoke particles at the barrel" and maybe play an animation. Server side, the same action creates a bullet entity, so it can hook up events there if it wanted.

So in essence: the client calling code doesn't have access to the entity because it's waiting for the server, and the client factory code doesn't know anything about "exploding arrows", it only knows how to create an "arrow"

I would add some packets that allow you to make an entity from the ground up if needed. Things like an add component packet and such. Then you could create the arrow and then just add the exploding effect as a component.


Sorry, somehow I completely missed this post.

That's something I've considered but still wouldn't deal with the event hooking situation. Suppose there's some entity that fires 3 events: OnSpawned, OnAttacked, OnAttack. If I want to make entity A self destruct whenever it gets hit and entity B self destruct on spawn, how would I communicate where the "explosion component" should attach itself?

This topic is closed to new replies.

Advertisement