Search Unity

Iterate through nested list of entities in job

Discussion in 'Entity Component System' started by MadboyJames, Sep 30, 2019.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    I'm trying to make a system that finds the closest target to an entity. In the job I iterate through every unit, and I'd like to iterate through every target for every unit (in a nested Entites.Foreach), but since I'm using jobs, I don't have access to "Entities" in the struct. How do I create an array of all entities in world with two specific components to pass into the find targets job? I was looking at EntityQuery, but my implementation gave many errors, So I figured I'd ask if EntityQuery is even the way to do what I want to do.

    Any help is appreciated.
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    you can pass a native array to your job using EntityQuery.ToComponentDataArray<Component> or EntityQuery.ToEntityArray()
     
    MadboyJames likes this.
  3. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Gotcha. I used EntityQuery.ToEntityArray(). also I found this about 10 minutes after posting the question.

    The issue was actually with another system due to me not accounting for a scenario that popped up.