Game.NotificationTriggerSystem
Assembly: Assembly-CSharp (game managed assembly)
Namespace: Game.Triggers
Type: class
Base: GameSystemBase
Summary:
NotificationTriggerSystem is an ECS system that detects notification entities (entities carrying an Icon and a PrefabRef) being created or resolved and enqueues corresponding TriggerAction entries into the global TriggerSystem. It gathers three entity queries (created/applied notifications, deleted notifications, and all active notifications), packages entity and prefab data into NativeArrays, and schedules a Burst-compiled IJob (TriggerJob) to read Owner/Target components, count occurrences of the same notification prefab, and push TriggerAction items into a TriggerSystem action buffer. The system is initially disabled and enabled only in game mode during preload. It uses ComponentLookup (via a generated TypeHandle) and safely passes data to the job using DeallocateOnJobCompletion and TempJob allocators.
Fields
-
private TriggerSystem m_TriggerSystem
Holds a reference to the game's TriggerSystem. Used to create a NativeQueue/ action buffer and to register the job dependency so trigger actions produced by this system are seen by the TriggerSystem. -
private EntityQuery m_CreatedNotificationsQuery
EntityQuery selecting newly created or applied notification entities: requires Icon and PrefabRef, requires Created or Applied, and excludes Temp and Deleted. -
private EntityQuery m_DeletedNotificationsQuery
EntityQuery selecting notification entities that were deleted (Icon + PrefabRef + Deleted) and excluding Temp. Used to generate NotificationResolved trigger actions. -
private EntityQuery m_AllNotificationsQuery
EntityQuery selecting all active notifications (Icon + PrefabRef) excluding Temp and Deleted. Used by the job to count how many instances of a particular notification prefab currently exist. -
private TypeHandle __TypeHandle
Generated helper struct that stores ComponentLookupand ComponentLookup prepared for read-only access inside the job. Populated in OnCreateForCompiler.
Properties
- None (this system does not expose public properties).
Constructors
public NotificationTriggerSystem()
Default constructor. Marked with Preserve attribute in code and performs no additional runtime initialization; actual initialization is done in OnCreate.
Methods
-
protected override void OnCreate()
Initializes the system: obtains the TriggerSystem from the world, creates the three EntityQuery objects (created/applied, deleted, all active notifications), and disables the system by default (base.Enabled = false). This method prepares the queries used later in OnUpdate. -
protected override void OnGamePreload(Purpose purpose, GameMode mode)
Called during game preload. Enables or disables the system depending on the provided GameMode: base.Enabled is set to mode.IsGame(), ensuring the system runs only during actual gameplay. -
protected override void OnUpdate()
Main update method: constructs a TriggerJob instance, populates it with NativeArrays retrieved from the three EntityQueries (allocated with Allocator.TempJob) and with component lookups obtained via the compiled TypeHandle, and creates an action buffer from m_TriggerSystem. Schedules the job (Burst-compiled) and sets base.Dependency accordingly. Finally, registers the job dependency with m_TriggerSystem using AddActionBufferWriter so the TriggerSystem knows when the action buffer is ready. -
private void __AssignQueries(ref SystemState state)
Compiler helper used during OnCreateForCompiler; in this implementation it constructs and disposes an EntityQueryBuilder. Present for compiler-generated setup; does not change runtime semantics beyond assisting code generation. -
protected override void OnCreateForCompiler()
Compiler helper invoked to assign queries and component lookups (via __AssignQueries and the TypeHandle.__AssignHandles). Ensures the generated TypeHandle has its ComponentLookup instances bound to the SystemState before jobs run. -
Nested type:
private struct TriggerJob : IJob
- Burst-compiled job that runs on worker threads. It receives:
- NativeArray
m_Created, m_Deleted (entities for created/applied and deleted notifications). - NativeArray
m_CreatedPrefabRefs, m_DeletedPrefabRefs, m_AllPrefabRefs (corresponding prefab references). - ComponentLookup
m_OwnerData and ComponentLookup m_TargetData for reading owner/target info. - NativeQueue
m_ActionQueue to enqueue TriggerAction items.
- NativeArray
- Execute(): enqueues TriggerAction entries for both new notifications (TriggerType.NewNotification) and resolved notifications (TriggerType.NotificationResolved) by calling the internal Execute overload.
- Execute(NativeArray
, NativeArray , TriggerType): iterates arrays, attempts to read Owner and Target components for each entity, counts how many instances of the notification prefab exist (via Count), and enqueues a TriggerAction with (triggerType, prefab, ownerEntity or Entity.Null, targetEntity or Entity.Null, count). - Count(Entity notification): iterates m_AllPrefabRefs to count matching prefab occurrences.
-
Notes: input NativeArrays are marked with DeallocateOnJobCompletion where appropriate to automatically free them after the job finishes.
-
Nested type:
private struct TypeHandle
- Holds read-only ComponentLookup
and ComponentLookup . - __AssignHandles(ref SystemState state): fills the ComponentLookup fields from the provided SystemState (called from OnCreateForCompiler) using state.GetComponentLookup
(isReadOnly: true). - Purpose: centralize and cache the ComponentLookup instances used by the Burst job.
Usage Example
The system itself constructs and schedules a Burst job in OnUpdate that enqueues TriggerAction entries into the TriggerSystem. A condensed excerpt showing how the job is prepared and scheduled:
[Preserve]
protected override void OnUpdate()
{
var jobData = new TriggerJob
{
m_Created = m_CreatedNotificationsQuery.ToEntityArray(Allocator.TempJob),
m_CreatedPrefabRefs = m_CreatedNotificationsQuery.ToComponentDataArray<PrefabRef>(Allocator.TempJob),
m_Deleted = m_DeletedNotificationsQuery.ToEntityArray(Allocator.TempJob),
m_DeletedPrefabRefs = m_DeletedNotificationsQuery.ToComponentDataArray<PrefabRef>(Allocator.TempJob),
m_AllPrefabRefs = m_AllNotificationsQuery.ToComponentDataArray<PrefabRef>(Allocator.TempJob),
m_OwnerData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Common_Owner_RO_ComponentLookup, ref base.CheckedStateRef),
m_TargetData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Common_Target_RO_ComponentLookup, ref base.CheckedStateRef),
m_ActionQueue = m_TriggerSystem.CreateActionBuffer()
};
base.Dependency = IJobExtensions.Schedule(jobData, base.Dependency);
m_TriggerSystem.AddActionBufferWriter(base.Dependency);
}
Notes for modders: - This system runs only during gameplay (enabled in OnGamePreload when mode.IsGame()). - TriggerAction items are produced by this system and consumed by TriggerSystem; if you need to react to notification triggers, listen to or consume TriggerSystem's action buffer instead of modifying NotificationTriggerSystem. - The job uses Burst and temp job allocations; be careful to mirror its safety choices if you produce similar jobs (DeallocateOnJobCompletion, Allocator.TempJob, read-only ComponentLookup).