mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Ensure generating unique Entity IDs
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user