Search Unity

Explicit EntityQuery support in JobComponentSystem Entities.ForEach()

Discussion in 'Entity Component System' started by maxxa05, Feb 14, 2020.

  1. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    I just wonder, this feature, which is available in the similar ComponentSystem Entities.ForEach() via the .With(query) method, is still not available in JobComponentSystem Entities.ForEach(). Is this supposed to be added eventually? I usually prefer to define my queries explicitely, and I'd like to know if this is a valid way to create queries for the long term. Without a .With(query) method, explicit queries will stay useless down the line.
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
  3. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    I see that method in the docs, even the latest version of the docs... but I can't seem to find it anywhere in code and IntelliSense isn't helping. :(

    If it's no longer available, I'd like to second maxxa05's suggestion. Using an EntityQuery nice because you can use it with EntityCommandBuffer.DestroyEntity(EntityQuery).
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Entities.ForEach and Job.WithCode are using compiler extension so the IDE will probably not list the correct parameters. This is a huge pain
     
  5. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    I'm trying to make it work, but it's harder than it seems. First, if I use

    .Schedule(query, inputDeps);

    my code editor (Rider) gives me an error, and it doesn't compile in Unity. If I use

    .Schedule<ForEachLambdaJobDescription>(query, inputDeps);

    Rider is ok with it, but it still doesn't compile in Unity. The error is the following:

    error CS1503: Argument 2: cannot convert from 'Unity.Entities.EntityQuery' to 'Unity.Entities.ComponentSystemBase'

    Funny thing is, if I swap the query to the system I use it in (this), it says that it cannot convert from 'Unity.Entities.ComponentSystemBase' to 'Unity.Entities.EntityQuery'. So yeah, it exists, but it doesn't seem to be usable yet.
     
  6. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    In fact, I just realized this is the .Schedule() method for the struct job syntax. So yeah, I don't think there's a way to use a query with the new Entities.ForEach() syntax yet.
     
  7. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    You can retrieve the implicit query from a ForEach with
    .WithStoreEntityQueryInField(ref query)
    : https://docs.unity3d.com/Packages/c...job_foreach.html#entitiesforeach-entity-query

    It works at compile time so you can even use the query before your ForEach if you need to.

    Unity has said the ability to just pass in a query is coming, but unless I'm missing something it would just be syntactic sugar. It should be possible to define any query implicitly just through the ForEach.
     
  8. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    Thing is, I usually prefer to define my queries explicitely for readability purposes. It's possible with everything else, except the new syntax. I know that I don't absolutely need it, but I think it makes my code easier to read and maintain.
     
    Sarkahn likes this.