Game.CalendarEventLaunchSystem
Assembly: Game (simulation/game assembly)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that periodically checks all entities with CalendarEventData and, depending on the current in-game month/time and a random roll, spawns corresponding event entities. It schedules a parallel IJobChunk (CheckEventLaunchJob) to evaluate CalendarEventData and uses an EndFrameBarrier command buffer to create event entities (PrefabRef set to the calendar event prefab). The system queries TimeSystem for normalized date/time and uses RandomSeed for deterministic randomness.
Fields
-
private const int UPDATES_PER_DAY = 4
Defines the number of time-slots per in‑game day used to bucket time when mapping normalized time into CalendarEventTimes (4 quarters per day). -
private EndFrameBarrier m_EndFrameBarrier
Reference to an EndFrameBarrier system used to create a parallel command buffer. The command buffer is used to create event entities safely from the scheduled job. -
private TimeSystem m_TimeSystem
Reference to the project's TimeSystem (fetched in OnCreate) used to read normalizedDate and normalizedTime for deciding current month/time. -
private EntityQuery m_CalendarEventQuery
EntityQuery that selects entities with CalendarEventData (read-only). The system requires this query to run. -
private TypeHandle __TypeHandle
Internal struct holding EntityTypeHandle and ComponentTypeHandle instances used by the job and assigned in OnCreateForCompiler / __AssignHandles. Used to obtain the correct handles for job scheduling. -
private struct CheckEventLaunchJob
(nested)
Burst-compiled IJobChunk that does the per-chunk work: reads Entity, CalendarEventData and EventData arrays, evaluates allowed months/times and occurrence probability, and creates event entities via the command buffer when conditions match. -
private struct TypeHandle
(nested)
Helper struct containing cached type handles: EntityTypeHandle __Unity_Entities_Entity_TypeHandle
ComponentTypeHandle<EventData> __Game_Prefabs_EventData_RO_ComponentTypeHandle
ComponentTypeHandle<CalendarEventData> __Game_Prefabs_CalendarEventData_RO_ComponentTypeHandle
Provides __AssignHandles(ref SystemState) to initialize the handles.
Properties
- None (no public properties are declared by this system).
Constructors
public CalendarEventLaunchSystem()
Default constructor (marked with [Preserve] in other lifecycle methods). The system performs actual initialization in OnCreate and OnCreateForCompiler.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns a large interval (65536) indicating the system's update interval relative to the given phase. In the source this always returns 65536. -
[Preserve] protected override void OnCreate()
Sets up runtime references and queries: - Retrieves/creates TimeSystem and EndFrameBarrier from the World.
- Creates the calendar event query (ReadOnly
). -
Calls RequireForUpdate with the calendar event query so the system only runs when CalendarEventData exists.
-
[Preserve] protected override void OnUpdate()
Main update: - Computes the current CalendarEventMonths and CalendarEventTimes from TimeSystem.normalizedDate and normalizedTime by flooring and shifting bit flags:
- month = (1 << floor(normalizedDate * 12))
- time = (1 << floor(normalizedTime * 4))
- Prepares a CheckEventLaunchJob with:
- Entity/component type handles obtained via InternalCompilerInterface and the cached __TypeHandle.
- RandomSeed = RandomSeed.Next()
- m_Month/m_Time set to current values
- A parallel command buffer from m_EndFrameBarrier
- Schedules the job in parallel over m_CalendarEventQuery and stores the returned JobHandle in base.Dependency.
-
Adds the JobHandle to m_EndFrameBarrier via AddJobHandleForProducer so the barrier waits for entity creation jobs.
-
private void __AssignQueries(ref SystemState state)
Internal helper used by the compiler-generated OnCreateForCompiler. It currently builds and disposes a temporary EntityQueryBuilder; primarily ensures required queries are set up for compilation. -
protected override void OnCreateForCompiler()
Compiler-time initialization: -
Calls __AssignQueries and __AssignHandles on the internal TypeHandle to prepare Entity/Component type handles used by the job.
-
private struct CheckEventLaunchJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Per-chunk execution logic (Burst-compiled): - Obtains Random seeded by unfilteredChunkIndex.
- Retrieves native arrays for Entity, CalendarEventData, and EventData for the chunk.
- Iterates elements:
- If (m_Month & CalendarEventData.m_AllowedMonths) != 0 AND (m_Time & CalendarEventData.m_AllowedTimes) != 0 AND random.NextInt(100) < CalendarEventData.m_OccurenceProbability.min then:
- Create an entity of EventData.m_Archetype via the parallel command buffer.
- Set its PrefabRef component to the calendar event prefab entity.
-
Note: uses occurrence probability minimum (m_OccurenceProbability.min) as threshold; random.NextInt(100) returns an int in [0,99].
-
private struct TypeHandle.__AssignHandles(ref SystemState state)
Assigns the entity and component type handles from the provided SystemState. Called from OnCreateForCompiler.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example of what this system does: obtain systems and require a query.
m_TimeSystem = base.World.GetOrCreateSystemManaged<TimeSystem>();
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_CalendarEventQuery = GetEntityQuery(ComponentType.ReadOnly<CalendarEventData>());
RequireForUpdate(m_CalendarEventQuery);
}
[Preserve]
protected override void OnUpdate()
{
// Compute current flags from TimeSystem
CalendarEventMonths month = (CalendarEventMonths)(1 << Mathf.FloorToInt(m_TimeSystem.normalizedDate * 12f));
CalendarEventTimes time = (CalendarEventTimes)(1 << Mathf.FloorToInt(m_TimeSystem.normalizedTime * 4f));
// Set up and schedule the chunk job
var jobData = new CheckEventLaunchJob {
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_EventType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Prefabs_EventData_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_CalendarEventType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Prefabs_CalendarEventData_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_Month = month,
m_Time = time,
m_RandomSeed = RandomSeed.Next(),
m_CommandBuffer = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter()
};
base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_CalendarEventQuery, base.Dependency);
m_EndFrameBarrier.AddJobHandleForProducer(base.Dependency);
}
Notes and tips: - The job is Burst-compiled and scheduled in parallel; ensure component data is set up and EventData.m_Archetype and CalendarEventData fields (m_AllowedMonths, m_AllowedTimes, m_OccurenceProbability) are valid. - The random seed is created per-schedule (RandomSeed.Next()), and the job derives per-chunk randomness using the unfiltered chunk index to keep results deterministic across runs that use the same seed. - The system uses bitflags for months/times; ensure CalendarEventMonths/CalendarEventTimes enums are defined with bit shifts matching expectations (1<<0 .. 1<<11 for months, 1<<0 .. 1<<3 for times).