Skip to content

Module Entities

local Entities = require "system.game.Entities"

Classes

Entity

Type Field Description
Entity.ID id
Entity.Type name

ECS.TypeMetaData

Type Field Description
Entity.ComponentType[] components List of component names for this entity type
Entity.ComponentType[] mutableComponents List of component names for this entity type with at least one mutable field
table setters Maps component name => field name => setter function
table fieldIDs Maps component name => field name => field ID
Entity.ComponentType[] layoutComponents Maps field ID => component name
string[] layoutFields Maps field ID => field name
string initialValues Serialized table containing all default values for this entity type

Functions

Entities.hasAllComponents(entity, components)

Entities.isValidEntityType(typeName): boolean

Returns true if a type exists with the given name, false otherwise

Entities.isValidComponent(componentName): boolean

Returns true if a component exists with the given name, false otherwise

Entities.listComponentNames(): Entity.ComponentType

Lists all registered component names

Entities.getComponentDefault(componentName, fieldName): any

Lists all registered component names

Entities.getEntityTypeName(entity): Entity.Type

Accepts an entity or type name, returns a type name (like entity.name, but also safe on strings)

Entities.getEntityTypeID(entityID): Entity.Type

Deprecated. Use ecs.getEntityByID(entityID).name. @deprecated

Entities.resolveEntityType(typeName): Entity.Type

Deprecated. Does nothing.

Entities.entityExists(entityID): boolean

Returns true if there is an entity with the given ID, false otherwise.

Entities.getEntityByID(entityID): Entity?

Returns the entity with the given ID. For compatibility, this also accepts entities, returning them unchanged.

Entities.getEntitiesByType(typeName)

Returns a list of entities of the given type.

Entities.getEntityPrototype(typeName): Entity

Returns the prototype (an immutable entity with id 0) for the given type.

Entities.getEntityTypesWithComponents(componentList): Entity.Type[]

Returns a list of types that have all of the given components.

Entities.typesWithComponents(componentList): integer

Iterates over types that have all of the given components.

Entities.entitiesWithComponents(componentNames): table

Iterates over entities that have all of the given components.

Entities.prototypesWithComponents(componentList): integer

Iterates over prototypes of types that have all of the given components.

Entities.spawn(typeName): Entity

Creates and initializes an entity of the given type. Returns the newly created entity.

Entities.duplicate(entityID): Entity

Creates a duplicate of the specified entity and returns a wrapper to it.

Entities.despawn(entityID)

Deletes the entity with the given ID.

Entities.resetAll()

Deletes all entities, and fully reset ECS state.

Entities.convert(entity, targetTypeName): Entity

Converts the given entity to the given type. Components are added and removed to match the target type. For components that exist on both the source and target types, mutable fields keep their current values, while constant fields are reset to the target type’s default.

Entities.typeHasComponent(typeName, componentName): boolean

Returns true if the given type has a component with the given name. Returns nil for invalid types, and false for valid types that don’t have the component.

Entities.defragmentEntityIDs()

Entities.serializeState(): EntitySnapshot

Creates a snapshot of the whole entity system's current state

Entities.deserializeState(serializedState)

Restores a snapshot of the whole entity system's current state

Entities.makeSchemaIndependentSnapshot(serializedState)