Game.Achievements.EventAchievementTriggerSystem
Assembly:
Game
Namespace:
Game.Achievements
Type:
class
Base:
GameSystemBase
Summary:
This system listens for newly created game events (entities with Game.Events.Event and associated PrefabRef + EventAchievement + Created tags) and starts temporary tracking entities for configured event achievements. It uses the SimulationSystem frame index and any Duration component on the original event to determine when an achievement's tracking period ends. When a tracking entry expires, the system marks it Deleted and calls PlatformManager.instance.UnlockAchievement to unlock the configured achievement. The system schedules structural changes using a ModificationEndBarrier to defer entity commands to the end of the simulation step.
Fields
-
private SimulationSystem m_SimulationSystem
This holds a reference to the SimulationSystem used to read the current frameIndex when scheduling tracking end times. -
private ModificationEndBarrier m_ModifiactionEndBarrier
Barrier system used to create EntityCommandBuffer instances for deferred structural changes (creating tracking entities, marking them Deleted, etc.). Note the field name contains a typo ("m_ModifiactionEndBarrier") matching the source. -
private EntityQuery m_TrackingQuery
Query for active EventAchievementTrackingData entities used to find tracking entries that may need to be stopped/unlocked. -
private EntityQuery m_CreatedEventQuery
Query that matches newly created event entities (requires Game.Events.Event, PrefabRef, EventAchievement, Created; excludes Temp). Used to detect events that should spawn achievement tracking entries. -
private EntityArchetype m_TrackingArchetype
Archetype used when creating tracking entities (contains EventAchievementTrackingData). -
private TypeHandle __TypeHandle
Compiler-generated struct instance that holds ComponentTypeHandle and BufferLookup handles used in OnUpdate for efficient component/buffer access. -
private struct TypeHandle
Nested compiler-generated struct holding: BufferLookup<EventAchievementData> __Game_Prefabs_EventAchievementData_RO_BufferLookup
— readonly buffer lookup to read EventAchievementData buffers from prefabs.ComponentTypeHandle<Duration> __Game_Events_Duration_RO_ComponentTypeHandle
— readonly handle to test/read Duration component on event entities.ComponentTypeHandle<PrefabRef> __Game_Prefabs_PrefabRef_RO_ComponentTypeHandle
— readonly handle to read PrefabRef from event entities. This struct also provides an __AssignHandles method to initialize the handles from a SystemState.
Properties
- None.
This system exposes no public properties; all state is private and driven by the ECS queries and attached barrier/other systems.
Constructors
public EventAchievementTriggerSystem()
Default parameterless constructor. It is decorated with [Preserve] in the source so the system is kept for IL2CPP code stripping. The actual setup of queries and references is done in OnCreate/OnCreateForCompiler rather than the constructor.
Methods
protected override void OnCreate()
Initializes system references and queries:- Acquires SimulationSystem and ModificationEndBarrier from the World.
- Creates m_CreatedEventQuery to match newly created event entities with EventAchievement tag.
-
Creates an archetype for EventAchievementTrackingData and a query to find tracking entries. This method is decorated with [Preserve] and sets up the data required by OnUpdate.
-
protected override void OnUpdate()
Main update logic executed each frame: - If there are newly created event entities (m_CreatedEventQuery not empty), it iterates the matching chunks and:
- Reads PrefabRef and optionally Duration (if present) on each event entity.
- For each EventAchievementData found in the prefab buffer, schedules a tracking entry via StartTracking with a start frame computed from either the event's Duration.startFrame or current simulation frame plus the configured frame delay.
- If there are tracking entries (m_TrackingQuery not empty), it collects their EventAchievementTrackingData and entities, then for each entry whose configured startFrame has passed (m_SimulationSystem.frameIndex > m_StartFrame), calls StopTracking to mark it Deleted and unlock the achievement.
-
Uses ModificationEndBarrier's EntityCommandBuffer to create and modify entities safely.
-
private void StartTracking(AchievementId id, uint startFrame, EntityCommandBuffer buffer)
Creates a new entity with the tracking archetype and sets EventAchievementTrackingData (m_ID and m_StartFrame) so the system can later detect expiry. All entity creation is performed on the provided command buffer. -
private void StopTracking(EventAchievementTrackingData data, Entity entity, EntityCommandBuffer buffer)
Marks the tracking entity Deleted via the command buffer and calls PlatformManager.instance.UnlockAchievement(data.m_ID) to unlock the achievement when its tracking period completes. -
private void __AssignQueries(ref SystemState state)
Compiler-generated method placeholder used in OnCreateForCompiler to ensure queries/handles can be assigned for AOT/IL2CPP compilation. The implementation in source is effectively a no-op that includes a temporary EntityQueryBuilder disposal. -
protected override void OnCreateForCompiler()
Compiler-related initialization that calls __AssignQueries and assigns the TypeHandle handles using the CheckedStateRef (used for correct AOT/IL2CPP behavior). -
private struct TypeHandle.__AssignHandles(ref SystemState state)
Initializes buffer and component type handles in the TypeHandle from the provided SystemState; called during OnCreateForCompiler to prepare the TypeHandle for use in OnUpdate.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire dependent systems
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
m_ModifiactionEndBarrier = base.World.GetOrCreateSystemManaged<ModificationEndBarrier>();
// Query for newly created events which have EventAchievement configured
m_CreatedEventQuery = GetEntityQuery(
ComponentType.ReadOnly<Game.Events.Event>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<EventAchievement>(),
ComponentType.ReadOnly<Created>(),
ComponentType.Exclude<Temp>());
// Create an archetype for per-event tracking entities
m_TrackingArchetype = base.EntityManager.CreateArchetype(ComponentType.ReadWrite<EventAchievementTrackingData>());
m_TrackingQuery = GetEntityQuery(ComponentType.ReadWrite<EventAchievementTrackingData>());
}
Additional notes:
- This system expects prefabs to carry a DynamicBuffer