Allocating Entities within an Entity System
- by miguel.martin
I'm quite unsure how I should allocate/resemble my entities within my entity system. I have various options, but most of them seem to have cons associated with them. In all cases entities are resembled by an ID (integer), and possibly has a wrapper class associated with it. This wrapper class has methods to add/remove components to/from the entity.
Before I mention the options, here is the basic structure of my entity system:
Entity
An object that describes an object within the game
Component
Used to store data for the entity
System
Contains entities with specific components
Used to update entities with specific components
World
Contains entities and systems for the entity system
Can create/destroy entites and have systems added/removed from/to it
Here are my options, that I have thought of:
Option 1:
Do not store the Entity wrapper classes, and just store the next ID/deleted IDs. In other words, entities will be returned by value, like so:
Entity entity = world.createEntity();
This is much like entityx, except I see some flaws in this design.
Cons
There can be duplicate entity wrapper classes (as the copy-ctor has to be implemented, and systems need to contain entities)
If an Entity is destroyed, the duplicate entity wrapper classes will not have an updated value
Option 2:
Store the entity wrapper classes within an object pool. i.e. Entities will be return by pointer/reference, like so:
Entity& e = world.createEntity();
Cons
If there is duplicate entities, then when an entity is destroyed, the same entity object may be re-used to allocate another entity.
Option 3:
Use raw IDs, and forget about the wrapper entity classes. The downfall to this, I think, is the syntax that will be required for it. I'm thinking about doing thisas it seems the most simple & easy to implement it. I'm quite unsure about it, because of the syntax.
i.e. To add a component with this design, it would look like:
Entity e = world.createEntity();
world.addComponent<Position>(e, 0, 3);
As apposed to this:
Entity e = world.createEntity();
e.addComponent<Position>(0, 3);
Cons
Syntax
Duplicate IDs