Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #4 on re-used Entity IDs #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions src/Archetypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Despawn(Identity identity)
meta.Row = 0;
meta.Identity = Identity.None;

UnusedIds.Enqueue(identity);
UnusedIds.Enqueue(new Identity(identity.Id, (ushort)(identity.Generation + 1)));

if (!_typesByRelationTarget.TryGetValue(identity, out var list))
{
Expand All @@ -96,6 +96,10 @@ public void Despawn(Identity identity)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddComponent(StorageType type, Identity identity, object data)
{
if (!IsAlive(identity))
Copy link

@thygrrr thygrrr Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using an assertion instead.

Nevermind, the Library throws runtime exceptions as well.

{
throw new Exception($"cannot add component {type.Type.Name} to despawned entity {identity}");
}
ref var meta = ref Meta[identity.Id];
var oldTable = Tables[meta.TableId];

Expand Down Expand Up @@ -139,6 +143,10 @@ public ref T GetComponent<T>(Identity identity, Identity target)
{
var type = StorageType.Create<T>(target);
var meta = Meta[identity.Id];
if (meta.Identity != identity)
{
throw new Exception($"cannot get component on despawned entity {identity}");
}
var table = Tables[meta.TableId];
var storage = (T[])table.GetStorage(type);
return ref storage[meta.Row];
Expand All @@ -147,13 +155,21 @@ public ref T GetComponent<T>(Identity identity, Identity target)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComponent(StorageType type, Identity identity)
{
if (identity == Identity.None)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early out feels wierd here, current HEAD already checks for Identity.none - so this can be...

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool HasComponent(StorageType type, Identity identity)
    {
        var meta = Meta[identity.Id];
        return meta.Identity != Identity.None 
               && meta.Identity == identity 
               && Tables[meta.TableId].Types.Contains(type);
    }

{
return false;
}
var meta = Meta[identity.Id];
return meta.Identity != Identity.None && Tables[meta.TableId].Types.Contains(type);
return meta.Identity == identity && Tables[meta.TableId].Types.Contains(type);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveComponent(StorageType type, Identity identity)
{
if (!IsAlive(identity))
{
throw new Exception($"cannot remove component {type.Type.Name} from despawned entity {identity}");
}
ref var meta = ref Meta[identity.Id];
var oldTable = Tables[meta.TableId];

Expand Down Expand Up @@ -283,13 +299,18 @@ internal bool IsMaskCompatibleWith(Mask mask, Table table)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool IsAlive(Identity identity)
{
return Meta[identity.Id].Identity != Identity.None;
return identity != Identity.None && Meta[identity.Id].Identity == identity;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ref EntityMeta GetEntityMeta(Identity identity)
{
return ref Meta[identity.Id];
ref var meta = ref Meta[identity.Id];
if (meta.Identity != identity)
{
throw new Exception($"cannot get entity meta on despawned entity {identity}");
}
return ref meta;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -302,6 +323,10 @@ internal Table GetTable(int tableId)
internal Entity GetTarget(StorageType type, Identity identity)
{
var meta = Meta[identity.Id];
if (meta.Identity != identity)
{
return Entity.None;
}
var table = Tables[meta.TableId];

foreach (var storageType in table.Types)
Expand All @@ -316,15 +341,17 @@ internal Entity GetTarget(StorageType type, Identity identity)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Entity[] GetTargets(StorageType type, Identity identity)
{
var meta = Meta[identity.Id];
var table = Tables[meta.TableId];

var list = ListPool<Entity>.Get();

foreach (var storageType in table.Types)
if (IsAlive(identity))
{
if (!storageType.IsRelation || storageType.TypeId != type.TypeId) continue;
list.Add(new Entity(storageType.Identity));
var meta = Meta[identity.Id];
var table = Tables[meta.TableId];
foreach (var storageType in table.Types)
{
if (!storageType.IsRelation || storageType.TypeId != type.TypeId) continue;
list.Add(new Entity(storageType.Identity));
}
}

var targetEntities = list.ToArray();
Expand All @@ -336,15 +363,17 @@ internal Entity[] GetTargets(StorageType type, Identity identity)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal (StorageType, object)[] GetComponents(Identity identity)
{
var meta = Meta[identity.Id];
var table = Tables[meta.TableId];

var list = ListPool<(StorageType, object)>.Get();

foreach (var type in table.Types)
if (IsAlive(identity))
{
var storage = table.GetStorage(type);
list.Add((type, storage.GetValue(meta.Row)!));
var meta = Meta[identity.Id];
var table = Tables[meta.TableId];
foreach (var type in table.Types)
{
var storage = table.GetStorage(type);
list.Add((type, storage.GetValue(meta.Row)!));
}
}

var array = list.ToArray();
Expand Down