Defold
Last updated
Last updated
The Defold SDK integrates our multiplayer platform with Defold. The plugin provides components and messages to use within your game client code - these communicate changes in state from your server side simulation to the game client itself.
If you are new here, we recommend starting with the .
The Defold SDK can be installed by navigating to your game.project
file, then in the 'Project' tab (in the Editor window) you will see the 'Dependencies' list.
You need to add the following dependencies:
: https://github.com/Planetary-Processing/defold-sdk/archive/master.zip
, which we depend on: https://github.com/defold/extension-websocket/archive/master.zip
, which we depend on: https://github.com/Melsoft-Games/defold-protobuf/archive/master.zip
You need then to fetch dependencies, this is done with the 'Fetch Libraries' button under 'Project' in the topbar.
If these settings are missing, edit your game.project
file in a text editor and add the options to it manually.
Player URL - Your local player will be represented by a GameObject at this URL.
Chunks are static and invisible. A chunk object will spawn at the origin of each loaded chunk. Chunk objects are optional and you can create a game without them if you wish.
There is 2KB limit on the size of messages in Defold, which affects the amount of data which can be transferred from the server. If your message buffer is being overloaded, reduce the amount of Data stored in each individual entity.
If you are using authentication, you should supply username and password in a table like so:
By default, the player will not have joined the world, in order to do this, you should send an empty message with ID hash("pp_join")
to the master component (or its GameObject).
uuid
string
UUID of the entity.
x
float
X coordinate in world units.
y
float
Y coordinate in world units.
z
float
Z coordinate in world units.
data
table
Data of the entity.
type
string
Type of the entity.
The example below shows an update message being handled by printing out all the key-value pairs in the message's data table.
Below are reference tables of the API available within Defold's environment.
Messages sent to component scripts are relayed directly to the game server.
Hashes from the server are received like so:
pp_init
table({
username: string
password: string
)}
Connect to and authenticate with the Planetary Processing servers. Use an empty table for Anonymous Auth.
pp_join
None.
Spawn your player into the world.
pp_message
table
Send a message to your server-side player script.
pp_disconnect
None.
Disconnect from PP's servers.
Received by component:
pp_update
table({
uuid: string
x: float
y: float
z: float
data: table
type: string
)}
An update message containing changes to this entity's server-side state.
Received by component:
pp_update
table({
uuid: string
x: float
y: float
z: float
data: table
type: string
)}
An update message each tick containing changes to this chunk's server-side state.
Received by script, which called pp_init
:
pp_update
table({
uuid: string
x: float
y: float
z: float
data: table
type: string
)}
A message containing changes to all entities' and chunks' server-side states.
pp_spawn
table({
uuid: string
x: float
y: float
z: float
data: table
type: string
)}
A message when a new entity or chunk spawns.
pp_delete
table({
uuid: string
x: float
y: float
z: float
data: table
type: string
)}
A message when an entity or chunk is removed.
pp_connected
The connected player's uuid
.
A message when the client successfully connects and authenticates.
pp_disconnected
string
A message when the client disconnects from PP's servers.
pp_connection_error
string
A message when there is an error during initial connection or authentication.
Received by script, assigned in Master Component Properties Panel:
pp_server_to_client
table
A message manually sent from the server, to this specific player client. Must have its URL assigned in the Master Component Properties Panel.
The Defold Websocket library has additional . Check there is a 'Websocket' tab in your game.project file, with the 'Debug' and 'Socket Timeout' settings.
You can find an example project in the same repo as the library, on our .
The Defold SDK provides three new components which can be added to GameObjects. These are , and .
The Master Component represents the main connection point to PP's servers and performs the heavy lifting and orchestration of the SDK. You must have a GameObject in your game which has the .
Factories to produce and must be attached to the same GameObject as the Master Component. Entity factories should be named after the in the format typenamefactory
as in the example below for the types cat
, player
and tree
.
Within the Master Component you will need to set the URL of the local player GameObject, the , and the Planetary Processing .
Game ID - The ID of your game from the , for Defold to connect to.
Chunk Size - The size of chunks in your game, defined in the game settings.
Server To Client URL- A GameObject or script at this URL, which receives manual messages from the game .
For every you wish to display in the Defold client, you must create a GameObject file containing the Entity Component. These will act as protypes. These Entity prototypes will be spawned into your world, by factories attached to the Game Object with the .
Other players in the game are also represented as Entities, using the player
. The local player, controlled by the person playing your game, is not a prototype. It should instead be a separate GameObject in your collection, however it also needs an .
By default, entities will be moved to their server-side position, you can disable this per entity type by un-ticking 'Use Server Position' in the entity component's config window. Should you not want this, you can still keep track of the server position using .
Like the entity component, if you wish to represent chunk data in the Defold client, you must create a GameObject file containing the . This will act as a protype. The chunk prototype will be spawned into your world as an invisible object, by a factory attached to the Game Object with the .
Messages can be sent to and from your game server along a connection,. Messages to the server are sent to the . Messages from the server are sent to each entity, and the (the script which sent the message). Each of these message use a message_id, such as , , , or .
The first thing you need to do when your game starts is initialise the SDK, to do this you send the (or its whole GameObject) a message with ID hash("pp_init")
. The sender url of this message will be allocated as the '' and will be sent all updates in future.
The message will fail if it is called too soon after the and before the connection is fully established. To avoid this , either trigger the once a message has been received, or on input from the person playing.
The Planetary Processing SDK sends and receives several different messages and accepts several too. Each time an or updates its position or data, a message with ID hash("pp_update")
is sent to that entity's GameObject and the .
This message is of the following format: (the same format is also used for and messages)
If you wish to send a message to the server, it must be sent to the (or its whole GameObject). It will be received by the player entity (player.lua) on the server. All messages are handled by the player entity's function on the server side API. Messages from the client can be identified by the . To use, send a message with ID hash("pp_message")
to the where the is the Lua table to be sent as a message.
Messages can be manually sent to a specific client from the game server using . These message can be received by a designated . This GameObject needs a custom script, which receives the server message to its on_message()
function with the hash .
The , , and components each receive various message hashes, which can be sent from scripts by posting to the component object like so: