R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ComponentBindingHelper.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** rtype
4** File description:
5** ComponentBindingHelper - Simplifies component registration
6*/
7
8#pragma once
9
10#include <functional>
11#include <sol/sol.hpp>
12#include <string>
13#include <unordered_map>
14#include <vector>
16
17namespace scripting::bindings {
18
24 std::string name;
25 std::function<void(sol::state &)> bindFunc;
26 };
27
41 public:
55 template <typename T>
56 void add(const std::string &name, std::function<void(sol::state &)> bindFunc);
57
67 template <typename T>
68 void registerComponent(const std::string &name);
69
74 void applyComponentBindings(sol::state &lua);
75
85 void applyEntityMethods(sol::usertype<ecs::wrapper::Entity> &entityType);
86
93 void applyRemoveFunction(sol::state &lua, ecs::wrapper::ECSWorld *world);
94
99 const std::vector<ComponentBinding> &getBindings() const;
100
104 void clear();
105
106 private:
107 std::vector<ComponentBinding> _bindings;
108 std::unordered_map<std::string, std::function<sol::object(ecs::wrapper::Entity &, sol::this_state)>>
110 std::unordered_map<std::string, std::function<bool(ecs::wrapper::Entity &)>> _hasCheckers;
111 std::unordered_map<std::string, std::function<void(ecs::wrapper::Entity &)>> _removers;
112 };
113
114 // Template implementation
115 template <typename T>
116 void ComponentBindingHelper::add(const std::string &name, std::function<void(sol::state &)> bindFunc) {
117 _bindings.push_back({name, bindFunc});
118
119 _getters[name] = [](ecs::wrapper::Entity &e, sol::this_state L) -> sol::object {
120 sol::state_view lua(L);
121 if (!e.has<T>()) {
122 return sol::lua_nil;
123 }
124 return sol::make_object(lua, &e.get<T>());
125 };
126
127 _hasCheckers[name] = [](ecs::wrapper::Entity &e) -> bool {
128 return e.has<T>();
129 };
130
131 _removers[name] = [](ecs::wrapper::Entity &e) {
132 e.remove<T>();
133 };
134 }
135
145 template <typename T>
146 void ComponentBindingHelper::registerComponent(const std::string &name) {
147 // Store direct lambdas that sol2 can understand
148 _getters[name] = [](ecs::wrapper::Entity &e, sol::this_state L) -> sol::object {
149 sol::state_view lua(L);
150 if (!e.has<T>()) {
151 return sol::lua_nil;
152 }
153 return sol::make_object(lua, &e.get<T>());
154 };
155
156 // CRITICAL: Use a direct lambda that sol2 can properly bind
157 // The key is to not wrap it in std::function before assigning to usertype
158 _hasCheckers[name] = [](ecs::wrapper::Entity &e) -> bool {
159 return e.has<T>();
160 };
161
162 _removers[name] = [](ecs::wrapper::Entity &e) {
163 e.remove<T>();
164 };
165 }
166
167} // namespace scripting::bindings
High-level ECS manager providing clean server-side API.
Definition ECSWorld.hpp:122
High-level entity wrapper providing fluent interface.
Definition ECSWorld.hpp:33
bool has() const
Check if this entity has a specific component.
Entity & remove()
Remove a component from this entity.
T & get()
Get a component from this entity.
Helper to simplify component registration.
void applyComponentBindings(sol::state &lua)
Apply all component bindings to the Lua state.
std::unordered_map< std::string, std::function< void(ecs::wrapper::Entity &)> > _removers
std::unordered_map< std::string, std::function< sol::object(ecs::wrapper::Entity &, sol::this_state)> > _getters
void applyRemoveFunction(sol::state &lua, ecs::wrapper::ECSWorld *world)
Create the global removeComponent function for all components.
std::unordered_map< std::string, std::function< bool(ecs::wrapper::Entity &)> > _hasCheckers
const std::vector< ComponentBinding > & getBindings() const
Get the list of registered components.
void applyEntityMethods(sol::usertype< ecs::wrapper::Entity > &entityType)
Apply get/has methods on Entity for all components.
void registerComponent(const std::string &name)
Register a component that's already been bound to Lua.
void clear()
Clear all registered components (for testing/reinitialization)
void add(const std::string &name, std::function< void(sol::state &)> bindFunc)
Information about a registered component.
std::function< void(sol::state &)> bindFunc