R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
scripting::LuaEngine Class Reference

Manages Lua state and script execution for the server. More...

#include <LuaEngine.hpp>

Collaboration diagram for scripting::LuaEngine:
Collaboration graph

Public Member Functions

 LuaEngine (const std::string &scriptPath="server/Scripting/scripts/")
 Constructor with scripts directory path.
 
void setWorld (ecs::wrapper::ECSWorld *world)
 Set the ECS world for entity operations.
 
bool loadScript (const std::string &scriptPath)
 Load and cache a Lua script.
 
void executeUpdate (const std::string &scriptPath, ecs::wrapper::Entity entity, float deltaTime)
 Execute onUpdate function for an entity's script.
 
void executeOnGameStart (const std::string &scriptPath, ecs::wrapper::Entity entity)
 Execute onGameStart function for an entity's script.
 
template<typename... Args>
void callFunction (const std::string &scriptPath, const std::string &functionName, Args &&...args)
 Call a specific Lua function.
 
sol::state & getLuaState ()
 Get direct access to Lua state (advanced usage).
 
void registerGameStartCallback (sol::function callback)
 Register a Lua callback to be called when the game starts.
 
void fireGameStartCallbacks (const std::string &roomId)
 Fire all registered game start callbacks.
 
void cleanupEntity (uint32_t entityId)
 Clean up script cache for a destroyed entity.
 

Private Member Functions

void initializeBindings ()
 

Private Attributes

sol::state _lua
 
std::string _scriptPath
 
std::unordered_map< std::string, sol::table > _scriptCache
 
std::unordered_map< uint32_t, std::unordered_map< std::string, sol::table > > _entityScriptCache
 
ecs::wrapper::ECSWorld_world
 
bool _bindingsInitialized
 
std::recursive_mutex _luaMutex
 
std::vector< sol::function > _gameStartCallbacks
 

Detailed Description

Manages Lua state and script execution for the server.

Handles loading, caching, and executing Lua scripts associated with entities. Provides bindings for ECS components and game logic.

Definition at line 28 of file LuaEngine.hpp.

Constructor & Destructor Documentation

◆ LuaEngine()

scripting::LuaEngine::LuaEngine ( const std::string &  scriptPath = "server/Scripting/scripts/")
explicit

Constructor with scripts directory path.

Parameters
scriptsPathBase path for Lua scripts

Definition at line 21 of file LuaEngine.cpp.

References _lua, LOG_INFO, and LOG_WARNING.

Member Function Documentation

◆ callFunction()

template<typename... Args>
void scripting::LuaEngine::callFunction ( const std::string &  scriptPath,
const std::string &  functionName,
Args &&...  args 
)

Call a specific Lua function.

Parameters
scriptPathPath to the script
functionNameName of the function to call
argsVariadic arguments to pass to Lua

Definition at line 284 of file LuaEngine.cpp.

References _bindingsInitialized, _luaMutex, _scriptCache, _world, loadScript(), LOG_ERROR, and LOG_WARNING.

Here is the call graph for this function:

◆ cleanupEntity()

void scripting::LuaEngine::cleanupEntity ( uint32_t  entityId)

Clean up script cache for a destroyed entity.

Parameters
entityIdThe ID of the entity to clean up

Definition at line 346 of file LuaEngine.cpp.

References _entityScriptCache, _luaMutex, and LOG_DEBUG.

Referenced by scripting::LuaSystemAdapter::update().

◆ executeOnGameStart()

void scripting::LuaEngine::executeOnGameStart ( const std::string &  scriptPath,
ecs::wrapper::Entity  entity 
)

Execute onGameStart function for an entity's script.

Parameters
scriptPathPath to the script
entityEntity wrapper

Definition at line 153 of file LuaEngine.cpp.

References _bindingsInitialized, _luaMutex, _scriptCache, _world, loadScript(), LOG_ERROR, and LOG_WARNING.

Here is the call graph for this function:

◆ executeUpdate()

void scripting::LuaEngine::executeUpdate ( const std::string &  scriptPath,
ecs::wrapper::Entity  entity,
float  deltaTime 
)

Execute onUpdate function for an entity's script.

Parameters
scriptPathPath to the script
entityEntity wrapper
deltaTimeFrame delta time

Definition at line 193 of file LuaEngine.cpp.

References _bindingsInitialized, _luaMutex, _scriptCache, _world, loadScript(), LOG_ERROR, and LOG_WARNING.

Referenced by scripting::LuaSystemAdapter::update().

Here is the call graph for this function:

◆ fireGameStartCallbacks()

void scripting::LuaEngine::fireGameStartCallbacks ( const std::string &  roomId)

Fire all registered game start callbacks.

Parameters
roomIdThe ID of the room where the game started

Definition at line 320 of file LuaEngine.cpp.

References _gameStartCallbacks, _luaMutex, LOG_DEBUG, LOG_ERROR, and LOG_INFO.

◆ getLuaState()

sol::state & scripting::LuaEngine::getLuaState ( )
inline

Get direct access to Lua state (advanced usage).

Returns
sol::state& Reference to the Lua state

Definition at line 82 of file LuaEngine.hpp.

References _lua.

◆ initializeBindings()

void scripting::LuaEngine::initializeBindings ( )
private

Definition at line 47 of file LuaEngine.cpp.

References _lua, _world, scripting::bindings::bindComponents(), scripting::bindings::bindEntity(), scripting::bindings::bindServerGame(), and scripting::bindings::bindWorld().

Referenced by setWorld().

Here is the call graph for this function:

◆ loadScript()

bool scripting::LuaEngine::loadScript ( const std::string &  scriptPath)

Load and cache a Lua script.

Parameters
scriptPathRelative path to script file
Returns
true if loaded successfully

Definition at line 60 of file LuaEngine.cpp.

References _lua, _luaMutex, _scriptCache, _scriptPath, LOG_ERROR, and LOG_INFO.

Referenced by callFunction(), executeOnGameStart(), and executeUpdate().

◆ registerGameStartCallback()

void scripting::LuaEngine::registerGameStartCallback ( sol::function  callback)

Register a Lua callback to be called when the game starts.

Parameters
callbackLua function to call

Definition at line 313 of file LuaEngine.cpp.

References _gameStartCallbacks, _luaMutex, and LOG_DEBUG.

◆ setWorld()

void scripting::LuaEngine::setWorld ( ecs::wrapper::ECSWorld world)

Set the ECS world for entity operations.

This method must be called before executing any Lua scripts that interact with entities or components. It initializes the Lua bindings with the world context.

Parameters
worldPointer to the ECS world (must not be nullptr)
Exceptions
std::invalid_argumentif world is nullptr

Definition at line 30 of file LuaEngine.cpp.

References _bindingsInitialized, _world, initializeBindings(), LOG_ERROR, LOG_INFO, and LOG_WARNING.

Here is the call graph for this function:

Member Data Documentation

◆ _bindingsInitialized

bool scripting::LuaEngine::_bindingsInitialized
private

Definition at line 109 of file LuaEngine.hpp.

Referenced by callFunction(), executeOnGameStart(), executeUpdate(), and setWorld().

◆ _entityScriptCache

std::unordered_map<uint32_t, std::unordered_map<std::string, sol::table> > scripting::LuaEngine::_entityScriptCache
private

Definition at line 107 of file LuaEngine.hpp.

Referenced by cleanupEntity().

◆ _gameStartCallbacks

std::vector<sol::function> scripting::LuaEngine::_gameStartCallbacks
private

Definition at line 116 of file LuaEngine.hpp.

Referenced by fireGameStartCallbacks(), and registerGameStartCallback().

◆ _lua

sol::state scripting::LuaEngine::_lua
private

Definition at line 103 of file LuaEngine.hpp.

Referenced by getLuaState(), initializeBindings(), loadScript(), and LuaEngine().

◆ _luaMutex

std::recursive_mutex scripting::LuaEngine::_luaMutex
mutableprivate

◆ _scriptCache

std::unordered_map<std::string, sol::table> scripting::LuaEngine::_scriptCache
private

Definition at line 105 of file LuaEngine.hpp.

Referenced by callFunction(), executeOnGameStart(), executeUpdate(), and loadScript().

◆ _scriptPath

std::string scripting::LuaEngine::_scriptPath
private

Definition at line 104 of file LuaEngine.hpp.

Referenced by loadScript().

◆ _world

ecs::wrapper::ECSWorld* scripting::LuaEngine::_world
private

The documentation for this class was generated from the following files: