Entities
Last updated
Last updated
Your game is made up of entities each of which have an associated UUID, position, type and data.
This section details the fields and methods available on each entity object.
Fields
Field | Type | Description |
---|---|---|
Methods
Method | Parameters | Returns | Description |
---|---|---|---|
Entities are divided into types, each of which have their own behaviour. This behaviour is determined by a script in the entity
folder; to create a new entity type, simply create a new file in this folder, call the file entitytypename.lua
and add the stub code shown below:
These three functions, init
, update
and message
are explained in detail later. There must always be an entity type called player
which acts as the abstraction for players within the simulation.
These three functions, init
, update
and message
, govern the behaviour of all entities of this type.
Init
This is called when the entity is created and should be used to set initial state (for example, setting the Chunkloader or Data fields).
Update
This is called each tick to update the entity, dt is the time since the last update in seconds.
Message
This is called when another entity sends a message to this entity (note that the entity sending the message might not be in the same chunk). The message is of type message (see types below).
On the player entity this function also acts as the handler for messages received from a game client, which follow the same format.
The msg argument supplied to this function is defined as follows:
A number of API calls are available for certain entity-related actions such as spawning entities, they are accessed by interfacing with the api.entity
object in the global scope within all server-side scripts.
For example, sending a message to a particular entity each tick would look like:
Below is the example cat.lua script which exists in the default template repository. The comments provide further information on what each line is doing.
Field | Type | Description |
---|---|---|
Method | Parameters | Return Value | Description |
---|---|---|---|
ID
string
UUID of the entity. Read-only.
Type
string
Type of the entity. Read-only.
Data
table
Custom lua table which can be used to store arbitrary data about the entity.
Chunkloader
bool
If true, the chunk containing this entity will remain loaded even if no players are present. Note that chunkloader entities are a premium feature.
Transient
bool
If true, when the chunk containing this entity is unloaded, it will not be persisted.
Move(dx, dy, dz)
dx: float
dy: float
dz: float
None
Move the entity relative to its current position.
MoveTo(x, y, z)
x: float
y: float
z: float
None
Move the entity relative to the origin (teleport).
GetPosition()
None
x: float
y: float
z: float
coordinates of the entity.
Get the current position of the entity.
Remove()
None
None
Delete this entity.
GetNearbyEntities(dist)
dist: float
Entity[]
Get entities within the specified distance. Please note that the maximum distance that can be reliably fetched is the game's chunk size, this is because GetNearbyEntities only searches 9 chunks centred on the entity's current chunk.
Warp(dimension, [x, y, z])
dimension: string
x: float
y: float
z: float
None
Transport this entity to (x,y,z) in the dimension specified. The coordinate fields are optional and default to (0,0,0) if not specified. Specifying just two coordinates will also work, for example, if your game is 2D or otherwise where 0 is the desired z-axis value.
Data
table
Contains the content of the message.
Client
bool
Indicates if the message is from a game client (true) or from another entity on a server (false).
Create
type: string
x: float
y: float
z: float
data: table
Entity
Creates a new entity in the current dimension and returns it. This function will automatically call the init
function for the specified entity type. It fails if there is no entity type that matches the provided type
.
Message
entityid: string
message: table
None
Sends a message to the entity with UUID entityid
if it exists, this entity can be anywhere in the world.