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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How do you get a BufferFromEntity or ComponentDataFromEntity without Inject?

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

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    942
    Since there are plans to deprecate Inject, I would like to avoid it and use the non-inject way. But how do you do this? I've only known to use Inject for these two.
     
  2. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    Pretty sure you can just call GetComponentDataFromEntity<Type>(bool isReadOnly) from your system's update
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
  4. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    could you please link a proof of that [Inject] going to be removed

    as far as I know, only "group struct injection" going to be replaced with something better, and there is nothing wrong with injection itself
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    Auto/reflection filled group structs
    Field injection


    Yep same thought.
    easy to get confused, or misinterpret, if we are new.
     
    SubPixelPerfect likes this.
  7. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    yes, looks like I'm wrong and all injects go away

    Here is a minimal example for GetComponentDataFromEntity without injection
    Code (CSharp):
    1.  
    2. public struct A : IComponentData{ public int Value; }
    3. public struct B : IComponentData{ public Entity OtherEntity;}
    4.  
    5. public class TestSys : JobComponentSystem{
    6.  
    7.   protected override JobHandle OnUpdate(JobHandle inputDeps){
    8.     return new Job(){
    9.         a = GetComponentDataFromEntity<A>() // read/write
    10.     }.Schedule(this, inputDeps);
    11.   }
    12.  
    13.   [BurstCompile]
    14.   private struct Job : IJobProcessComponentData<B>{
    15.     // beware of race conditions when ParallelForRestriction  disabled
    16.     [NativeDisableParallelForRestriction] public ComponentDataFromEntity<A> a;
    17.     public void Execute( [ReadOnly]ref B b){
    18.       Entity e = b.OtherEntity;
    19.       if( a.Exists( e) ){
    20.         var v = a[e].Value + 1;
    21.         a[e] = new A(){Value = v};
    22.       }
    23.     }
    24.   }
    25.  
    26. }
    27.  
     
    Last edited: Nov 25, 2018
  8. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    And minimal example for DynamicBuffer

    Code (CSharp):
    1. public struct A : IComponentData{  public int Value; }
    2. public struct BBuffer : IBufferElementData{
    3.   public int Value;
    4.   public static implicit operator int(BBuffer item) { return item.Value; }
    5.   public static implicit operator BBuffer(int i) { return new BBuffer { Value = i }; }
    6. }
    7.  
    8. public class TestSys : JobComponentSystem{
    9.  
    10.   protected override JobHandle OnUpdate(JobHandle inputDeps){
    11.     return new Job(){
    12.       BBuffers =  GetBufferFromEntity <BBuffer> (false) // read/write
    13.     }.Schedule(this, inputDeps);
    14.   }
    15.  
    16.   [BurstCompile]
    17.   [RequireComponentTag ( typeof(BBuffer) ) ]
    18.   private struct Job : IJobProcessComponentDataWithEntity<A>{
    19.  
    20.     // beware of race conditions when ParallelForRestriction disabled
    21.     [NativeDisableParallelForRestriction] public BufferFromEntity <BBuffer> BBuffers;
    22.  
    23.     public void Execute( Entity e, int i, [ReadOnly] ref A a ){
    24.       DynamicBuffer<BBuffer> buffer = BBuffers[e];
    25.       if( buffer.Length>0 )
    26.         buffer[0] = a.Value;
    27.     }
    28.   }
    29. }
     
    Last edited: Nov 25, 2018
    Antypodish and davenirline like this.
  9. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    942
    This forum is just so cool! Thanks!
     
  10. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    942
    I couldn't find GetBufferFromEntity(). I'm already using the latest of Unity.Entities.
     
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    davenirline and Antypodish like this.