R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
LuaEngine.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** LuaEngine - Core Lua script manager
6*/
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <mutex>
13#include <sol/sol.hpp>
14#include <string>
15#include <unordered_map>
16#include <vector>
18
19namespace scripting {
20
28 class LuaEngine {
29 public:
34 explicit LuaEngine(const std::string &scriptPath = "server/Scripting/scripts/");
35
46
52 bool loadScript(const std::string &scriptPath);
53
60 void executeUpdate(const std::string &scriptPath, ecs::wrapper::Entity entity, float deltaTime);
61
67 void executeOnGameStart(const std::string &scriptPath, ecs::wrapper::Entity entity);
68
75 template <typename... Args>
76 void callFunction(const std::string &scriptPath, const std::string &functionName, Args &&...args);
77
82 sol::state &getLuaState() { return _lua; };
83
88 void registerGameStartCallback(sol::function callback);
89
94 void fireGameStartCallbacks(const std::string &roomId);
95
100 void cleanupEntity(uint32_t entityId);
101
102 private:
103 sol::state _lua;
104 std::string _scriptPath;
105 std::unordered_map<std::string, sol::table> _scriptCache;
106 // Per-entity script state (for enemy scripts with local variables)
107 std::unordered_map<uint32_t, std::unordered_map<std::string, sol::table>> _entityScriptCache;
110 mutable std::recursive_mutex _luaMutex; // Protects _lua and _scriptCache from concurrent access
111
112 void initializeBindings();
113 //void bindComponents();
114
115 // Game start callbacks registered via onGameStart()
116 std::vector<sol::function> _gameStartCallbacks;
117 };
118
119} // namespace scripting
High-level ECS manager providing clean server-side API.
Definition ECSWorld.hpp:122
High-level entity wrapper providing fluent interface.
Definition ECSWorld.hpp:33
Manages Lua state and script execution for the server.
Definition LuaEngine.hpp:28
void executeOnGameStart(const std::string &scriptPath, ecs::wrapper::Entity entity)
Execute onGameStart function for an entity's script.
void registerGameStartCallback(sol::function callback)
Register a Lua callback to be called when the game starts.
void cleanupEntity(uint32_t entityId)
Clean up script cache for a destroyed entity.
std::string _scriptPath
void executeUpdate(const std::string &scriptPath, ecs::wrapper::Entity entity, float deltaTime)
Execute onUpdate function for an entity's script.
sol::state & getLuaState()
Get direct access to Lua state (advanced usage).
Definition LuaEngine.hpp:82
std::recursive_mutex _luaMutex
void setWorld(ecs::wrapper::ECSWorld *world)
Set the ECS world for entity operations.
Definition LuaEngine.cpp:30
std::unordered_map< uint32_t, std::unordered_map< std::string, sol::table > > _entityScriptCache
void callFunction(const std::string &scriptPath, const std::string &functionName, Args &&...args)
Call a specific Lua function.
bool loadScript(const std::string &scriptPath)
Load and cache a Lua script.
Definition LuaEngine.cpp:60
ecs::wrapper::ECSWorld * _world
std::unordered_map< std::string, sol::table > _scriptCache
std::vector< sol::function > _gameStartCallbacks
void fireGameStartCallbacks(const std::string &roomId)
Fire all registered game start callbacks.