diff --git a/src/logic/scripting/lua/libs/libhud.cpp b/src/logic/scripting/lua/libs/libhud.cpp index 1c4245ba..1bdcee12 100644 --- a/src/logic/scripting/lua/libs/libhud.cpp +++ b/src/logic/scripting/lua/libs/libhud.cpp @@ -1,9 +1,9 @@ +#include "libhud.hpp" + #include -#include #include "assets/Assets.hpp" #include "content/Content.hpp" -#include "engine/Engine.hpp" #include "frontend/UiDocument.hpp" #include "frontend/hud.hpp" #include "graphics/ui/elements/InventoryView.hpp" @@ -16,11 +16,7 @@ #include "voxels/voxel.hpp" #include "voxels/blocks_agent.hpp" #include "world/Level.hpp" -#include "api_lua.hpp" -namespace scripting { - extern Hud* hud; -} using namespace scripting; static int l_open_inventory(lua::State*) { @@ -176,22 +172,22 @@ static int l_set_allow_pause(lua::State* L) { } const luaL_Reg hudlib[] = { - {"open_inventory", lua::wrap}, - {"close_inventory", lua::wrap}, - {"open", lua::wrap}, - {"open_block", lua::wrap}, - {"open_permanent", lua::wrap}, - {"show_overlay", lua::wrap}, - {"get_block_inventory", lua::wrap}, - {"close", lua::wrap}, - {"pause", lua::wrap}, - {"resume", lua::wrap}, - {"is_paused", lua::wrap}, - {"is_inventory_open", lua::wrap}, - {"get_player", lua::wrap}, - {"_is_content_access", lua::wrap}, - {"_set_content_access", lua::wrap}, - {"_set_debug_cheats", lua::wrap}, - {"set_allow_pause", lua::wrap}, + {"open_inventory", wrap_hud}, + {"close_inventory", wrap_hud}, + {"open", wrap_hud}, + {"open_block", wrap_hud}, + {"open_permanent", wrap_hud}, + {"show_overlay", wrap_hud}, + {"get_block_inventory", wrap_hud}, + {"close", wrap_hud}, + {"pause", wrap_hud}, + {"resume", wrap_hud}, + {"is_paused", wrap_hud}, + {"is_inventory_open", wrap_hud}, + {"get_player", wrap_hud}, + {"_is_content_access", wrap_hud}, + {"_set_content_access", wrap_hud}, + {"_set_debug_cheats", wrap_hud}, + {"set_allow_pause", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libs/libhud.hpp b/src/logic/scripting/lua/libs/libhud.hpp new file mode 100644 index 00000000..74833e4b --- /dev/null +++ b/src/logic/scripting/lua/libs/libhud.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "api_lua.hpp" + +#include "logic/scripting/scripting_hud.hpp" +#include "graphics/render/WorldRenderer.hpp" +#include "engine/Engine.hpp" + +template +inline int wrap_hud(lua_State* L) { + if (scripting::hud == nullptr) { + return luaL_error( + L, "renderer is not initialized yet, see hud.lua on_hud_open event" + ); + } + return lua::wrap(L); +} diff --git a/src/logic/scripting/lua/libs/libparticles.cpp b/src/logic/scripting/lua/libs/libparticles.cpp index d3bccf76..0c468007 100644 --- a/src/logic/scripting/lua/libs/libparticles.cpp +++ b/src/logic/scripting/lua/libs/libparticles.cpp @@ -1,11 +1,8 @@ -#include "api_lua.hpp" +#include "libhud.hpp" -#include "logic/scripting/scripting_hud.hpp" -#include "graphics/render/WorldRenderer.hpp" #include "graphics/render/ParticlesRenderer.hpp" #include "graphics/render/Emitter.hpp" #include "assets/assets_util.hpp" -#include "engine/Engine.hpp" using namespace scripting; @@ -81,10 +78,10 @@ static int l_is_alive(lua::State* L) { } const luaL_Reg particleslib[] = { - {"emit", lua::wrap}, - {"stop", lua::wrap}, - {"is_alive", lua::wrap}, - {"get_origin", lua::wrap}, - {"set_origin", lua::wrap}, + {"emit", wrap_hud}, + {"stop", wrap_hud}, + {"is_alive", wrap_hud}, + {"get_origin", wrap_hud}, + {"set_origin", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libs/libtext3d.cpp b/src/logic/scripting/lua/libs/libtext3d.cpp index f334c85f..4529fc0c 100644 --- a/src/logic/scripting/lua/libs/libtext3d.cpp +++ b/src/logic/scripting/lua/libs/libtext3d.cpp @@ -1,10 +1,7 @@ -#include "api_lua.hpp" +#include "libhud.hpp" -#include "logic/scripting/scripting_hud.hpp" -#include "graphics/render/WorldRenderer.hpp" #include "graphics/render/TextsRenderer.hpp" #include "graphics/render/TextNote.hpp" -#include "engine/Engine.hpp" using namespace scripting; @@ -99,17 +96,17 @@ static int l_set_rotation(lua::State* L) { } const luaL_Reg text3dlib[] = { - {"show", lua::wrap}, - {"hide", lua::wrap}, - {"get_text", lua::wrap}, - {"set_text", lua::wrap}, - {"get_pos", lua::wrap}, - {"set_pos", lua::wrap}, - {"get_axis_x", lua::wrap}, - {"set_axis_x", lua::wrap}, - {"get_axis_y", lua::wrap}, - {"set_axis_y", lua::wrap}, - {"set_rotation", lua::wrap}, - {"update_settings", lua::wrap}, + {"show", wrap_hud}, + {"hide", wrap_hud}, + {"get_text", wrap_hud}, + {"set_text", wrap_hud}, + {"get_pos", wrap_hud}, + {"set_pos", wrap_hud}, + {"get_axis_x", wrap_hud}, + {"set_axis_x", wrap_hud}, + {"get_axis_y", wrap_hud}, + {"set_axis_y", wrap_hud}, + {"set_rotation", wrap_hud}, + {"update_settings", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index d6f618cb..478b4f9e 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -65,15 +65,22 @@ void scripting::on_frontend_render() { } void scripting::on_frontend_close() { + auto L = lua::get_main_state(); for (auto& pack : engine->getAllContentPacks()) { lua::emit_event( - lua::get_main_state(), + L, pack.id + ":.hudclose", [&](lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); } ); } + lua::pushnil(L); + lua::setglobal(L, "hud"); + lua::pushnil(L); + lua::setglobal(L, "gfx"); + + scripting::renderer = nullptr; scripting::hud = nullptr; }