Entities
Last updated
Last updated
Your game is made up of entities each of which have an associated , , and . These fields can be edited and used alongside to create gameplay.
The msg
argument supplied to this function is defined as follows:
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).
For example, sending a message to a particular entity each tick would look like:
And be received like this:
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.
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.
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 (Premium Only)
bool
If true, the chunk containing this entity will remain loaded even if no players are present.
Transient
bool
If true, when the chunk containing this entity is unloaded, it will not be persisted.
Each of these methods takes the entity object self
as its first parameter. Hence you may use self:Move(dx, dy, dz)
instead of self.Move(self, dx, dy, dz)
.
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.
Init
self: Entity
None
A necessary function, to initialise an entity in its default state.
Update
self: Entity
dt: float
None
A necessary function, to process an entity's behaviour each tick.
Message
self: Entity
msg: table({
Data: table
Client: bool
)}
None
A necessary function, to receive messages to an entity.
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 entity_type_name.lua
and add the stub code shown below:
There must always be an called player
which acts as the abstraction for both the player client and other players within the simulation.
These three functions , , and govern the behaviour of all entities of this type. Each must be referenced and returned in a table at the end of the file.
The function is called when the entity is created and should be used to set initial state (for example, setting the or fields).
The function is called each tick to update the , for movement and repeating processes. The parameter is the time since the last in seconds.
The function receives messages sent to this entity and them. Messages can come from or the game client.
All messages sent from the game client are sent to the player
entity, and received by the function in player.lua
. If a message is sent from the client, its message's field will be true.
Messages are sent using either or by the . They are received by an entity's function.
Messages sent from the client will always be received by the corresponding player entity, in the function of the player.lua
file.
Entities won't load game world , by default. If an entity tries to move into an unloaded chunk, it will delay processing until the chunk is . Players and entities are not required to wait and will automatically load the required chunk.
Entities can access the data for the chunk they are in, using the variable.
type: string
x: float
y: float
z: float
data: table
Entity
Creates a new entity in the current dimension and returns it.
entityid: string
message: table
None
Sends a message to the entity with UUID entityid.