class Entity : Object

The superclass of entitys and cells, holding their components.

components: Component[]

A table containing all of the entity’s component instances. Generated at runtime.

componentCache: table<Component, Component>

This is a cache of prototype -> component for component queries, reducing most queries to a hashmap lookup.

__new(self: Entity)

Constructor for an entity. Initializes and copies the entity’s fields from its prototype.

give(self: Entity, component: Component) Entity

Adds a component to the entity, replacing any existing component of the same type. Will check if the component’s prerequisites are met and will throw an error if they are not.

Parameters:

component (Component) – The component to add to the entity.

Returns:

_1 (Entity) – The entity, for chaining purposes.

remove(self: Entity, component: Component) Entity

Removes a component from the entity.

Parameters:

component (Component) – The component to remove from the entity.

Returns:

_1 (Entity) – The entity, for chaining purposes.

ensure(self: Entity, component: Component) Entity

Gives the component, but only if the entity doesn’t already have it.

Parameters:

component (Component) – The component to ensure.

Returns:

_1 (Entity) – The entity, for chaining.

has(self: Entity, ...: Component) has: boolean

Checks whether the entity has all of the components given.

Parameters:

... (Component) – The list of component prototypes.

Returns:

has (boolean) – True if the entity has all of the components given.

get(self: Entity, prototype: <T>, ...: unknown) <T>?, ...?: Component

Returns the components for the prototypes given, or nil if the entity does not have it.

Parameters:

prototype (<T>) – The type of the component to return.

getName(self: Entity) string

Returns the entity’s name from their Name component, or the className if it doesn’t have one.

expect(self: Entity, prototype: <T>) <T>

Expects a component, returning it or erroring if the entity does not have the component.

Parameters:

prototype (<T>) – The type of the component to return.