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

Physics queries writes to collisionWorld.m_Bodies and Entities.ForEach => 0.5

Discussion in 'Entity Component System' started by daschatten, Jan 23, 2020.

  1. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    This is a follow up to https://forum.unity.com/threads/invalidoperationexception-the-previously-scheduled-job.682744/. I decided to start a new thread with a better description and a slightly updated focus.

    If i need physics queries i usually add these dependencies to run the queries after the physics world is completed:

    Code (CSharp):
    1. [UpdateAfter(typeof(BuildPhysicsWorld))]
    2.  
    3. JobHandle.CombineDependencies(inputDeps, _buildPhysicsWorld.FinalJobHandle)
    This part works fine.

    Though the queries (e.g. collisionWorld.CastRay) write to collisionWorld.m_Bodies and so start my problems, like this:

    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job PlayerSpawnSystem:Job reads from the NativeArray Job.Data.collisionWorld.m_Bodies. You are trying to schedule a new job BipedCharacterMovementSystem:<>c__DisplayClass_Move_LambdaJob0, which writes to the same NativeArray (via <>c__DisplayClass_Move_LambdaJob0.Data.collisionWorld.m_Bodies). To guarantee safety, you must include PlayerSpawnSystem:Job as a dependency of the newly scheduled job.
    With the old IJobForEach i could add [ReadOnly] (Old code still in PlayerSpawnSystem:Job):

    Code (CSharp):
    1. [ReadOnly] public CollisionWorld collisionWorld;
    With the new Entities.ForEach() < 0.5 i did this:

    Code (CSharp):
    1. .WithReadOnly(collisionWorld)
    The entities >= 0.5 prevents this due to

    Code (CSharp):
    1. error DC0034: Entities.WithReadOnly is called with an argument collisionWorld of unsupported type Unity.Physics.CollisionWorld. It can only be called with an argument that is marked with [NativeContainerAttribute].
    My questions:
    1. Why does a query writes to the m_Bodies container?
    2. How can i use queries in Entities.ForEach() >= 0.5 with collisionWorld set to readonly?
     
    florianhanke likes this.
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    As a workaround for CollisionWorld as a readonly I'm using this:
    Code (CSharp):
    1.  
    2. class TestSystem : ... {
    3.     struct CollisionWorldRO {
    4.         [ReadOnly]
    5.         public CollisionWorld CollisionWorld;
    6.     }
    7.  
    8.     ... OnUpdate(...) {
    9.         var collisionWorldRO = new CollisionWorldRO {
    10.             CollisionWorld = ...
    11.         };
    12.         ... Entities
    13.             .ForEach((...) => {
    14.                     collisionWorldRO.CollisionWorld
    15.             })
    16.             ...;
    17. }
    18.  
    I'm on mobile so sorry to not be more precise with the code.

    Edit: accidentally hit publish before finishing typing

    []'s
     
    Last edited: Jan 23, 2020
    BigRookGames and florianhanke like this.
  3. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Uh nice, that works :) Thanks!
     
    florianhanke likes this.