I am simply beginning to implement my very own ECS and am drawing all the pieces down on paper to verify I perceive all of it earlier than tackling the implementation in code. Nonetheless I am getting caught on entity-lifetime and nothing I’ve learn actually solutions my query.
Simply to make certain I am not lacking one thing, my understanding thus far is:
- Parts of the identical kind all sit subsequent to one another in reminiscence
- Entities are simply an ID, which Parts discuss with (i.e. Parts have an “proprietor”)
- Parts all have a singular Id
Nice! So for instance, say I’ve a PositionComponent
, VelocityComponent
, and VisibleComponent
.
struct PositionComponent {
vector2f place;
};
struct VelocityComponent {
vector2f velocity;
};
struct VisibleComponent {
Picture imageToRender;
};
Nice. Now for example I’ve two kinds of Entity:
- EntityA has a place, velocity, and visual elements
- EntityB has a place and velocity element, however just isn’t seen
(let’s hypothetically say that the Picture
within the seen element is the precise picture information as a result of each entity is tremendous distinctive or one thing)
Say I’ve an PhysicsSystem
which needs to iterate over all of the Place and Velocity elements and replace the Place. This works, as a result of each entity I’ve created has each of those elements.
And so we’re utilizing EntityID as an index into the element arrays, we are able to simply know we’re accessing the proper ones.
However then if there was a RenderSystem
that desires to iterate over all of the Place and Seen Parts, the issue is that the EntityId can now not be an index into the arrays, as a result of not each Entity has each element.
So; the best way I’ve thought to handle that is by having an “Entity to Index Map” for every element. Mainly, this EntityID has a PositionComponent
in index 232 of the array.
However due to this, now any system that wants two or extra Parts to behave, will lose out on all the advantages of an ECS, proper?
I dismissed giving all elements arrays empty house to maintain all of them “in sync”, however meaning I may have loads of wasted information, which solely will get worse the extra totally different elements I create.
There’s then the issue of lifetime. If EntityID 232 is killed, I ideally would shift all of the elements in these arrays right down to fill the hole, which once more means I can not use EntityId as an index into the arrays.
An answer I considered right here was to re-use EntityIDs after they’re now not wanted, possibly through the use of a 16bits of a 32-bit ID because the precise array index, and the opposite 16-bits as some type of random guid to maintain it clear that it is a new entity (and never the previous one who’s EntityID is being reused). This implies I needn’t shift the info round, and would not must hold a map from ID to Index; but it surely does not resolve the issue the place not all entities have all elements, so not one of the arrays can ever be assumed to be “in sync”.
So in abstract my questions are:
1. How does a System in an ECS deal with iterating over two separate elements, when it isn’t assured each element arrays may have the identical entities elements in the identical indices?
2. Is it extra useful to maintain the info compact, or hold the arrays in sync?