Ensure generating unique Entity IDs

This commit is contained in:
Simon Lübeß
2024-03-17 20:25:38 +01:00
parent 84204aed46
commit a2c7bf7d06
2 changed files with 15 additions and 4 deletions
@@ -24,10 +24,9 @@ namespace GlitchyEngine.World
{
public readonly UUID ID;
/// Create a new IDComponent with a random UUID.
public this()
{
ID = UUID.Create();
ID = .Zero;
}
public this(UUID id)
+14 -2
View File
@@ -834,7 +834,7 @@ namespace GlitchyEngine.World
}
/// Creates a new Entity with the given name.
public Entity CreateEntity(StringView name = "", UUID id = default)
public Entity CreateEntity(StringView name = "", UUID id = .Zero)
{
Entity entity = Entity(_ecsWorld.NewEntity(), this);
entity.AddComponent<TransformComponent>();
@@ -848,7 +848,7 @@ namespace GlitchyEngine.World
#endif
// If no id is given generate a random one.
IDComponent idComponent = (id == default) ? IDComponent() : IDComponent(id);
IDComponent idComponent = (id == .Zero) ? IDComponent(CreateEntityId()) : IDComponent(id);
entity.AddComponent<IDComponent>(idComponent);
@@ -857,6 +857,18 @@ namespace GlitchyEngine.World
return entity;
}
/// Creates a new entity Id that is unique within this scene.
public UUID CreateEntityId()
{
while (true)
{
UUID entityId = UUID.Create();
if (!_idToEntity.ContainsKey(entityId))
return entityId;
}
}
/** Deletes the given entity.
* @param entity The entity to delete.
* @param destroyChildren If set to true all children of entity will be destroyed.