Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Entity Component System' started by AOH, Jan 8, 2020.

  1. AOH

    AOH

    Joined:
    Feb 8, 2014
    Posts:
    4
    Hi, I've created this unit test to make sure I understand the ISystemStateComponentData component.

    Code (CSharp):
    1.     [Test]
    2.     public void SystemStateComponentExistsAfterDestroyEntity() {
    3.       var e = _entityManager.CreateEntity();
    4.       _entityManager.AddComponent<TestComponentData>(e);
    5.       _entityManager.AddComponent<TestSystemStateComponentData>(e);
    6.       // We have two components.
    7.       Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(2));
    8.       // After destroying the entity we only have the system state component left.
    9.       _entityManager.DestroyEntity(e);
    10.       Assert.That(_entityManager.HasComponent<TestComponentData>(e), Is.False);
    11.       Assert.That(_entityManager.HasComponent<TestSystemStateComponentData>(e), Is.True);
    12.       assertion fails, GetComponentCount() returns 2???
    13.       Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(1));
    14.       // Removing the system state component removes the entity.
    15.       _entityManager.RemoveComponent<TestSystemStateComponentData>(e);
    16.       Assert.That(_entityManager.Exists(e), Is.False);
    17.     }
    Why is GetComponentCount() returning 2, but the HasComponent<TestComponentData>(e) returns false? This seems like a bug with GetComponentCount() to me?
     
    pal_trefall likes this.
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    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
     
    pal_trefall likes this.
  3. AOH

    AOH

    Joined:
    Feb 8, 2014
    Posts:
    4
    Thanks! That makes sense I guess. I should have checked which components were on the entity.
     
  4. AOH

    AOH

    Joined:
    Feb 8, 2014
    Posts:
    4
    I was going to do this, but CleanupEntity is an internal struct so I guess I'll have to leave that assert out.
    Code (CSharp):
    1.       Assert.That(_entityManager.HasComponent<TestComponentData>(e), Is.False);
    2.       Assert.That(_entityManager.HasComponent<TestSystemStateComponentData>(e), Is.True);
    3.       Assert.That(_entityManager.HasComponent<CleanupEntity>(e), Is.True);
    4.       Assert.That(_entityManager.GetComponentCount(e), Is.EqualTo(2));