diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index eabf6dea..a9867e0b 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -173,6 +173,8 @@ function run_current_file() func = function() item.reload_script(unit) end elseif script_type == "world" then func = function() world.reload_script(unit) end + elseif script_type == "hud" then + func = function() hud.reload_script(unit) end end local output = core.capture_output(func) document.output:add( @@ -392,6 +394,7 @@ local function build_scripts_classification() local packs = pack.get_installed() for _, packid in ipairs(packs) do scripts_classification[packid..":scripts/world.lua"] = {"world", packid} + scripts_classification[packid..":scripts/hud.lua"] = {"hud", packid} end end diff --git a/res/preload.json b/res/preload.json index 5d9bdde5..394f0429 100644 --- a/res/preload.json +++ b/res/preload.json @@ -33,7 +33,8 @@ "gui/module", "gui/play", "gui/info", - "gui/world" + "gui/world", + "gui/hud" ], "fonts": [ { diff --git a/res/textures/gui/hud.png b/res/textures/gui/hud.png new file mode 100644 index 00000000..4d32d616 Binary files /dev/null and b/res/textures/gui/hud.png differ diff --git a/src/logic/scripting/lua/libs/libhud.cpp b/src/logic/scripting/lua/libs/libhud.cpp index 1bdcee12..2d1af84f 100644 --- a/src/logic/scripting/lua/libs/libhud.cpp +++ b/src/logic/scripting/lua/libs/libhud.cpp @@ -171,6 +171,23 @@ static int l_set_allow_pause(lua::State* L) { return 0; } +static int l_reload_script(lua::State* L) { + auto packid = lua::require_string(L, 1); + if (content == nullptr) { + throw std::runtime_error("content is not initialized"); + } + auto& writeableContent = *engine->getWriteableContent(); + auto pack = writeableContent.getPackRuntime(packid); + const auto& info = pack->getInfo(); + scripting::load_hud_script( + pack->getEnvironment(), + packid, + info.folder / "scripts/hud.lua", + pack->getId() + ":scripts/hud.lua" + ); + return 0; +} + const luaL_Reg hudlib[] = { {"open_inventory", wrap_hud}, {"close_inventory", wrap_hud}, @@ -189,5 +206,6 @@ const luaL_Reg hudlib[] = { {"_set_content_access", wrap_hud}, {"_set_debug_cheats", wrap_hud}, {"set_allow_pause", wrap_hud}, + {"reload_script", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index eabbd392..9ebd649d 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -806,21 +806,21 @@ bool scripting::register_event( if (lua::pushenv(L, env) == 0) { lua::pushglobals(L); } - if (lua::getfield(L, name)) { - lua::pop(L); - lua::getglobal(L, "events"); - lua::getfield(L, "reset"); - lua::pushstring(L, id); - lua::getfield(L, name, -4); - lua::call_nothrow(L, 2); - lua::pop(L); - - // remove previous name + bool success = true; + lua::getglobal(L, "events"); + lua::getfield(L, "reset"); + lua::pushstring(L, id); + if (!lua::getfield(L, name, -4)) { + success = false; lua::pushnil(L); - lua::setfield(L, name); - return true; } - return false; + lua::call_nothrow(L, 2); + lua::pop(L); + + // remove previous name + lua::pushnil(L); + lua::setfield(L, name); + return success; } int scripting::get_values_on_stack() {