Register trigger handling||doc135694

September 20, 2022

Format

 Trigger.RegisterHandler(cfg, name, handler)
  • The class to which the function belongs:Trigger

Description

Register the processing callback function (override) that triggers the event, bind the function to the event name, and execute the bound processing callback function when the event is triggered. The first parameter of the processing function is the context information when the event is triggered.context


How to register a global handler function

with global configurationWorld.cfgAs the configuration information of the registered trigger handler function is passed in, the handler function of the global trigger can be registered

Trigger.RegisterHandler(World.cfg, "BLOCK_TRIGGER", function(context)
    print("Global trigger !")
end)

Precautions

  • Triggers with the same name are registered with different configuration information. If a global trigger is registered, the same global trigger will be triggered when it is triggered.
  • RegitsterHandler can only bind one handler function to the event name at a time. When trying to register multiple functions, only the last one will be bound.

Parameters

Variable name type Description
cfg config The configuration information of the object that needs to trigger the event
name string event name
handler function trigger callback function


Code Samples

Register a trigger named "BLOCK_TRIGGER" in the server script

local blcokCfg = Block:GetNameCfg("myplugin/grass")
Trigger.RegisterHandler(blcokCfg, "BLOCK_TRIGGER", function(context)
    print("Block trigger !")
end)