Search Unity

Create and access an Entity

Discussion in 'Entity Component System' started by alexandre-fiset, Feb 18, 2019.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Noob question here.

    I've built a simple script to update a global time of day. I made it using the Job System and ECS to familiarize myself with both. The thing is: I don't know what would be the best way to store and access these values.

    My script in its current form:

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using Unity.Burst;
    5. using UnityEngine;
    6.  
    7. namespace Game
    8. {
    9.     public class WorldTimeSystem : JobComponentSystem
    10.     {
    11.         private const float m_scale = 10;
    12.        
    13.         [BurstCompile]
    14.         struct AdvanceTimeJob : IJobProcessComponentData<WorldTime>
    15.         {
    16.             [ReadOnly] public float DeltaTime;
    17.             [ReadOnly] public float Scale;
    18.  
    19.             public void Execute(ref WorldTime worldTime)
    20.             {
    21.                 var unscaledSeconds = worldTime.UnscaledSeconds + DeltaTime;
    22.                 var hour = worldTime.Hour + ((unscaledSeconds * Scale) / 3600);
    23.                 var day = worldTime.Day;
    24.  
    25.                 if (hour >= 23)
    26.                 {
    27.                     day += 1;
    28.                     hour = hour - 23;
    29.                 }
    30.  
    31.                 worldTime = new WorldTime
    32.                 {
    33.                     UnscaledSeconds = unscaledSeconds,
    34.                     Day = day,
    35.                     Hour = hour,
    36.                     Scale = Scale
    37.                 };
    38.             }
    39.         }
    40.  
    41.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    42.         {
    43.             return new AdvanceTimeJob
    44.             {
    45.                 DeltaTime = Time.deltaTime,
    46.                 Scale = m_scale
    47.             }.Schedule(this, inputDeps);
    48.         }
    49.     }
    50. }
    51.  
    Since I only need one WorldTime and I need to access it from other jobs, for instance to move our sun's directional light according to the time of the day, what's the best way to achieve that?

    Can I update/set static variables in a job?
    Can I create an entity reference and use it in other jobs?

    Sorry for the simple questions, but I don't know where to start to accomplish this. All the samples I found don't cover that aspect of "having only one object to process and reuse its data"...
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    If there is only 1 worldtime and you require there to be 1, you can just use var worldTime = GetSingleton<WorldTime>() and then just pass it to other jobs. It's a bit easier than using GetComponentGroup() and having to iterate in every other job.
     
  3. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Oh great, GetSingleton works :) Thanks!