Docs / ECS

Glossary: ECSWorld

The methods of ECSWorld, in alphabetical order.

Color legend method

The entry point of the ECS: creates and destroys entities, keeps one store per component type, and runs queries. Systems talk only to the world.

A sealed class. The quick start guide shows it in context, with code. It isn’t thread-safe: see threads. The callbacks of its queries are its nested QueryAction delegates.

Names are colored by what they are: a method.

Methods

ECSWorld() →

ECSWorld() · Creates an empty world: no entities and no stores. The store of a component type is created by the first add of that type.

AddComponent() →

AddComponent<T>(Entity entity, in T component) · Copies the component into its type’s store, or overwrites the one the entity has: an entity has at most one component of each type. The first add of a type creates its store. Throws InvalidOperationException when the entity isn’t alive.

Parameter Type Description
entity Entity The entity to add to. Must be alive.
component T The component. T is any type that implements IComponent.

AddComponent(Entity entity, Type componentType, object component = null) · The same, for a type known only at run time. Without a component, adds the type’s declared default (see ComponentDefaults). Throws ArgumentNullException for a null type, InvalidOperationException when the entity isn’t alive, and ArgumentException for a type that doesn’t implement IComponent, a component that isn’t of the type, or no component for a type that declares no default and isn’t a struct. A rejected add creates no store.

Parameter Type Description
entity Entity The entity to add to. Must be alive.
componentType Type The component’s type. Must implement IComponent.
component object The component, of that type, or null for the type’s declared default. Default: null.

CreateEntity() →

CreateEntity() · A new entity with no components, as an Entity handle. Takes the most recently freed id when there is one, under that id’s new generation, so handles to the entity that had it stay dead; otherwise the next new id, from 0 up.

DestroyEntity() →

DestroyEntity(Entity entity) · Removes every component of the entity, moves its id’s generation on, so every handle to it stops being alive, and frees the id for a later entity. Does nothing for an entity that isn’t alive. Not to be called during a query, see changing the world during a query.

Parameter Type Description
entity Entity The entity to destroy.

GetComponent() →

GetComponent<T>(Entity e) · Returns ref T: a reference to the entity’s component, to read and write in place. It stays valid until the next add or remove of a T on any entity, see how long a reference lasts. Throws KeyNotFoundException when the entity isn’t alive or has no T.

Parameter Type Description
e Entity The entity whose component to get.

GetComponent(Entity entity, Type componentType) · The same, for a type known only at run time: the component as an object. For a struct that is a boxed copy, and changes to it reach the store only when it is handed back to AddComponent; for a class it is the object in the store. Throws ArgumentNullException for a null type, and KeyNotFoundException when the entity isn’t alive or has no component of the type.

Parameter Type Description
entity Entity The entity whose component to get.
componentType Type The component’s type.

GetComponentTypes() →

GetComponentTypes(Entity entity) · Returns IReadOnlyList<Type>: the type of every component the entity has, in no particular order, as a new list. Empty for an entity that isn’t alive. Asks every store of the world.

Parameter Type Description
entity Entity The entity whose component types to list.

HasComponent() →

HasComponent<T>(Entity e) · Whether the entity is alive and has a T. Asking about a type no entity has ever had creates no store.

Parameter Type Description
e Entity The entity to ask about.

HasComponent(Entity entity, Type componentType) · The same, for a type known only at run time. Throws ArgumentNullException for a null type.

Parameter Type Description
entity Entity The entity to ask about.
componentType Type The component’s type.

IsEntityAlive() →

IsEntityAlive(Entity entity) · Whether the handle refers to a live entity: one this world created and hasn’t destroyed since. false for a handle the world never handed out, whatever its id and generation, default(Entity) included until the first entity is created.

Parameter Type Description
entity Entity The handle to check.

Query() →

Query<T1>(QueryAction<T1> action) · Calls the action for every entity that has a T1, with a reference to the component to change it in place. Entities come in the order of the store. Returns at once for a type no entity has ever had.

Parameter Type Description
action QueryAction<T1> Called once per entity. Keep it in a field rather than making it on every call.

Query<T1, T2>(QueryAction<T1, T2> action) · Calls the action for every entity that has both types. Walks the smaller of the two stores and looks each of its entities up in the other.

Parameter Type Description
action QueryAction<T1, T2> Called once per entity, with references to both components.

Query<T1, T2, T3>(QueryAction<T1, T2, T3> action) · Calls the action for every entity that has all three types. Walks the smallest of the three stores and looks each of its entities up in the other two.

Parameter Type Description
action QueryAction<T1, T2, T3> Called once per entity, with references to the three components.

Inside the action, change components through the references, but don’t add or remove components of the queried types or destroy entities: see changing the world during a query.

RemoveComponent() →

RemoveComponent<T>(Entity e) · Removes the entity’s T, moving the store’s last component into its place. Does nothing when the entity isn’t alive or has none.

Parameter Type Description
e Entity The entity to remove from.

RemoveComponent(Entity entity, Type componentType) · The same, for a type known only at run time. Throws ArgumentNullException for a null type.

Parameter Type Description
entity Entity The entity to remove from.
componentType Type The component’s type.