Search Unity

What is the proper way to manipulate entities or components in non ECS context like MonoBehaviour?

Discussion in 'Entity Component System' started by davenirline, Nov 25, 2018.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    I'm destroying some entities via a MonoBehaviour class using EntityManager but I keep getting this error:

    InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x4ac1a530 + 0x00052> in <2f14d49edc704940aee557642343f344>:0
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at C:/buildslave/unity/build/Runtime/Export/AtomicSafetyHandle.bindings.cs:143)
    Unity.Entities.ArchetypeChunk.GetNativeArray (Unity.Entities.ArchetypeChunkEntityType archetypeChunkEntityType) (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/Iterators/ArchetypeChunkArray.cs:63)
    Unity.Transforms.TransformSystem.UpdateFrozen () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Transforms/TransformSystem.cs:320)
    Unity.Transforms.TransformSystem.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Transforms/TransformSystem.cs:1178)
    Unity.Entities.JobComponentSystem.InternalUpdate () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/ComponentSystem.cs:508)
    Unity.Entities.ScriptBehaviourManager.Update () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/ScriptBehaviourManager.cs:77)
    Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelagateWrapper.TriggerUpdate () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/ScriptBehaviourUpdateOrder.cs:703)

    I figured that since I've disrupted the arrays by destroying an entity using EntityManager, the ECS context doesn't like this. I tried using a system's PostUpdateCommand. I got an instance of the system using World.GetOrCreateManager(). On the call to EntityCommandBuffer.DestroyEntity(), I got the following error:

    InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x505961b0 + 0x00052> in <2f14d49edc704940aee557642343f344>:0
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at C:/buildslave/unity/build/Runtime/Export/AtomicSafetyHandle.bindings.cs:153)
    Unity.Entities.EntityCommandBuffer.EnforceSingleThreadOwnership () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/EntityCommandBuffer.cs:556)
    Unity.Entities.EntityCommandBuffer.DestroyEntity (Unity.Entities.Entity e) (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.21/Unity.Entities/EntityCommandBuffer.cs:684)

    I'm stuck. What are my other options?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Any chance for simplified code snippet, with error reproduction?
     
  3. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    MonoBehaviour isn't a good place to do that
    create a ComponentSystem, query entities and destroy using PostUpdateCommands
     
  4. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    I'm integrating a framework made in ECS to an existing nonECS project. What you're saying is not always possible or disrupts too much code. I just want to manipulate entities/components from outside ECS safely. I don't think it's impossible with the current system.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    ECS entities can not be manipulated outside ECS. At least not in safe manner.
    You need use Hybrid ECS approach for that purpose.
     
  6. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    After more investigation, I've found out that everything works if I just remove the Static component of the entities I'm trying to modify. Is this a potential bug? I do need the Static component as it allows further optimization among my systems.
     
  7. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Regarding the first question: you may just do what you did, it is safe. EntityManager will Complete all jobs currently accessing ECS. Of course this is not the most performant way to do it. Using a command buffer, as you already tried to do, is better. To get one you can actually use in the main thread get a barrier system, eg EndFrameBarrier and use CreateCommandBuffer on it.

    I think the static issue is not related.
     
    Last edited: Nov 26, 2018
    eizenhorn likes this.
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Yes they can, any call to EntityManager forces a sync so it's safe.

    This is a known issue with static and the TransformSystem.
    Discussion here and a solution (if you're willing to modify the TransformSystem)
    https://forum.unity.com/threads/got...eption-for-a-simple-code.583876/#post-3902383
     
    davenirline, Antypodish and eizenhorn like this.
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    They can and they do :) Hybrid it’s absolutely same as pure just with couple of wrappers for manipulating heap objects, most part of things (under hood) do pure stuff there and here :)
    And agree with @tertle, it’s just call Static changes untill Pending Frozen not processed.
     
    Antypodish likes this.
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Yep just realizing, while reading thread. I guess more to learn for me ;)
     
  11. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    The TransformSystem hack works. Thanks!