Manages sandboxed Lua scripts and their integration with the event dispatcher.
More...
#include <LuaManager.hpp>
|
| template<typename Event> |
| void | RegisterReceiveEvent (entt::dispatcher &dispatcher) |
| | Connects the dispatcher so all loaded scripts receive events of type Event.
|
| template<typename Event> |
| void | RegisterSendEvent (entt::dispatcher &dispatcher) |
| | Exposes a global Lua function that fires an event on the dispatcher.
|
| bool | LoadScript (const std::string &path) |
| | Loads and executes a Lua script in its own sandboxed environment.
|
| void | RemoveScript (const std::string &path) |
| | Unloads a script and discards its environment.
|
| bool | IsScriptLoaded (const std::string &path) |
| | Checks whether a script is currently loaded.
|
| void | EnableScript (const std::string &path) |
| | Allows a script's Update function to be called each frame.
|
| void | DisableScript (const std::string &path) |
| | Prevents a script's Update function from being called each frame.
|
| std::optional< sol::environment > | GetScriptEnvironment (const std::string &path) |
| | Returns the sol::environment of a loaded script.
|
| void | ReloadScripts () |
| | Re-executes all loaded scripts in fresh environments.
|
|
|
sol::state | lua |
| | The shared Lua state; all scripts execute within this state.
|
Manages sandboxed Lua scripts and their integration with the event dispatcher.
Each script runs in its own sol::environment so scripts cannot interfere with each other's globals. Scripts receive a sandboxed require that is restricted to their own directory.
Event dispatch into Lua is deferred: events are queued on the calling thread and flushed at the start of the next Update to avoid calling into Lua while holding the internal mutex.
◆ DisableScript()
| void LuaManager::DisableScript |
( |
const std::string & | path | ) |
|
Prevents a script's Update function from being called each frame.
The script remains loaded and its environment is preserved. Does nothing if the script is not loaded.
- Parameters
-
| path | File path of the script to disable |
◆ EnableScript()
| void LuaManager::EnableScript |
( |
const std::string & | path | ) |
|
Allows a script's Update function to be called each frame.
Does nothing if the script is not loaded.
- Parameters
-
| path | File path of the script to enable |
◆ GetScriptEnvironment()
| std::optional< sol::environment > LuaManager::GetScriptEnvironment |
( |
const std::string & | path | ) |
|
Returns the sol::environment of a loaded script.
Useful for reading or writing script globals from C++.
- Parameters
-
| path | File path of the script |
- Returns
- The script's environment, or nullopt if it is not loaded
◆ IsScriptLoaded()
| bool LuaManager::IsScriptLoaded |
( |
const std::string & | path | ) |
|
Checks whether a script is currently loaded.
- Parameters
-
- Returns
- True if the script has been loaded and not yet removed
◆ LoadScript()
| bool LuaManager::LoadScript |
( |
const std::string & | path | ) |
|
Loads and executes a Lua script in its own sandboxed environment.
The script is stored under its path so it can be retrieved, reloaded, or removed later. Returns false if the script is already loaded or fails to execute.
- Parameters
-
| path | File path of the script to load |
- Returns
- True if the script was loaded successfully
◆ RegisterReceiveEvent()
template<typename Event>
| void LuaManager::RegisterReceiveEvent |
( |
entt::dispatcher & | dispatcher | ) |
|
|
inline |
Connects the dispatcher so all loaded scripts receive events of type Event.
Registers the C++ type with Lua if it has not been registered yet. Scripts handle the event by defining a global function named "On<EventTypeName>Event".
- Template Parameters
-
| Event | Event type; must expose a static LuaRegister(sol::state&) method |
- Parameters
-
| dispatcher | Dispatcher to listen on |
◆ RegisterSendEvent()
template<typename Event>
| void LuaManager::RegisterSendEvent |
( |
entt::dispatcher & | dispatcher | ) |
|
|
inline |
Exposes a global Lua function that fires an event on the dispatcher.
Registers the C++ type with Lua if it has not been registered yet. The generated function is named "Trigger<EventTypeName>Event". The trigger is deferred through the pending-events queue to keep dispatches on the main thread.
- Template Parameters
-
| Event | Event type; must expose a static LuaRegister(sol::state&) method |
- Parameters
-
| dispatcher | Dispatcher to trigger events on |
◆ ReloadScripts()
| void LuaManager::ReloadScripts |
( |
| ) |
|
Re-executes all loaded scripts in fresh environments.
Useful for hot-reloading during development. Each script's environment is replaced and the file is re-run from disk.
◆ RemoveScript()
| void LuaManager::RemoveScript |
( |
const std::string & | path | ) |
|
Unloads a script and discards its environment.
Does nothing if the script is not currently loaded.
- Parameters
-
| path | File path of the script to remove |
The documentation for this class was generated from the following files: