GetComponentCount() bug after DestroyEntity() with system state component data?

Hi, I’ve created this unit test to make sure I understand the ISystemStateComponentData component.

    [Test]
    public void SystemStateComponentExistsAfterDestroyEntity() {
      var e = _entityManager.CreateEntity();
      _entityManager.AddComponent<TestComponentData>(e);
      _entityManager.AddComponent<TestSystemStateComponentData>(e);
      // We have two components.
      Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(2));
      // After destroying the entity we only have the system state component left.
      _entityManager.DestroyEntity(e);
      Assert.That(_entityManager.HasComponent<TestComponentData>(e), Is.False);
      Assert.That(_entityManager.HasComponent<TestSystemStateComponentData>(e), Is.True);
      assertion fails, GetComponentCount() returns 2???
      Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(1));
      // Removing the system state component removes the entity.
      _entityManager.RemoveComponent<TestSystemStateComponentData>(e);
      Assert.That(_entityManager.Exists(e), Is.False);
    }

Why is GetComponentCount() returning 2, but the HasComponent(e) returns false? This seems like a bug with GetComponentCount() to me?

1 Like

Not a bug. DestroyEntity will add an internal component called CleanupEntity whey encontering a ISystemState* component. This component is used to track entities that should be destroyed after all ISystemState* components have been removed.

[ ]'s

*edit: formatting

1 Like

Thanks! That makes sense I guess. I should have checked which components were on the entity.

I was going to do this, but CleanupEntity is an internal struct so I guess I’ll have to leave that assert out.

      Assert.That(_entityManager.HasComponent<TestComponentData>(e), Is.False);
      Assert.That(_entityManager.HasComponent<TestSystemStateComponentData>(e), Is.True);
      Assert.That(_entityManager.HasComponent<CleanupEntity>(e), Is.True);
      Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(2));