EntityReference

EntityReference, keep entities apart.

You already know what an Entity is. However, one thing has not yet been mentioned: the ID of an entity can reappear from time to time. This is called recycling, if an Entity with the ID of โ€˜25โ€™ is destroyed, one of the next entities will receive its ID. An ID will never appear more than once at the same time, but it can appear more often over time.

So it is possible that you have saved an Entity struct somewhere that originally refers to a dwarf... This is destroyed, you don't realise it and the next time you access it, it's suddenly an elf.

To prevent this and work memory-efficiently at the same time, there is EntityReference for this purpose.

var entity = world.Create<Dwarf, Position, Velocity>();
var reference = 

// Checks if the entity is the same as before by comparing the version and its id
if(reference.IsAlive()) {
    var referencedEntity = ((Entity)reference);
    Console.WriteLine(referencedEntity);
}     

If the original Entity to which the EntityReference points is destroyed, EntityReference.IsAlive()will return false. Even if the ID of the Entity has been recycled. It therefore ensures that we only work with the referenced Entity.

And now you can safely and easily handle references to other entities, great, right?

Last updated

Was this helpful?