Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with accessing variables from different entities

Discussion in 'Entity Component System' started by KitItSimpleStupid, Jun 3, 2020.

  1. KitItSimpleStupid

    KitItSimpleStupid

    Joined:
    Jun 3, 2020
    Posts:
    2
    I've spent the last couple weeks getting into ecs/dots with various video tutorials and bits of documentation, but I've gotten stuck for the last two days trying to figure out something that I would normally know how to do with object oriented scripts.

    I want to be able to take and save the translation of an entity and then use it against all other entity's translations to move all entities toward each other. I feel like once I understand the method to doing this, I can start to repurpose the same/similar method wherever as needed.

    The issue I seem to be running into so far is that Entities.ForEach isn't returning what I assumed it would, which is that each loop, the entity variable in the lamda would swap to a different entity in the scene but that doesn't seem to be the case from testing with a few different attempts.

    Here's my latest attempt where I try to just save the first translation the system captures (used a bool that is true once because I couldn't find a way to mark a translation variable as null) and then is supposed to iterate through all other entities and move them toward that saved translation.

    Code (CSharp):
    1.  protected override void OnUpdate()
    2.     {
    3. Entities
    4.         .ForEach((Entity entity, ref Translation trans, ref TransData transData) =>
    5.         {
    6.             if (transData.transNull == true)
    7.             {
    8.                 transData.trans = trans;
    9.                 transData.transNull = false;
    10.             }
    11.  
    12.             float3 direction = (transData.trans.Value - trans.Value);
    13.             trans.Value += (direction * 10);
    14.  
    15.         }).Schedule();
    16.     }
    Any help with solving this issue would be much appreciated.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    Declare your shared Translation inside the OnUpdate but outside the Entities.ForEach. You can either write to it outside the Entities.ForEach if you know which Entity you want, or you could add entityInQueryIndex to your ForEach and write to the shared Translation if that value is 0.
     
  3. KitItSimpleStupid

    KitItSimpleStupid

    Joined:
    Jun 3, 2020
    Posts:
    2
    Couldn't add the change you mentioned about the Translation variable without having to switch it to .Run() but the entityInQueryIndex seems like the exact thing I was looking for. Got a working variant where the first entity acts as the "anchor point" that all entities move towards at the moment. I should be able to iterate and experiment more now with this basis.

    Thanks a lot for the help!