From f6be6689aa2a24fe26091405c319aa5bebcdb279 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 6 Aug 2025 23:34:00 +0300 Subject: [PATCH] feat: passing args to component in entity definition --- src/content/loading/EntityLoader.cpp | 13 ++++++++++++- src/logic/scripting/scripting.cpp | 2 ++ src/objects/Entities.cpp | 4 ++-- src/objects/Entities.hpp | 11 +++++++++-- src/objects/EntityDef.hpp | 10 ++++++++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/content/loading/EntityLoader.cpp b/src/content/loading/EntityLoader.cpp index 7778282c..29a72cb7 100644 --- a/src/content/loading/EntityLoader.cpp +++ b/src/content/loading/EntityLoader.cpp @@ -30,7 +30,18 @@ template<> void ContentUnitLoader::loadUnit( if (auto found = root.at("components")) { for (const auto& elem : *found) { - def.components.emplace_back(elem.asString()); + std::string name; + dv::value params; + if (elem.isObject()) { + name = elem["name"].asString(); + if (elem.has("args")) { + params = elem["args"]; + } + } else { + name = elem.asString(); + } + def.components.push_back(ComponentInstance { + std::move(name), std::move(params)}); } } if (auto found = root.at("hitbox")) { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 39168843..56c5a17e 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -603,6 +603,8 @@ void scripting::on_entity_spawn( } else { lua::createtable(L, 0, 0); } + } else if (component->params != nullptr) { + lua::pushvalue(L, component->params); } else { lua::createtable(L, 0, 0); } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 4cfe1b29..cdf979e0 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -173,9 +173,9 @@ entityid_t Entities::spawn( auto& scripting = registry.emplace(entity); registry.emplace(entity, skeleton->instance()); - for (auto& componentName : def.components) { + for (auto& instance : def.components) { auto component = std::make_unique( - componentName, EntityFuncsSet {}, nullptr + instance.component, EntityFuncsSet {}, nullptr, instance.params ); scripting.components.emplace_back(std::move(component)); } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 6518b599..5bd8f955 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -80,11 +80,18 @@ struct UserComponent { std::string name; EntityFuncsSet funcsset; scriptenv env; + dv::value params; UserComponent( - const std::string& name, EntityFuncsSet funcsset, scriptenv env + const std::string& name, + EntityFuncsSet funcsset, + scriptenv env, + dv::value params ) - : name(name), funcsset(funcsset), env(env) { + : name(name), + funcsset(funcsset), + env(std::move(env)), + params(std::move(params)) { } }; diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index 53c69c95..4d10a2d0 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -5,6 +5,7 @@ #include #include "typedefs.hpp" +#include "data/dv.hpp" #include "maths/aabb.hpp" #include "physics/Hitbox.hpp" @@ -12,12 +13,17 @@ namespace rigging { class SkeletonConfig; } +struct ComponentInstance { + std::string component; + dv::value params; +}; + struct EntityDef { /// @brief Entity string id (with prefix included) std::string const name; - /// @brief Component IDs - std::vector components; + /// @brief Component instances + std::vector components; /// @brief Physic body type BodyType bodyType = BodyType::DYNAMIC;