Entities API
A mod registers a class with entities.add and creates an instance of the class with entities.spawn. A class is a table of components, keyed by the component name. The engine owns the components and their fields. See Components.
entities.spawn, entities.patch and entities.despawn need an active world. Outside a callback or a server tick there is no active world, and the call raises a Lua error. entities.add, entities.get and entities.set_player do not need a world.
Constants
| Value | Description |
|---|---|
entities.NULL_CLASS | The ID of no class. entities.get returns this value when it cannot resolve a name |
Function: entities.add(name, def) -> integer
Registers a class and returns its ID. The class belongs to the mod that registers it. Another mod can spawn the class after this mod finishes loading.
Arguments
nameis the namespaced class IDdefis a table of components. Each key is a component name from Components. Each value is the definition config of that component
Return value
- The class ID
Notes
- If another class already holds the name, the engine appends
~Nto it. The engine also logs a warning. See namespaced identifiers. - A malformed name, an unknown component or a config that the component rejects raises a Lua error and fails the mod. A config that is not a table does the same.
Function: entities.get(name) -> integer
Resolves a class name to an ID. The engine looks for a class of this mod first, then in the registry of every loaded mod.
Arguments
nameis the namespaced class ID
Return value
- The class ID
entities.NULL_CLASSif the name has an invalid form or no class holds it
Function: entities.spawn(name, kv) -> integer
Creates an instance of a class in the active world and returns its handle. The engine attaches every component with its definition config, then it applies kv.
Arguments
nameis the namespaced class IDkvis an optional table of component fields for the new entity. Each key is a component name and each value is a patch config from Components. The engine ignores a key for a component that the class does not have
Return value
- The entity handle, as an integer. The handle is valid only in the current world
Notes
- A malformed name, an unknown class or a config that the component rejects raises a Lua error.
Function: entities.patch(entity, component, kv) -> boolean[, string]
Assigns fields of one component of an entity.
Arguments
entityis the entity handlecomponentis the component namekvis the patch config
Return value
trueif the component took the configfalseif the handle is not valid in the active worldfalseand a message if the component rejects the config
Notes
- An unknown component name, a handle that is not an integer or a config that is not a table raises a Lua error.
Function: entities.despawn(entity) -> boolean
Destroys the entity.
Return value
trueif the engine destroyed the entityfalseif the handle is not valid in the active world
Function: entities.set_player(name)
Marks the class that the engine spawns when a player joins the world.
Arguments
nameis the namespaced class ID
Return value
- No value
Notes
- Call this function during init. The engine resolves the class once, after every mod loads. If no such class exists at that point, the start fails.
- A second call replaces the class of the first call and logs a warning.
Components
| Component | Definition config | Patch config |
|---|---|---|
transform | {} | { bpos = {...}, angs = {...} } |
head | { x, y, z } | { x, y, z } |
collision | { step_height, width, height } or { step_height, min, max } | { step_height, width, height } or { step_height, min, max } |
velocity | {} | { x, y, z } |
move_mode | { value = "WALK" } | { value = "WALK" } |
player | {} | none |
A component that the engine does not know is an error at entities.add. The engine has these six components.
transform
Holds the position and the facing of the entity. Both patch fields are optional and keep the current value when they are absent.
bposis the world block position, as three integersangsis the facing, as three numbers in radians
head
Holds the head of the entity.
- The definition config is the offset of the head from the entity origin, as three numbers in blocks. The default is
{ 0, 0, 0 }. The built-in player uses{ 0, 1.62, 0 } - The patch config is the look angles, as three numbers in radians.
entities.patchdoes not clamp them. The client clamps the look pitch of a player to 90 degrees
collision
Holds the collision box of the entity and the height that it steps over.
- The definition config requires
step_height, a number in blocks. The value must not be negative - The config also needs a box. Use
minandmaxfor an explicit box, orwidthandheightfor a box at the entity origin. Give both fields of one pair - A
widthandheightbox is centered on the entity origin on X and Z. It starts at the feet and reachesheightblocks up - The patch config accepts
step_height,minandmax, orwidthandheight. Absent fields keep their current value - The built-in player uses
{ width = 0.6, height = 1.8, step_height = 0.5 } - A class in
WALKmode needs this component. Without it,pmovelogs a warning and does not move the entity.
velocity
Holds the velocity of the entity in blocks per second. Only the pmove system moves an entity, and only when the entity carries player. A FLY entity adds velocity * frametime to its position and zeroes a velocity below the epsilon. A WALK entity feeds the same product through the collision shapes, so it needs a collision component, and its velocity is rewritten after the move.
- The definition config is empty
move_mode
Holds the movement rules that pmove uses for the entity.
valueis"WALK"or"FLY". The default is"WALK". A patch with any other value fails
player
Marks the entity as a player. It holds no fields.
- The definition config is empty
entities.patchaccepts an empty config and returnstrue