From 7243ff564efda96007566273b4d8dada0e1acace Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Mon, 17 Oct 2022 14:36:31 +0100 Subject: [PATCH] store pointer to state in lua --- demo/data/rollball.lua | 3 ++- demo/demo/rollball.cpp | 2 ++ include/fggl/script/engine.hpp | 2 ++ integrations/lua/include/fggl/script/lua/engine.hpp | 3 +++ integrations/lua/src/engine.cpp | 5 +++++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/demo/data/rollball.lua b/demo/data/rollball.lua index 1221bc1..7c16c4b 100644 --- a/demo/data/rollball.lua +++ b/demo/data/rollball.lua @@ -1 +1,2 @@ -print("File has been loaded!") \ No newline at end of file +print("File has been loaded!") +print(state); \ No newline at end of file diff --git a/demo/demo/rollball.cpp b/demo/demo/rollball.cpp index 55f8631..c594b32 100644 --- a/demo/demo/rollball.cpp +++ b/demo/demo/rollball.cpp @@ -81,7 +81,9 @@ namespace demo { m_phys = physService->create(&world(), entFactory); auto* scriptProvider = m_owner.service<fggl::script::ScriptProvider>(); + m_scripts = scriptProvider->create(); + m_scripts->setGlobal("state", this); // asset loader auto* assetLoader = m_owner.service<fggl::assets::Loader>(); diff --git a/include/fggl/script/engine.hpp b/include/fggl/script/engine.hpp index f81d7f1..fb566cf 100644 --- a/include/fggl/script/engine.hpp +++ b/include/fggl/script/engine.hpp @@ -40,6 +40,8 @@ namespace fggl::script { // run code in engine virtual bool run(const char* script) = 0; virtual bool load(const char* filename) = 0; + + virtual void setGlobal(const char* name, void* ptr) = 0; }; class ScriptProvider { diff --git a/integrations/lua/include/fggl/script/lua/engine.hpp b/integrations/lua/include/fggl/script/lua/engine.hpp index 0442c74..dcce6f2 100644 --- a/integrations/lua/include/fggl/script/lua/engine.hpp +++ b/integrations/lua/include/fggl/script/lua/engine.hpp @@ -45,6 +45,9 @@ namespace fggl::script::lua { bool run(const char* script) override; bool load(const char* filename) override; + // variables + void setGlobal(const char* name, void* ptr) override; + private: data::Storage* m_storage; lua_State* m_state; diff --git a/integrations/lua/src/engine.cpp b/integrations/lua/src/engine.cpp index 984b588..e171ef8 100644 --- a/integrations/lua/src/engine.cpp +++ b/integrations/lua/src/engine.cpp @@ -95,4 +95,9 @@ namespace fggl::script::lua { return true; } + void LuaScriptEngine::setGlobal(const char *name, void *ptr) { + lua_pushlightuserdata(m_state, ptr); + lua_setglobal(m_state, name); + } + } \ No newline at end of file -- GitLab