R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
LuaSystemAdapter.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** LuaSystemAdapter implementation
6*/
7
11
12namespace scripting {
13
15 : _luaEngine(luaEngine), _world(world) {
16 if (!luaEngine) {
17 LOG_ERROR("LuaSystemAdapter: LuaEngine is null");
18 }
19 if (!world) {
20 LOG_ERROR("LuaSystemAdapter: ECSWorld is null");
21 }
22 }
23
24 void LuaSystemAdapter::update(ecs::Registry &registry, float deltaTime) {
25 if (!_luaEngine || !_world) {
26 LOG_ERROR("LuaSystemAdapter not properly initialized");
27 return;
28 }
29
30 auto entities = registry.getEntitiesWithMask(this->getComponentMask());
31
32 static int logCounter = 0;
33 if (++logCounter % 60 == 0) {
34 //LOG_INFO("LuaSystemAdapter: Processing " + std::to_string(entities.size()) +
35 // " entities with LuaScript component");
36 }
37
38 for (const auto &entityAddr : entities) {
39 try {
40 // Check if entity still has LuaScript component
41 // (may have been destroyed by previous script execution)
42 try {
43 if (!registry.hasComponent<ecs::LuaScript>(entityAddr)) {
44 continue;
45 }
46 } catch (...) {
47 // Entity doesn't exist anymore, skip it
48 _luaEngine->cleanupEntity(entityAddr);
49 continue;
50 }
51
52 auto &luaScript = registry.getComponent<ecs::LuaScript>(entityAddr);
53 const std::string &scriptPath = luaScript.getScriptPath();
54
55 if (scriptPath.empty()) {
56 continue;
57 }
58
59 // Convert Registry address to ECSWorld Entity wrapper
60 ecs::wrapper::Entity entity = _world->getEntity(entityAddr);
61
62 if (!entity.isValid()) {
63 LOG_WARNING("Invalid entity " + std::to_string(entityAddr) +
64 " for script: " + scriptPath);
65 // Clean up script cache for invalid entity
66 _luaEngine->cleanupEntity(entityAddr);
67 continue;
68 }
69
70 // Execute the script via LuaEngine
71 _luaEngine->executeUpdate(scriptPath, entity, deltaTime);
72
73 } catch (const std::exception &e) {
74 LOG_ERROR("Error executing Lua script for entity " + std::to_string(entityAddr) + ": " +
75 std::string(e.what()));
76 }
77 }
78 }
79
81 return (1ULL << ecs::getComponentType<ecs::LuaScript>());
82 }
83
84} // namespace scripting
#define LOG_ERROR(...)
Definition Logger.hpp:183
#define LOG_WARNING(...)
Definition Logger.hpp:182
Component that holds the path to a Lua script for entity behavior.
Definition LuaScript.hpp:21
const std::string & getScriptPath() const
Get the path to the Lua script.
Definition LuaScript.hpp:40
Manages entities, their signatures and component type registrations.
Definition Registry.hpp:68
std::vector< Address > getEntitiesWithMask(Signature requiredMask)
Get all entities matching a specific component mask.
Definition Registry.cpp:79
T & getComponent(Address address)
Get a component from an entity.
bool hasComponent(Address address)
Check if an entity has a specific component.
High-level ECS manager providing clean server-side API.
Definition ECSWorld.hpp:122
Entity getEntity(Address address)
Get an entity wrapper from an address.
Definition ECSWorld.cpp:83
High-level entity wrapper providing fluent interface.
Definition ECSWorld.hpp:33
bool isValid() const
Check if this entity is valid.
Definition ECSWorld.cpp:43
Manages Lua state and script execution for the server.
Definition LuaEngine.hpp:28
void cleanupEntity(uint32_t entityId)
Clean up script cache for a destroyed entity.
void executeUpdate(const std::string &scriptPath, ecs::wrapper::Entity entity, float deltaTime)
Execute onUpdate function for an entity's script.
ecs::ComponentMask getComponentMask() const override
Get component mask for this system.
ecs::wrapper::ECSWorld * _world
LuaSystemAdapter(LuaEngine *luaEngine, ecs::wrapper::ECSWorld *world)
Constructor with LuaEngine and ECSWorld.
void update(ecs::Registry &registry, float deltaTime) override
Updates all Lua scripts for entities.
std::uint64_t ComponentMask
Type alias for component bitmask.
Definition ISystem.hpp:24