Search Unity

Question RayCast Inside IJobEntity

Discussion in 'Entity Component System' started by andreyakladov, Apr 17, 2023.

  1. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    I'm getting error when calling CastRay from within IJobEntity. What is the proper way to call it? When I CastRay from ISystem it works just fine.

    Error
    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job ProjectileControlJob reads from the UNKNOWN_OBJECT_TYPE ProjectileControlJob.JobData.CollisionWorld.EntityBodyIndexMap. You must call JobHandle.Complete() on the job ProjectileControlJob, before you can write to the UNKNOWN_OBJECT_TYPE safely.
    Job
    Code (CSharp):
    1. [BurstCompile]
    2.     public partial struct ProjectileControlJob : IJobEntity
    3.     {
    4.         public float DeltaTime;
    5.         public EntityCommandBuffer.ParallelWriter ECB;
    6.         [ReadOnly] public CollisionWorld CollisionWorld;
    7.  
    8.         [BurstCompile]
    9.         private void Execute(
    10.             [EntityIndexInQuery] int index,
    11.             ProjectileAspect projectileData,
    12.             RefRW<LocalTransform> localTransform,
    13.             RefRO<CollisionFilterData> collisionFilterData,
    14.             Entity projectileEntity)
    15.         {
    16.             /* Calculate position-related values */
    17.             ProcessCollisions(position, nextPosition, collisionFilter);
    18.             /* Move projectile if not collided */
    19.         }
    20.  
    21.         [BurstCompile]
    22.         private bool ProcessCollisions(float3 from, float3 to, CollisionFilter collisionFilter)
    23.         {
    24.             var input = new RaycastInput
    25.             {
    26.                 Start = from,
    27.                 End = to,
    28.                 Filter = collistionFilter
    29.             };
    30.             if (!CollisionWorld.CastRay(input, out var hit)) return false;
    31.             Debug.Log("Collided");
    32.             return true;
    33.         }
    34.     }
    System
    Code (CSharp):
    1. [BurstCompile]
    2.         public void OnUpdate(ref SystemState state)
    3.         {
    4.             var ecb = SystemAPI
    5.                 .GetSingleton<BeginInitializationEntityCommandBufferSystem.Singleton>()
    6.                 .CreateCommandBuffer(state.WorldUnmanaged)
    7.                 .AsParallelWriter();
    8.             var dt = SystemAPI.Time.fixedDeltaTime;//DeltaTime;
    9.             var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<PhysicsWorldSingleton>();
    10.             var singletonQuery = state.EntityManager.CreateEntityQuery(builder);
    11.             var collisionWorld = singletonQuery.GetSingleton<PhysicsWorldSingleton>().CollisionWorld;
    12.             state.Dependency = new ProjectileControlJob {DeltaTime = dt, ECB = ecb, CollisionWorld = collisionWorld}.ScheduleParallel(state.Dependency);
    13.             singletonQuery.Dispose();
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Make sure to include state.Dependency to the specified ECBSystem via AddJobHandleForProducer.
    Also, disabling burst / job safetly should provide better error description.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You shouldn’t. Getting ECB Singleton already handle dependencies automatically. You only need AddJobHandleForProducer if you access non-singleton ECB system.
     
    xVergilx likes this.
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You shouldn’t create query and all that burden in OnUpdate. You should just directly call:
    SystemAPI.GetSingleton<PhysicsWorldSingleton>()
    , because you’re working in ISystem, code I see in OnUpdate for getting physics world seems copy-paste from doc samples, but it’s for calling it from MB land, and comments for that sample specify that in system you don’t need that.
     
    xVergilx and andreyakladov like this.
  5. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    That helped, thanks a lot!