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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Why do we perform queries with SystemAPI, not SystemState?

Discussion in 'Entity Component System' started by chemicalcrux, Oct 11, 2022.

  1. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    This is more of an academic question, but I don't understand why we grab singletons through a static method on SystemAPI. I suppose the same question goes for performing queries in general.

    (this is in Entities 1.0, if it matters)

    I understand that SystemState is used by unmanaged systems to access the current entity state. Component lookups use the SystemState to update themselves, and you can access the EntityManager through it.

    But why do we query for singletons, and perform queries in general, through SystemAPI? It seems like you use SystemState to get an EntityQuery, but that you use SystemAPI to actually perform queries.

    I'd think you would need to perform the query in a specific world: information that would be attached to be attached to the SystemState, and not necessarily available to the static SystemAPI methods.
     
  2. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    910
    SystemAPI is generating code. It does way more than just the line you're calling. SystemAPI.GetSingleton generates a private EntityQuery and sets it up in OnCreate.

    It's not quite there yet but SystemAPI is trying to reduce boilerplate code. We are missing Component/BufferTypeHandle but ComponentLookup is already implemented. Just calling SystemAPI.GetComponentLookup generates the private ComponentLookup, the Get in OnCreate and an .Update in the system Update method. All with just one line. I think that's pretty neat.
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Ahh, that makes sense! Codegen of this sort is pretty new to me. I had a sneaking suspicion there was something going on under the hood :)

    Thanks!

    I didn't realize GetComponentLookup would actually record it for you, rather than generating it every time. That's going to be very handy.
     
    Last edited: Oct 11, 2022