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.

relations: table<Relation, table<Entity, Relation>>
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.

addRelation(
    self: Entity,
    relation: Relation,
    target: Entity,
    final?: boolean
): Entity

Adds a relation between this entity and another. Enforces cardinality, symmetry, and exclusivity rules.

Parameters:
  • relation (Relation) – The relation instance to add.

  • target (Entity) – The target entity of the relation.

  • final? (boolean) – If true this add is part of a symmetry and we shouldn’t attempt again.

Returns:

_1 (Entity) – self, for chaining.

removeRelation(self: Entity, relationType: Relation, target: Entity): Entity

Removes a relation of a given type with a specific target.

Parameters:
  • relationType (Relation) – The relation type/class (not an instance).

  • target (Entity) – The target entity of the relation.

Returns:

_1 (Entity) – self, for chaining.

removeAllRelations(self: Entity, relationType: Relation): Entity

Removes all relations with the given type.

hasRelation(self: Entity, relationType: Relation, target?: Entity): boolean

Checks whether this entity has a relation.

Parameters:
  • relationType (Relation) – The relation prototype.

  • target? (Entity) – An optional entity to check. If nil, will check if the entity has any relation with the type.

Returns:

_1 (boolean) – True if the entity has the relation.

getRelations(self: Entity, relationType: Relation): table<Entity, Relation>

Gets all relations of a given type.

Parameters:

relationType (Relation) – The relation type/class.

getRelation(self: Entity, relationType: Relation): Entity?

Gets the first/random entity with the given relation. This is mostly useful for exclusive relations.

Parameters:

relationType (Relation) – The relation prototype.

clone(self: Entity): unknown