Entities
Entities are the "game objects" or "model entities" in applications that use Ark. In effect, an entity is just an ID that can be associated with Components, which contain the entity's properties or state variables.
Creating entities
An entity can only exist in a World, and thus can only be created through a World.
Here, we use new_entity! to create an entity with a Position and a Velocity components. Note that component values are passed as a tuple!
entity = new_entity!(world, (
Position(100, 100),
Velocity(0, 0),
))Components can be added to and removed from the entity later. This is described in the next chapter.
Often, multiple entities with the same set of components are created at the same time. For that sake, Ark provides batch entity creation, which is much faster than creating entities one by one. See chapter Batch operations for details.
Removing entities
Removing an entity from the World is as simple as this, using remove_entity!:
remove_entity!(world, entity)For removing many entities in batches, see chapter Batch operations.
Alive status
Entities can be safely stored, e.g. in the Components of other entities to represent relationships. However, as they may have been removed from the world elsewhere, it may be necessary to check whether an entity is still alive with is_alive:
if is_alive(world, entity)
# ...
endZero entity
There is a reserved zero_entity that can be used as a placeholder for "no entity". The zero entity is never alive. The function is_zero can be used to determine whether an entity is the zero entity:
if is_zero(entity)
# ...
end