Search Unity

GetEntity using identifier / Best way to find an associated entity

Discussion in 'Entity Component System' started by alexandre-fiset, Apr 3, 2019.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Is there a way to get an entity based on a specific identifier?

    For instance, if I had a GlobalClock I could use GetSingleton() to retrieve its unique instance, but what I'd like to do is to get a Clock based on a unique identifier, so that I can bind the right Clock for the right job.

    Think of a game with 5 planets with each their own Clock, with systems on each planet acting differently based on their local time.

    This is where I'm stuck:

    Code (CSharp):
    1.    protected override JobHandle OnUpdate(JobHandle inputDependencies)
    2.         {
    3.            
    4.             var job = new UpdateSpansJob()
    5.             {
    6.                 Time = GetSingleton<Clock>(), // TODO: Need to pass the right uid Clock there
    7.                 RealtimeDeltaTime = Time.deltaTime
    8.             };
    9.             return job.Schedule(this, inputDependencies);
    10.         }
    What is the best way to do this?

    It's a really simple thing but I don't even know where to start... I'm struggling to manage dependencies and binding in ECS.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Create 5 entities each one containing Planet and Clock components then use IJobProcessData<Planet, Clock> to operate on those components.
     
  3. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    What I mean is that on the same planet there are many components listening to the planet's clock. I don't know what's the best way to "pass" that information to the systems...

    For instance, 10 Volumes are on Planet A and change according to the Clock A time of day. 5 other Volumes, this time on Planet B, change according to the Clock B time of day. And so on.

    The samples and examples lack scenarios like this where multiple systems need data processed by other systems. The answer must be simple but since there's no documention I just don't know where to start.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    Sounds like you want a ClockReference ISharedComponentData that contains an entity corresponding to a planet's clock. You then loop through each clock entity on the main thread, and for each clock schedule a job with that clock the same way you assigned it in your singleton example. The trick is that you also set your ISharedComponentData filter on your query for each clock so that you only process the entities which use the clock assigned to the job.
     
    alexandre-fiset likes this.
  5. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Oh sh*t. I didn't even know I could store entitites in ISharedComponentData. That's powerful. Thanks :)