|
本帖最后由 懒动行不行 于 2023-7-10 10:08 AM 编辑
EventRegistry
来源: SharedXML\GlobalCallbackRegistry.lua
回调事件事件注册Details- 需要注意的是,如果通过事件注册表注册的某个函数执行时间过长(脚本超时),则注册表中同一事件的所有其他回调也可能无法执行。
- EventRegistry事件及其有效载荷在触发时显示在 Event Trace 面板中。
例子
常见的使用案例是注册暴雪回调事件, 例如当Mount Journa(坐骑日志)打开时( opened)
[Lua] 纯文本查看 复制代码 EventRegistry:RegisterCallback("MountJournal.OnShow", function()
print("showed the mount journal")
end)
注册对自定义事件的两个回调.
[Lua] 纯文本查看 复制代码 local function a(ownerID, ...)
print("a", ...) -- prints "a", "foo", "bar"
end
local function b(ownerID, ...)
print("b", ...) -- prints "b", "foo", "bar"
end
EventRegistry:RegisterCallback("HelloWorld.OnSomething", a)
EventRegistry:RegisterCallback("HelloWorld.OnSomething", b)
-- /run EventRegistry:TriggerEvent("HelloWorld.OnSomething", "foo", "bar")
注销
回调由所有者句柄注销。注册回调时,所有者可以是表、函数或字符串;如果省略,它将被分配一个内部编号。 [1]
[Lua] 纯文本查看 复制代码 local cb = function(ownerID, journal, tabID)
print("switched to collections tab", tabID)
end
local ownerID = EventRegistry:RegisterCallback("CollectionsJournal.TabSet", cb) -- omitted owner param
Example = ownerID
-- /run EventRegistry:UnregisterCallback("CollectionsJournal.TabSet", Example)
框架事件 这允许您响应事件,而无需创建框架并向其注册事件。
[Lua] 纯文本查看 复制代码 EventRegistry:RegisterFrameEventAndCallback("PLAYER_ENTERING_WORLD", function(ownerID, ...)
print(...) -- on login this prints: "foo", "bar", true, false
end, nil, "foo", "bar")
私有回调注册
创建您自己的CallbackRegistry对象,而不是使用EventRegistry。
[Lua] 纯文本查看 复制代码 local cr = CreateFromMixins(CallbackRegistryMixin)
cr:OnLoad()
cr:SetUndefinedEventsAllowed(true)
cr:RegisterCallback("MyCustomEvent", function(ownerID, ...)
print("a", ...) -- prints "a", "foo", "bar"
end)
Example = cr
-- /run Example:TriggerEvent("MyCustomEvent", "foo", "bar")
外链
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?加入我们
x
|