Skip to reading
Lua Scripting

Send a custom event

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
On this page

LESSON 13

Connect two records without making the sender know which record handles a clue.

Sender · bell_sender#

Code example
local Script = {}
Code example
function Script.run(context)
    await(EmitEvent("OnCustomEvent", {
        topic = "archive_bell", strikes = 3
    }))
    return true
end
Code example
return Script

Receiver · bell_listener#

Code example
local Script = {}
Code example
function Script.on_custom_event(event)
    if tostring(event.topic) == "archive_bell" then
        Log("Archive bell rang " .. tostring(event.strikes) .. " times.")
    end
end
Code example
return Script

Check OnCustomEvent on bell_listener, then apply both records. Run bell_sender. The log should

show three strikes. Subscribe the receiver only. Emitting the same event again from its own

unguarded handler would cause repeated dispatch.

The dispatcher matches the emitted name. Use OnCustomEvent for this checkbox and carry the

gameplay name in topic. The context also carries eventName. A custom field such as strikes

belongs to your own protocol.

Page 31 of 133Permalink
Go to page