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

Resolved InvalidOperationException while dealing with ComponentLookup

Discussion in 'Entity Component System' started by jay3sh, Sep 1, 2023.

  1. jay3sh

    jay3sh

    Joined:
    Dec 30, 2013
    Posts:
    18
    Hi,

    I'm trying to write a piece of code based on the pattern I've seen elsewhere, but I'm getting a runtime error. My code looks similar to what I see in SnakeSystems.cs from EntitiesSamples.

    I've a BeingSystem that works on Being IComponentData. In its OnUpdate I want to schedule a IJobEntity. The Execute method of that IJobEntity needs access to ComponentLookup, because I cannot do SystemAPI.GetComponent in the Execute method. My code looks like this

    Code (CSharp):
    1.  
    2.     [BurstCompile]
    3.     [RequireMatchingQueriesForUpdate]
    4.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    5.     public partial struct BeingSystem : ISystem
    6.     {
    7.         ComponentLookup<Being> m_beingLookup;
    8.  
    9.         [BurstCompile]
    10.         public void OnCreate(ref SystemState state)
    11.         {
    12.             state.RequireForUpdate<Being>();
    13.             m_beingLookup = state.GetComponentLookup<Being>(true);
    14.         }
    15.  
    16.         [BurstCompile]
    17.         public void OnUpdate(ref SystemState state)
    18.         {
    19.             m_beingLookup.Update(ref state);
    20.  
    21.             new BeingIdleJob
    22.             {
    23.                 BeingLookup = m_beingLookup,
    24.             }.ScheduleParallel();
    25.         }
    26.     }
    27.  
    28.     [BurstCompile]
    29.     partial struct BeingIdleJob : IJobEntity
    30.     {
    31.         [ReadOnly]
    32.         public AgentSpatialPartitioningSystem.Singleton Spatial;
    33.         [ReadOnly]
    34.         public ComponentLookup<Being> BeingLookup;
    35.  
    36.         public void Execute(Entity entity, ref Being being)
    37.         {
    38.         }
    39.     }
    40. }
    When I run this code, I get

    InvalidOperationException: The writeable ComponentTypeHandle<com.bluemathsoftware.dots.Being> BeingIdleJob.JobData.__TypeHandle.__com_bluemathsoftware_dots_Being_RW_ComponentTypeHandle is the same ComponentLookup<com.bluemathsoftware.dots.Being> as BeingIdleJob.JobData.BeingLookup, two containers may not be the same (aliasing).

    What am I doing wrong?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    You can't have a lookup for the same type of component you are accessing as one of the Execute parameters.
     
    HiddenTess and rhodos like this.
  3. jay3sh

    jay3sh

    Joined:
    Dec 30, 2013
    Posts:
    18
    That must be it. Dropping
    ref Being being
    from the signature or changing ref to in fixes the error.
    Thanks!
     
    rhodos likes this.
  4. rhodos

    rhodos

    Joined:
    Mar 25, 2020
    Posts:
    5
    this worked like a charm. thanks!