Skip to content

Commit

Permalink
Duplicate entity name increment
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Oct 31, 2024
1 parent 83d35e4 commit 42968cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions Engine/Source/Engine/Scene/ECSWorld.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#include "ECSWorld.h"

#include <format>

namespace sl
{

Entity ECSWorld::CreateEntity(std::string name)
Entity ECSWorld::CreateEntity(std::string_view name)
{
uint32_t index = 1;
std::string newName = name.data();
auto it = m_names.find(newName);
while (it != m_names.end())
{
newName = std::format("{} ({})", name.data(), index++);
it = m_names.find(newName);
}
m_names.insert(newName);

Entity entity{ m_registry.create() };
entity.AddComponent<TagComponent>(name);
entity.AddComponent<TagComponent>(std::move(newName));
entity.AddComponent<TransformComponent>();

return entity;
Expand Down
5 changes: 4 additions & 1 deletion Engine/Source/Engine/Scene/ECSWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <entt/entt.hpp>

#include <set>

namespace sl
{

Expand All @@ -25,7 +27,7 @@ class ECSWorld final
ECSWorld &operator=(ECSWorld &&) = delete;
~ECSWorld() = delete;

static Entity CreateEntity(std::string name = "Empty Entity");
static Entity CreateEntity(std::string_view name = "Empty Entity");
static entt::registry &GetRegistry() { return m_registry; }

static Entity GetMainCameraEntity();
Expand All @@ -34,6 +36,7 @@ class ECSWorld final

private:
inline static entt::registry m_registry;
inline static std::set<std::string> m_names;
};

// Basically, Entity is just a tool class that allows us to use ECS more intuitively.
Expand Down

0 comments on commit 42968cb

Please sign in to comment.