Receiving Messages - Silverfeelin/Starbound-MessageHandling-Demo GitHub Wiki
To handle incoming messages, a new message handler should be set using message.setHandler
. This will bind a message to a function, which will fire when the message is received.
Once the function returns a value, it will be sent back to the caller automatically.
The function takes 2 arguments:
message.setHandler(messageName, handler)
- messageName
The name of the message to listen to. When an entity message arrives matching this name, the message handler will run.
- handler
The function that handles the message. When an entity message matching messageName
is received, this function is called.
The requirements of the message handler can be found on the Message Handler page.
You can use an existing function, or declare one inside the message.setHandler
call.
Example
function init()
message.setHandler("foo", someFunction)
message.setHandler("alt", someFunction)
message.setHandler("bar", function(msg, isLocal)
sb.logInfo("Received %s message '%s'.", isLocal and "local" or "remote", msg)
end)
end
function someFunction(msg, isLocal)
sb.logInfo("Received message '%s' in 'someFunction'.", msg)
end