Architecture Advice - ECS without events?

Started by
15 comments, last by Oberon_Command 5 years, 11 months ago

Fwiw, @SomeoneRichards, I implement game events as entities in my ECS engine, just like you've described.

Each of my game events are normal entities. Each one has a "PropertySet" component, which works as a blackboard of sorts, holding data relating to the entity. I use this Component for non-game-event entities too- it's just a general-case Component type for storing persistent entity data.

My process of raising a game event goes like this:

  1. Grab the PropertySet Component associated with my game event's entity ID, from a central component database.
  2. Populate it with any data relating to the event (for example, the "enemy" in an "Enemy Detected" event)
  3. 'Raise' the game event's entity ID with my event system.
  4. Anything listening for that event receives a callback, with the entity ID I raised.
  5. For there, it can grab the event's PropertySet Component from the central database, and read data from it as needed.

These game events end up being super light as entities go, so I instantiate as many as I'll possibly need, and pool them at startup.

Advertisement

While there are a number of definitions of ECS, what is common to just about all of them is that entities are a binding mechanism that connects discrete pieces of "state" (which are labelled "components") together in such a way that they share an "identity." As it turns out, you can think of an "event" as a piece of state that is bound to its sender. Therefore, in an ECS-like system where binding state to an entity is an inexpensive operation (which it should be, or you aren't really reaping the benefits of this kind of architecture), sending an "event" to an entity could just consist of creating a new "component" to represent the event, and then updating the event as you would any other component attached to the entity.

55 minutes ago, Oberon_Command said:

sending an "event" to an entity could just consist of creating a new "component" to represent the event, and then updating the event as you would any other component attached to the entity.

I would respectfully question this approach. A few reasons to consider:

  • Organizationally, it may be strange to think of an 'event' as state data for an entity that raises it. In a strict ECS model, all logic takes place in systems. This would mean that if an event is raised, it's a system which does the raising, and not an entity. A system may raise a single event in response to the action of many entities. In that case, for which single entity would the event be considered to hold state?

    I would argue it makes more sense to think of events as their own entities, with their own state data in the form of their own components. Then it doesn't matter what raised them, or under what circumstances. And there's an existing framework in place for how to think about them (the same way we think of other entities).
     
  • This is a personal preference, but if events are components, and if you can raise an undefined number of them during a game, then you may be setting yourself up for a memory management headache. You may have to use a dynamic, growing collection of some kind to store referenced to your components, which could be costly. And if you're using an environment with a garbage collector, you'll also have to watch out for those costs.
2 hours ago, Henrik Luus said:

Organizationally, it may be strange to think of an 'event' as state data for an entity that raises it. In a strict ECS model, all logic takes place in systems. This would mean that if an event is raised, it's a system which does the raising, and not an entity. A system may raise a single event in response to the action of many entities. In that case, for which single entity would the event be considered to hold state?

So? Are systems not allowed to create components? I was assuming a "system" was just a collection of procedures with no state of its own, since that seems to be what most people think of when they think "ECS".

The entity that binds the "event component" is the entity (or entities) to which the event applies.

2 hours ago, Henrik Luus said:

I would argue it makes more sense to think of events as their own entities, with their own state data in the form of their own components. Then it doesn't matter what raised them, or under what circumstances. And there's an existing framework in place for how to think about them (the same way we think of other entities).

Why do you need a separate entity for that? I don't see that "what raised them" particularly influences this design choice. The event component itself can hold that state if needed.

2 hours ago, Henrik Luus said:

This is a personal preference, but if events are components, and if you can raise an undefined number of them during a game, then you may be setting yourself up for a memory management headache. You may have to use a dynamic, growing collection of some kind to store referenced to your components, which could be costly. And if you're using an environment with a garbage collector, you'll also have to watch out for those costs.

I've been assuming that if you're using ECS, you're already using a dynamic collection of components, probably arranged such that components are in homogeneous sub-collections. If you're not doing that, ECS is kind of pointless, because In my view the dynamic nature of ECS is one of its greatest benefits (the other being that designers can glue behaviours together somewhat arbitrarily). If that's an issue, I wouldn't be using ECS.

You may have noticed that ECS isn't a silver bullet, there are cases for which it is not the optimal solution and a lot of corner cases. Communication between components is a common one. :)

Plus you'd have the same problem even if you didn't make them components. If you're storing event state, it has to live somewhere.

7 hours ago, Oberon_Command said:

The entity that binds the "event component" is the entity (or entities) to which the event applies.

I'm not sure how you are going from the singular entity that binds to the potential multiple binded entities. I suppose the component could allow specification of multiple topics. But, anyway, it imposes an asymmetricity that I don't like. I want my entities to contain data (so components) that concerns them only.

I'm working towards an architecture where entities have only their own components, and, of course, specific systems only work on the components that they care about. Then a higher level - the gameplay level - has its own systems, components and entities (scripted couplings in the form of interactions, and the ad-hoc triggering of these interactions, in the form of events).

The entire discussion started because I was wondering why make a new structure for these events when I can just use the existing entity component framework... So far, there are reasons for both, and I am still undecided.

7 hours ago, Oberon_Command said:

I've been assuming that if you're using ECS, you're already using a dynamic collection of components, probably arranged such that components are in homogeneous sub-collections. If you're not doing that, ECS is kind of pointless, because In my view the dynamic nature of ECS is one of its greatest benefits (the other being that designers can glue behaviours together somewhat arbitrarily). If that's an issue, I wouldn't be using ECS.

I've been assuming that this wasn't an either/or case... I'm using ECS in the places that it makes sense for my program - and I'm having this discussion to clarify those places :)

To be clear, I didn't decide on an ECS architecture. I simply arranged the program in a way that seemed logical, refactoring as I went. It is only when I am looking for advice or information about specific questions or problems that I have to force my architecture in to an established paradigm - because paradigms seem to be the primary vocabulary for discussing this things - with all the added confusion that that brings.

8 hours ago, SomeoneRichards said:

I'm not sure how you are going from the singular entity that binds to the potential multiple binded entities. I suppose the component could allow specification of multiple topics. But, anyway, it imposes an asymmetricity that I don't like. I want my entities to contain data (so components) that concerns them only.

Sorry, I should have been clearer - I'm referring to a hypothetical ability of a component to be bound to multiple entities at once. I've never actually seen an ECS that does that, but from the viewpoint of an entity as a binding mechanism rather than the "object itself," it seems like a logical extrapolation, so I didn't want to rule it out. :)

This topic is closed to new replies.

Advertisement