The CustomEvent system provides two distinct ways to handle events: Receipt (Callback) Mode and Notification Mode.
Receipt Mode (event_dispatch)
Allows for logic insertion and returns values. When multiple callbacks are registered, they are called in order. If any callback returns a non-nil value, subsequent triggers are skipped.
Notification Mode (event_notify)
A standard notification. If an event is triggered while another is executing, the new event is queued.
Registering Events (event_on)
You can register callbacks. You can optionally specify an expected argument pattern to ensure the event only triggers when arguments match.
Example: Receipt Mode
Obj:event_on('获取', function (trigger,...)
return 1
end)
Obj:event_on('获取', function (trigger,...)
print('This won't run')
end)
local result = Obj:event_dispatch('获取') -- result will be 1
Example: Notification Mode
Obj:event_on('获得', function ()
print('Triggered')
Obj:event_notify('移除') -- This will be queued if '获得' is still processing
end)
Obj:event_notify('获得')
Example: Argument Filtering
-- Only triggers if the first argument is '123'
Obj:event_on('输入', {'123'}, function (trigger, ...)
print('Matched!')
end)
Obj:event_notify('输入', 1) -- Does NOT trigger
Obj:event_notify_with_args('输入', {'123'}, 2) -- Triggers
Obj:event_on('input', function(trg, ...) print('Event fired') end)
Obj:event_notify('input')