Structural changes
Structural changes, what you need to consider to avoid shooting yourself in the foot.
When making changes to the structure of an Entity
, there are also a few things to consider. In most cases, you can simply edit an entity using Add
and Remove
.
var entity = World.Create<Dwarf, Position, Velocity>();
entity.Add<Pickaxe>();
The entity gets a new component and does not have it yet, that fits.
But sometimes you need to take a closer look.
var entity = GetRandomEntity(); // Returns an entirely random entity
entity.Add<Pickaxe>(); // What if it already has that one?
entity.Remove<Helmet>(); // What if it does NOT have this one?
Add and Remove do not check whether the Entity
already has these components. In this case, the entity could already have these components and it would lead to a corrupted memory.
It is therefore recommended that you always check beforehand if you cannot guarantee that an entity has or does not have a component.
var entity = GetRandomEntity(); // Returns an entirely random entity
if(!entity.Has<Pickaxe>() entity.Add<Pickaxe>();
if(entity.Has<Helmet>()) entity.Remove<Helmet>();
Why is that?
Last updated
Was this helpful?