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>(); 

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? 

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?

The reason

You're probably wondering why we don't just install a few security checks to prevent this kind of behavior? Good question!

This has pros and cons, one advantage is clean code and performance. In the future Arch will get optional security checks.

Last updated

Was this helpful?