diff --git a/Resources/Engine/Lua/Scene/Actor.lua b/Resources/Engine/Lua/Scene/Actor.lua index 48a1432b..f1400bf2 100644 --- a/Resources/Engine/Lua/Scene/Actor.lua +++ b/Resources/Engine/Lua/Scene/Actor.lua @@ -33,6 +33,12 @@ function Actor:GetGUID() end ---@return Actor[] function Actor:GetChildren() end +--- Finds a child actor by name +---@param name string +---@param recursive boolean +---@return Actor|nil +function Actor:FindChild(name, recursive) end + --- Returns the parent (or nil) of this actor ---@return Actor|nil function Actor:GetParent() end diff --git a/Sources/OvCore/include/OvCore/ECS/Actor.h b/Sources/OvCore/include/OvCore/ECS/Actor.h index 018fb94d..7add0c76 100644 --- a/Sources/OvCore/include/OvCore/ECS/Actor.h +++ b/Sources/OvCore/include/OvCore/ECS/Actor.h @@ -175,6 +175,13 @@ namespace OvCore::ECS */ std::vector& GetChildren(); + /** + * Finds a child actor by name + * @param p_name + * @param p_recursive + */ + Actor* FindChild(const std::string& p_name, bool p_recursive) const; + /** * Mark the Actor as "Destroyed". A "Destroyed" actor will be removed from the scene by the scene itself */ diff --git a/Sources/OvCore/src/OvCore/ECS/Actor.cpp b/Sources/OvCore/src/OvCore/ECS/Actor.cpp index f62aae69..e82f75b0 100644 --- a/Sources/OvCore/src/OvCore/ECS/Actor.cpp +++ b/Sources/OvCore/src/OvCore/ECS/Actor.cpp @@ -247,6 +247,30 @@ std::vector& OvCore::ECS::Actor::GetChildren() return m_children; } +OvCore::ECS::Actor* OvCore::ECS::Actor::FindChild(const std::string& p_name, bool p_recursive) const +{ + for (auto child : m_children) + { + if (child->GetName() == p_name) + { + return child; + } + } + + if (p_recursive) + { + for (auto child : m_children) + { + if (auto found = child->FindChild(p_name, true); found) + { + return found; + } + } + } + + return nullptr; +} + void OvCore::ECS::Actor::MarkAsDestroy() { m_destroyed = true; diff --git a/Sources/OvCore/src/OvCore/Scripting/Lua/Bindings/LuaActorBindings.cpp b/Sources/OvCore/src/OvCore/Scripting/Lua/Bindings/LuaActorBindings.cpp index b0b6df95..bc3ec5e1 100644 --- a/Sources/OvCore/src/OvCore/Scripting/Lua/Bindings/LuaActorBindings.cpp +++ b/Sources/OvCore/src/OvCore/Scripting/Lua/Bindings/LuaActorBindings.cpp @@ -39,6 +39,7 @@ void BindLuaActor(sol::state& p_luaState) "SetName", &Actor::SetName, "GetTag", &Actor::GetTag, "GetChildren", &Actor::GetChildren, + "FindChild", &Actor::FindChild, "SetTag", &Actor::SetTag, "GetID", &Actor::GetID, "GetGUID", [](Actor& p_actor) { return std::format("{:016X}", p_actor.GetGUID()); },