From 0b6a5731401348ab5931a80e07e134ae460d9955 Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:37:47 +0200 Subject: Added DeleteComponentsById<> method --- mwe/ecs-homemade/inc/ComponentManager.hpp | 8 +++++++- mwe/ecs-homemade/src/main.cpp | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'mwe/ecs-homemade') diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index e6523ca..75b2ab1 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -18,7 +18,13 @@ template void ComponentManager::DeleteComponentsById(std::uint32_t id) { std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) - + if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) + std::vector>>& componentArray = mComponents[type]; //Get the correct vector<> + + if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> + componentArray[id].clear(); //Clear the whole vector<> of this specific type and id + } + } } template diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index e1ca9e9..f0b94f0 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -69,8 +69,8 @@ int main() { } std::cout << std::endl; - ComponentManager::GetInstance().DeleteComponents(); - gameObect5.AddComponent(); + ComponentManager::GetInstance().DeleteComponentsById(gameObect2.mId); + gameObect2.AddComponent(); std::cout << "Finding all rigidbodies of entity 3" << std::endl; std::vector> rigidbodyOfEntity3 = ComponentManager::GetInstance().GetComponentsByID(gameObect3.mId); -- cgit v1.2.3