Welcome!

You are just one step away from creating awesome games!

You need a Google account to use Defold

HOW TO SUBMIT

To submit your game, simply log in to your Defold dashboard, click “submit your entry”, and follow the instructions!

HOW TO SUBMIT

Sorry, you can't submit your game from a phone. To submit your game, you need to log in to Defold on a desktop.

Select document

Messaging API documentation

version 1.2.98

Functions for passing messages and constructing URL objects.

Functions

msg.post

msg.post(receiver, message_id, [message])

Post a message to a receiving URL. The most common case is to send messages to a component. If the component part of the receiver is omitted, the message is broadcast to all components in the game object.

The following receiver shorthands are available:

  • "." the current game object
  • "#" the current component
  • nil the current component

There is a 2 kilobyte limit to the message parameter table size.

Parameters

receiver

string | url | hash | nil The receiver must be a string in URL-format, a URL object, a hashed string or nil.

message_id

string | hash The id must be a string or a hashed string.

[message]

table a lua table with message parameters to send.

receiver

string | url | hash | nil The receiver must be a string in URL-format, a URL object, a hashed string or nil.

message_id

string | hash The id must be a string or a hashed string.

[message]

table a lua table with message parameters to send.

Examples

Send "enable" to the sprite "my_sprite" in "my_gameobject":

msg.post("my_gameobject#my_sprite", "enable")

Send a "my_message" to an url with some additional data:

local params = {my_parameter = "my_value"}
msg.post(my_url, "my_message", params)

msg.url

msg.url()

This is equivalent to msg.url(nil) or msg.url("#"), which creates an url to the current script component.

Returns

url

url a new URL

url

url a new URL

Examples

Create a new URL which will address the current script:

local my_url = msg.url()
print(my_url) --> url: [current_collection:/my_instance#my_component]

msg.url

msg.url(urlstring)

The format of the string must be [socket:][path][#fragment], which is similar to a HTTP URL. When addressing instances:

  • socket is the name of a valid world (a collection)
  • path is the id of the instance, which can either be relative the instance of the calling script or global
  • fragment would be the id of the desired component

In addition, the following shorthands are available:

  • "." the current game object
  • "#" the current component
  • nil the current component

Parameters

urlstring

string string to create the url from

urlstring

string string to create the url from

Returns

url

url a new URL

url

url a new URL

Examples

local my_url = msg.url("#my_component")
print(my_url) --> url: [current_collection:/my_instance#my_component]

local my_url = msg.url("my_collection:/my_sub_collection/my_instance#my_component")
print(my_url) --> url: [my_collection:/my_sub_collection/my_instance#my_component]

local my_url = msg.url("my_socket:")
print(my_url) --> url: [my_collection:]

msg.url

msg.url([socket], [path], [fragment])

Parameters

[socket]

string | number socket of the URL

[path]

string | hash path of the URL

[fragment]

string | hash fragment of the URL

[socket]

string | number socket of the URL

[path]

string | hash path of the URL

[fragment]

string | hash fragment of the URL

Returns

url

url a new URL

url

url a new URL

Examples

local my_socket = "main" -- specify by valid name
local my_path = hash("/my_collection/my_gameobject") -- specify as string or hash
local my_fragment = "component" -- specify as string or hash
local my_url = msg.url(my_socket, my_path, my_fragment)

print(my_url) --> url: [main:/my_collection/my_gameobject#component]
print(my_url.socket) --> 786443 (internal numeric value)
print(my_url.path) --> hash: [/my_collection/my_gameobject]
print(my_url.fragment) --> hash: [component]

I want to report anonymously.