Fix environment again
// TODO: proper exception handling for Lua funcs!
template <typename ...Args>
-static sol::protected_function_result TryLuaFunction(std::shared_ptr<spdlog::logger> aLogger, sol::function aFunc, Args... aArgs)
+static sol::protected_function_result TryLuaFunction(std::shared_ptr<spdlog::logger> aLogger, sol::environment& aEnv, sol::function aFunc, Args... aArgs)
{
sol::protected_function_result result{ };
if (aFunc)
{
try
{
- result = aFunc(aArgs...);
+ result = aFunc(aArgs..., aEnv);
}
catch(std::exception& e)
{
@@ -74,9 +74,12 @@ ScriptContext::ScriptContext(LuaSandbox& aLuaSandbox, const std::filesystem::pat
auto loggerRef = m_logger;
std::string vkBindID = m_name + '.' + acID;
- VKBind vkBind = { vkBindID, acDescription, [loggerRef, aCallback]()
+ VKBind vkBind = { vkBindID, acDescription, [loggerRef, aCallback, this]()
{
- TryLuaFunction(loggerRef, aCallback);
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
+ TryLuaFunction(loggerRef, env, aCallback);
}};
m_vkBindInfos.emplace_back(VKBindInfo{vkBind});
@@ -130,37 +133,52 @@ const std::vector<VKBindInfo>& ScriptContext::GetBinds() const
void ScriptContext::TriggerOnInit() const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onInit);
+ TryLuaFunction(m_logger, env, m_onInit);
}
void ScriptContext::TriggerOnUpdate(float aDeltaTime) const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onUpdate, aDeltaTime);
+ TryLuaFunction(m_logger, env, m_onUpdate, aDeltaTime);
}
void ScriptContext::TriggerOnDraw() const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onDraw);
+ TryLuaFunction(m_logger, env, m_onDraw);
}
void ScriptContext::TriggerOnOverlayOpen() const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onOverlayOpen);
+ TryLuaFunction(m_logger, env, m_onOverlayOpen);
}
void ScriptContext::TriggerOnOverlayClose() const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onOverlayClose);
+ TryLuaFunction(m_logger, env, m_onOverlayClose);
}
sol::object ScriptContext::GetRootObject() const
@@ -170,7 +188,10 @@ sol::object ScriptContext::GetRootObject() const
void ScriptContext::TriggerOnShutdown() const
{
+ auto& sb = m_sandbox[m_sandboxID];
+ auto& env = sb.GetEnvironment();
+
auto state = m_sandbox.GetState();
- TryLuaFunction(m_logger, m_onShutdown);
+ TryLuaFunction(m_logger, env, m_onShutdown);
}
