Skip to content

Entities ​

An entity is an 8-byte handle, Entity: an int Index (the slot) plus an int Generation. It owns no data itself; its components live in per-type pools.

Creating and destroying ​

csharp
// Always create with at least one component
var entity = world.CreateEntity(new Position { X = 1 });

// Add more components manually
world.Add(entity, new Velocity());
world.Add(entity, new Health { Value = 100 });

bool alive = world.IsAlive(entity);
world.DestroyEntity(entity);

// Copy entity with all components
var copy = world.CopyEntity(source);

Auto-destroy: entities are destroyed when their last component is removed. That is also why CreateEntity takes a component: an entity is alive if and only if it has at least one.

DestroyEntity of a dead entity is a no-op. CopyEntity creates a new entity with copies of all components of the source (by value, or through IAutoCopy); on a dead source it returns Entity.Null in release and throws under KENSEI_DEBUG. When a listener of a copied component destroys the copy, copying stops and the returned handle is dead, so check IsAlive if your listeners can do that; the same holds for CreateEntity when a listener of its component destroys the new entity. An AutoCopy that destroys the copy, clears the world or restores a snapshot ends the copy too: the value it returns is stored nowhere (up to 2.5.0 it was stored on the freed slot, where the next entity created there found it, or on the restored entity that had the copy's handle). A source that has no components left, which a world or filter listener of the removal of its last component sees before the auto-destroy, is not copied: CopyEntity returns Entity.Null in every build (up to 2.2.1 it created an alive entity without components, which no filter saw and only an explicit DestroyEntity removed). If an AutoCopy or a listener of a copied component throws, the copy is destroyed before the exception leaves CopyEntity: the caller never gets its handle, so a half-made copy would be an entity nothing can reach. World listeners never hear of it (it is destroyed inside its own creation, before OnEntityCreated); typed and filter listeners that heard a component added to it hear it removed. Up to 2.4.0 the copy stayed alive, with the components copied before the failure, or with none when the first one failed. A world listener that throws in OnEntityCreated is a different case: the copy is complete by then and stays alive, like the entity of a CreateEntity whose OnEntityCreated throws.

Handles vs. indices ​

Filters yield int slot indices; World methods take Entity handles. An Entity carries a generation and stays safe forever: once its entity is destroyed, IsAlive is false, and after the slot is reused by another entity it still does not match. An int index has no generation: it is valid only until the end of the current iteration.

WARNING

Store Entity handles, never ints.

csharp
ref var target = ref world.Get<Target>(hunter);
foreach (int e in enemies) {
    var handle = world.GetEntity(e);     // convert inside the loop
    target.Enemy = handle;               // store the handle, not e
}

// Frames later: the handle tells whether its entity still exists
if (world.IsAlive(target.Enemy)) {
    ref var enemyHealth = ref world.Get<Health>(target.Enemy);
}

GetEntity(int) on a dead slot returns the handle of the entity that last lived there; IsAlive is false for it. Once the slot is reused, the same index names the new entity.

When you are not sure an index refers to a live entity, TryGetEntity checks first; it is safe for any index:

csharp
if (world.TryGetEntity(index, out Entity entity)) {
    // the slot holds an alive entity; entity is its handle
}

Debug names ​

csharp
world.SetName(player, "Player");       // does nothing without KENSEI_DEBUG
string name = world.GetName(player);   // null unless named

Names show up in the World Inspector and Profiler (see Debug Tools), and are dropped when the entity is destroyed. Without KENSEI_DEBUG, SetName does nothing and GetName always returns null. The call is not removed: its arguments are still evaluated, so world.SetName(world.CreateEntity(...), "Player") creates the entity either way.