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

System is not running if Entities query cant find any

Discussion in 'Entity Component System' started by victor_apihtin, Mar 17, 2021.

  1. victor_apihtin

    victor_apihtin

    Unity Technologies

    Joined:
    Mar 2, 2021
    Posts:
    28
    I find it strange that it is not only that OnUpdate is not being called if Entities.ForEach can't find any matches, but also that there are no warnings about it. Imagine having a logical error in it, and not understanding why the code just stopped executing for no reason. Especially considering, that if there is no Entities.ForEach present in the code, the system is just running normally
     
    Nyanpas likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    It is a bit confusing but it actually makes the most logical sense for most use cases (and it's performant)

    Anyway there are basically 4 cases
    1. if there are any entity queries in the system then the system will only run if at least 1 have a result
    2. if there are no entity queries in the system, the system will always run
    3. if you have multiple queries in a system but require a specific query to have results, you can use RequireForUpdate()
    4. if the system has [AlwaysUpdateSystem] it will always run regardless of any of the above
     
  3. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    To me it's confusing because you don't have an easy overview of what is being run from where. At least with gameobjects you can assume it's enabled when the script and the gameobject are on and it's in the scene you're running.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Well, before docs even exist we learned all of this through reading source code (which is easy to understand in that case), don't forget - it's still in preview. But for that specific case, it's past and if you'll open package documentation about Systems in ECS you'll see an absolutely clear description of that behaviour :)
    upload_2021-3-18_11-33-14.png
     
  5. victor_apihtin

    victor_apihtin

    Unity Technologies

    Joined:
    Mar 2, 2021
    Posts:
    28
    I did suspect it is with intention, I am just raising a flag about absense of warnings and absense of symmetry between
    A. No queries
    B. Query without a match

    I was also about to ask a question what happens if only one of the queries has no matches, but I got it covered :) Even though with the logic like this, it could be easily made by default that ALL queries must have matches. And that can also be defended as optimization
     
    Last edited: Mar 18, 2021
    Marco-Playable likes this.
  6. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    But this requires you to know where these OnUpdates() are by digging through the code. I am slightly in favour of the gameobject-to-entity workflow as you at least know that if it's in the scene it will be a part of ther system(s) running. There are no secrets or obscurities. Very easy to quickly scan to get an overview, especially for non-technical people.
     
  7. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    There is entities debugger for that. You can easily see. what systems are running and what not. Also DOTS.Editor has Systems window.
     
    MNNoxMortem, xVergilx and Antypodish like this.
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Additionally there is info regarding query match, telling why system runs, or not.

    Worth investigating. Specially when combining entityQuery with Entities.ForEach in the same system.
    There are few cave cats catches there, for someone unaware.
     
    MNNoxMortem likes this.
  9. Recatek

    Recatek

    Joined:
    May 2, 2020
    Posts:
    30
    This caching of entity queries is unintuitive and leads to unexpected behavior without warning. If you wrap your Entities.ForEach query in something like a check to see if a MonoBehaviour exists, you'll be looking at bugs where it ignores that check and performs the query anyway. I ran into exactly this and couldn't figure out what was going on until this thread, in fact.
     
  10. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    I was not aware it does this, and actually for me this is great :D Thanks for pointing that out and everyone who explained it!
     
  11. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    180
    It's important to note that it's not just Entities.ForEach.
    Any call on a system that asks for some kind of entity data will trigger this behaviour.

    I honestly think it's incredibly misleading and [AlwaysUpdateSystem] should be the default.
    Its not uncommon for systems to do things other than query for entities.
    The DOTS compiler extensions do some incredibly voodoo S***, this is among the most confusing for new people.
     
    ChrisPie likes this.
  12. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    Should the more common behaviour not be default? You learn this behaviour pretty quickly and at that point I don't see why you want want it opt out instead.

    I just had a quick scan of our project. We have 661 systems and 34 usages of [AlwaysUpdateSystem] for 5.1%

    An early out from no matching queries is actually much more efficient as it skips the whole JobHandle dependency combining.

    -edit- to just give you a vague idea about how much more performant it is, from unity's patch notes way back
     
    Last edited: Sep 26, 2021
    bit-master, Krajca and xVergilx like this.
  13. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    180
    Thanks for the numbers @tertle. I can't really argue with that.
    I imagine most cases where [AlwaysUpdateSystem] is required is for GameObject interop and are an edge case, e.g. for me, the one that was tripping me up was my camera follow system, I expected it to throw an error when it couldn't find the camera but instead it just didn't run. In this case I expect it to always run as I expect there will always be a camera.

    It's really just the way that it's done that is so left of field.
    Like you can write a line of code and it not get executed because further down in the body there is an entity query, that's just not normal in any programming language I know of.
    TBH, this is probably a problem for tooling to solve.
    If Rider gave me a warning that I had some code that wouldn't run when there are no matching entities then I wouldn't get caught out.
     
  14. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,218
    It's not really *that* 'out of left field', considering this behavior has been mentioned in Unity talks, it's plainly written in the docs, etc. I have seen it mentioned many times in many places and have known this for, what, 3ish years now or however long it's been since DOTS came about? I am not sure what more you are wanting/expecting?
     
  15. Goularou

    Goularou

    Joined:
    Oct 19, 2018
    Posts:
    50
    First of all, many thanks to tertle for his help!
    One crazy thing remains that if I encapsulate a foreach job in a if then{} test, including a query getting some entities in the process of being created (Entity Manager instructions upstream the foreach job), the use of [AlwaysUpdateSystem] appears necessary, while the test can not be

    Here is an example:

    Code (CSharp):
    1. EntityManager.SetComponentData<PhysicsVelocity>(_band, physicsVelocity);
    2.  
    3.         if (1 == 2)
    4.         {
    5.  
    6.            Entities.ForEach(
    7.                        (ref PhysicsVelocity physicsVelocity) => {
    8.                            float3 linear = 1f; //just for fill it up
    9.                        }).Run();
    10.         }
    _band is a global Entity in the system

    Thanks in advance
     
  16. chriscode

    chriscode

    Joined:
    Mar 2, 2015
    Posts:
    44
    Of course this has now changed: upload_2023-6-16_13-57-29.png
     

    Attached Files:

    victor_apihtin likes this.
  17. Goularou

    Goularou

    Joined:
    Oct 19, 2018
    Posts:
    50
    Yes. Thanks for the useful reminder: system are now always updated, thus it is the opposite to the previous situation.