3#include "Lua/MyLua.hpp"
5#include "entt/entt.hpp"
12#include <unordered_map>
59 template <
typename Event>
62 if (!Lua::TypeExists<Event>(
lua))
65 event.LuaRegister(
lua);
68 dispatcher.sink<Event>().
template connect<&LuaManager::OnEvent<Event>>(
this);
82 template <
typename Event>
85 if (!Lua::TypeExists<Event>(
lua))
88 event.LuaRegister(
lua);
91 std::string functionName =
"Trigger" + DemangleWithoutNamespace<Event>() +
"Event";
93 if (!Lua::FunctionExists(
lua, functionName.c_str()))
95 Lua::RegisterFunction(
lua, functionName.c_str(), [
this, &dispatcher](Event event)
97 std::unique_lock lock(m_mutex);
98 m_pendingEvents.push([&dispatcher, e = std::move(event)]() mutable
100 dispatcher.trigger<Event>(std::move(e));
115 bool LoadScript(
const std::string& path);
124 void RemoveScript(
const std::string& path);
132 bool IsScriptLoaded(
const std::string& path);
141 void EnableScript(
const std::string& path);
151 void DisableScript(
const std::string& path);
161 std::optional<sol::environment> GetScriptEnvironment(
const std::string& path);
169 void ReloadScripts();
185 void Update(
const float deltaT);
196 template <
typename Event>
197 void OnEvent(
const Event& event)
201 std::unique_lock lock(m_mutex);
203 for (
auto& [path, script] : m_scripts)
207 std::string functionName =
"On" + DemangleWithoutNamespace<Event>() +
"Event";
209 m_pendingEvents.push([&script, functionName, copy]()
211 Lua::CallFunction<false>(script.environment, functionName.c_str(), copy);
219 std::unordered_map<std::string, LuaScript> m_scripts;
221 std::queue<std::function<void()>> m_pendingEvents;
Core engine.
Definition Engine.hpp:90
void RegisterReceiveEvent(entt::dispatcher &dispatcher)
Connects the dispatcher so all loaded scripts receive events of type Event.
Definition LuaManager.hpp:60
sol::state lua
The shared Lua state; all scripts execute within this state.
Definition LuaManager.hpp:172
void RegisterSendEvent(entt::dispatcher &dispatcher)
Exposes a global Lua function that fires an event on the dispatcher.
Definition LuaManager.hpp:83
Holds the runtime state of a single Lua script.
Definition LuaManager.hpp:23
bool enabled
When false the script's Update function is not called each frame.
Definition LuaManager.hpp:31
std::string directory
Directory the script was loaded from; used to resolve sandboxed requires.
Definition LuaManager.hpp:28
sol::environment environment
Isolated Lua environment; each script gets its own globals table.
Definition LuaManager.hpp:25