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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to properly check "if entity disabled"?

Discussion in 'Entity Component System' started by alvinwan, Jul 8, 2020.

  1. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    I've setup a demo Unity project with just one entity and system. The goal of this demo is to check if the entity is disabled. Just like I'd check for any other component, I setup the system with `GetComponentDataFromEntity` as follows:

    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3.  
    4. public class System : SystemBase
    5. {
    6.     protected override void OnUpdate()
    7.     {
    8.         ComponentDataFromEntity<Disabled> disabledGroup = GetComponentDataFromEntity<Disabled>();
    9.         Dependency = Entities.ForEach((Entity entity) =>
    10.         {
    11.             UnityEngine.Debug.Log(disabledGroup.Exists(entity));
    12.         }).Schedule(Dependency);
    13.     }
    14. }

    However, this results in a mysterious InvalidOperationException.

    Code (CSharp):
    1. InvalidOperationException: The writeable ComponentDataFromEntity<Unity.Entities.Disabled> <>c__DisplayClass_OnUpdate_LambdaJob0.JobData.disabledGroup is the same Unity.Entities.ArchetypeChunkEntityType as <>c__DisplayClass_OnUpdate_LambdaJob0.safety, two containers may not be the same (aliasing).
    2. Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode, System.Boolean isParallel) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/IJobChunk.cs:216)
    3. Unity.Entities.JobChunkExtensions.ScheduleSingle[T] (T jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/IJobChunk.cs:111)
    4. System.OnUpdate () (at Assets/System.cs:38)
    5. Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/SystemBase.cs:414)
    6. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:445)
    7. UnityEngine.Debug:LogException(Exception)
    8. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Stubs/Unity/Debug.cs:19)
    9. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:450)
    10. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:398)
    11. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystem.cs:109)
    12. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ScriptBehaviourUpdateOrder.cs:192)
    13.  
    14.  

    I've noticed this error come up before when two archetypes are indistinguishable e.g., exact same number of each field type. I suspect the issue is now because the Disabled struct is empty. I've searched the forums and checked all threads mentioning "disabled", with no solution in sight. What is the proper way to check if an entity is disabled?


    Some context: One solution for the above demo is to write ForEach((Entity entity, Disabled disabled) => {...}). However, this wouldn't work for my actual need: In short, I have a parent component referencing a child entity. My goal is to check if that child entity is disabled or not. Thus, the need for `GetComponentDataFromEntity`.

    More context: I'm trying to swap between two prefabs, depending on the state of an object. (Specifically, object is healthy or object is sick. Pick a prefab depending on healthy or sick). My naive approach is to instantiate both prefabs and disable the prefabs selectively. If more experienced folks have other suggestions, open to ideas (e.g., change mesh directly?)
     

    Attached Files:

    Last edited: Jul 8, 2020
  2. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    Why don't use simple: .Without<Disabled> or .With<Disabled>?
     
  3. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    WithNone
     
    Krajca likes this.
  4. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    Entities with a Disabled component are excluded from queries. IDK if you can force them to be included but I don't think so... also the error is because you need to capture the ComponentDataFromEntity. Add .WithReadOnly(disabledGroup) to the query or switch to .Run()
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    Always can.

    Code (CSharp):
    1.             GetEntityQuery(new EntityQueryDesc()
    2.             {
    3.                 All = ... ,
    4.                 Any = ... ,
    5.                 None = ... ,
    6.                 Options = EntityQueryOptions.IncludeDisabled
    7.             });
    Code (CSharp):
    1. .WithEntityQueryOptions(EntityQueryOptions.IncludeDisabled)
     
  6. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    Many thanks everyone! That fixes it.