diff --git a/doc/en/scripting/events.md b/doc/en/scripting/events.md index 0d3f83e4..10b9e3f1 100644 --- a/doc/en/scripting/events.md +++ b/doc/en/scripting/events.md @@ -145,6 +145,12 @@ events.on(code: str, handler: function) Adds an event handler by its code, not limited to the standard ones. +```lua +events.reset(code: str, [optional] handler: function) +``` + +Removes the event, adding a handler if specified. + ```lua events.emit(code: str, args...) -> bool ``` diff --git a/doc/ru/scripting/events.md b/doc/ru/scripting/events.md index cf61343f..d7805d60 100644 --- a/doc/ru/scripting/events.md +++ b/doc/ru/scripting/events.md @@ -144,6 +144,12 @@ events.on(code: str, handler: function) Добавляет обработчик события по его коду, не ограничиваясь стандартными. +```lua +events.reset(code: str, [опционально] handler: function) +``` + +Удаляет событие, добавляя обработчик, если указан. + ```lua events.emit(code: str, args...) -> bool ``` diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index fb2c7702..8a4f3513 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -23,6 +23,14 @@ function events.on(event, func) table.insert(events.handlers[event], func) end +function events.reset(event, func) + if func == nil then + events.handlers[event] = nil + else + events.handlers[event] = {func} + end +end + function events.remove_by_prefix(prefix) for name, handlers in pairs(events.handlers) do local actualname = name diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index c437a6ae..70a397b4 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -616,7 +616,7 @@ bool scripting::register_event( if (lua::getfield(L, name)) { lua::pop(L); lua::getglobal(L, "events"); - lua::getfield(L, "on"); + lua::getfield(L, "reset"); lua::pushstring(L, id); lua::getfield(L, name, -4); lua::call_nothrow(L, 2); @@ -706,6 +706,7 @@ void scripting::load_layout_script( uidocscript& script ) { int env = *senv; + lua::pop(lua::get_main_state(), load_script(env, "layout", file)); script.onopen = register_event(env, "on_open", prefix + ".open"); script.onprogress =