feat: reloading hud scripts

This commit is contained in:
MihailRis 2025-03-15 21:12:19 +03:00
parent f9998f0a93
commit 6fb14ee0a4
5 changed files with 36 additions and 14 deletions

View File

@ -173,6 +173,8 @@ function run_current_file()
func = function() item.reload_script(unit) end func = function() item.reload_script(unit) end
elseif script_type == "world" then elseif script_type == "world" then
func = function() world.reload_script(unit) end func = function() world.reload_script(unit) end
elseif script_type == "hud" then
func = function() hud.reload_script(unit) end
end end
local output = core.capture_output(func) local output = core.capture_output(func)
document.output:add( document.output:add(
@ -392,6 +394,7 @@ local function build_scripts_classification()
local packs = pack.get_installed() local packs = pack.get_installed()
for _, packid in ipairs(packs) do for _, packid in ipairs(packs) do
scripts_classification[packid..":scripts/world.lua"] = {"world", packid} scripts_classification[packid..":scripts/world.lua"] = {"world", packid}
scripts_classification[packid..":scripts/hud.lua"] = {"hud", packid}
end end
end end

View File

@ -33,7 +33,8 @@
"gui/module", "gui/module",
"gui/play", "gui/play",
"gui/info", "gui/info",
"gui/world" "gui/world",
"gui/hud"
], ],
"fonts": [ "fonts": [
{ {

BIN
res/textures/gui/hud.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

View File

@ -171,6 +171,23 @@ static int l_set_allow_pause(lua::State* L) {
return 0; 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[] = { const luaL_Reg hudlib[] = {
{"open_inventory", wrap_hud<l_open_inventory>}, {"open_inventory", wrap_hud<l_open_inventory>},
{"close_inventory", wrap_hud<l_close_inventory>}, {"close_inventory", wrap_hud<l_close_inventory>},
@ -189,5 +206,6 @@ const luaL_Reg hudlib[] = {
{"_set_content_access", wrap_hud<l_set_content_access>}, {"_set_content_access", wrap_hud<l_set_content_access>},
{"_set_debug_cheats", wrap_hud<l_set_debug_cheats>}, {"_set_debug_cheats", wrap_hud<l_set_debug_cheats>},
{"set_allow_pause", wrap_hud<l_set_allow_pause>}, {"set_allow_pause", wrap_hud<l_set_allow_pause>},
{"reload_script", wrap_hud<l_reload_script>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -806,21 +806,21 @@ bool scripting::register_event(
if (lua::pushenv(L, env) == 0) { if (lua::pushenv(L, env) == 0) {
lua::pushglobals(L); lua::pushglobals(L);
} }
if (lua::getfield(L, name)) { bool success = true;
lua::pop(L);
lua::getglobal(L, "events"); lua::getglobal(L, "events");
lua::getfield(L, "reset"); lua::getfield(L, "reset");
lua::pushstring(L, id); lua::pushstring(L, id);
lua::getfield(L, name, -4); if (!lua::getfield(L, name, -4)) {
success = false;
lua::pushnil(L);
}
lua::call_nothrow(L, 2); lua::call_nothrow(L, 2);
lua::pop(L); lua::pop(L);
// remove previous name // remove previous name
lua::pushnil(L); lua::pushnil(L);
lua::setfield(L, name); lua::setfield(L, name);
return true; return success;
}
return false;
} }
int scripting::get_values_on_stack() { int scripting::get_values_on_stack() {