RegisterCustomEvent||doc136852

September 20, 2022

Format

Event:RegisterCustomEvent(eventName)

Describe

Register user-defined global events, which can be used for events defined by developers themselves or to transfer information between different scripts. Because the registered event may have the same name as the engine event, it is best to add a namespace distinction when obtaining custom events. For example, if you want to register the "onplayerlogin" event, and the engine also has this event with the same name, then it is best to use getevent ("testevent", "anykey") to obtain events in a custom space. If you do not add a namespace, the default is to obtain engine events.

parameter

Variable name Type Describe
eventName string Event name

Code example

Script 1 binds the global custom event for players to purchase items.

Event:RegisterCustomEvent("OnPlayerBuyItem")
local bindableEvent = Event:GetEvent("OnPlayerBuyItem")
bindableEvent:Bind(function(player, itemName)
    print("OnPlayerBuyItem", player.name, itemName)
end)

Script 2 fires a global custom event for players to purchase items

local player1 = {
    name = "zzy",
    id = 1
}
bindableEvent:Emit(player1, "testItem")