Search Unity

Removing Disabled from entities via EntityQuery

Discussion in 'Project Tiny' started by Maras, Jan 24, 2020.

  1. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    I am trying to remove component Disabled via EntityQuery:

    Code (CSharp):
    1. ecb.RemoveComponent(entityQuery, typeof(Disabled));
    It works in editor and in a Release build, but not in Debug (uncaught exception). I tried WASM and windows il2cpp.

    Creating array from the query and using
    Code (CSharp):
    1. EntityManager.SetEnabled(entityQuery[i], true)
    also does not work in Debug builds.

    Any idea why?
    Thanks
     
    Last edited: Jan 24, 2020
  2. kevinmv

    kevinmv

    Unity Technologies

    Joined:
    Nov 15, 2018
    Posts:
    10
    EntityQueries can have a few extra Options provided to them to affect what components they will include in their search. By default, `Disabled` components are excluded. You will need to ensure you create your EntityQuery via an EntityQueryDesc and pass in the `IncludeDisabled` flag:
    https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/component_group.html

    (From the link above, modified for Disabled)

    Code (CSharp):
    1. var getAllDisabledComponentQueryDesc = new EntityQueryDesc{
    2.    All = new ComponentType{typeof(Disabled)},
    3.    Options = EntityQueryDescOptions.IncludeDisabled
    4. };
    5. var entityQuery = GetEntityQuery(getAllDisabledComponentQueryDesc);
    I imagine the exception you are getting in Debug is that
    RemoveComponent 
    cannot find the Disabled component and the check is disabled in release builds. If this is not the case, please post the exception message and stacktrace to help narrow dow the issue.

    All the best,
    Kev
     
    Maras likes this.
  3. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    I will try with EntityQueryDesc and let you know, thanks. I created the query including the disabled component and it works as it should both in release/editor which is a bit confusing.
     
  4. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
  5. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    Tried with desc, it still does not work under debug.

    OnCreate:
    Code (CSharp):
    1. var eqDesc = new EntityQueryDesc()
    2.         {
    3.             All = new ComponentType[] {
    4.                 ComponentType.ReadOnly<TouchSymbolTag>(),
    5.                 ComponentType.ReadWrite<Disabled>()
    6.             },
    7.  
    8.             Options = EntityQueryOptions.IncludeDisabled
    9.         };
    10.        
    11.         disabledTouchSymbolEQ = GetEntityQuery(eqDesc);
    OnUpdate:
    Code (CSharp):
    1. ecb.RemoveComponent(disabledTouchSymbolEQ, typeof(Disabled));
    disabledTouchSymbolEQ.CalculateEntityCount() is 1

    Works in Editor and Release, not in Debug
     
  6. kevinmv

    kevinmv

    Unity Technologies

    Joined:
    Nov 15, 2018
    Posts:
    10
    Based on that call stack it looks like you are accessing a DynamicBuffer after making a structural change (adding/removing a component) and the safety system (only enabled when not in release) is asserting since I believe DynamicBuffers are invalidated after structural changes. (I can't seem to find a document reference for this so I'll see if we can add this as a remark since it's important and non-obvious).


    In case it's helpful in the future, when you run in editor you are running DOTS Hybrid. You will be able to get a more representative and nicer debugging experience making a BuildSetting for DotNet (rather than wasm) and debugging that build in VisualStudio/Rider. Builds made from BuildSettings are Tiny Player Builds which are the builds that will ultimately feed through our build pipeline to generating the Wasm output in Wasm builds.

    Hope this helps!
    Kev
     
    Maras likes this.
  7. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
  8. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    It seems that the issue is that removing the Disabled component is actually making some changes in the parent object in the background - adding the child as a buffer element. The object I am trying to re-enable is not a root object. The whole "scene" is actually a prefab to simulate a simple SceneManager.

    Is that an anti-pattern? Or is there a specific time when a system or command buffer doing this kind of work should be triggered? And why it behaves correctly under release builds?



    upload_2020-1-27_14-6-30.png
     
  9. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    If I create the entity query in the same frame the Disabled component is removed, it works correctly even under debug.
     
  10. kevinmv

    kevinmv

    Unity Technologies

    Joined:
    Nov 15, 2018
    Posts:
    10
    Ah changing child entities in a hierarchy will trigger the Transform system to update the hierarchy appropriately. You should be able to avoid this if the entities in your prefab have the `Prefab` component added to them since Prefabs like Disabled components are excluded from general EntityQueries.

    A bit troubling you didn't see the asserts in the editor though. Do you have SafetyChecks disabled perhaps? (located under Jobs > Burst > Safety Checks from the editor toolbar)
     
  11. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    The "Scene" which is created from regular prefab has component "Prefab" added automatically and all of its children have it as well.

    This is happening for instantiated "Scene" entities, which are active and simulated though.

    All I want to do is re-enable an entity that was previously disabled - this entity was originally part of a prefab.

    I have the safety checks enabled.

    Is this wrong and should I avoid disabling/enabling entities that were part of a prefab?
     
unityunity