Skip to content

Game.Events.IgniteSystem

Assembly:
Namespace: Game.Events

Type: class

Base: GameSystemBase

Summary: IgniteSystem is an ECS system that processes Ignite components (events that request igniting a target entity) and converts them into OnFire state on target entities. The system aggregates ignite requests per target (keeping the highest intensity), updates existing OnFire components or adds them via an EntityCommandBuffer, notifies event targets via TargetElement buffers, and creates journal event entities for buildings. The work is performed in a Burst-compiled IJob (IgniteFireJob) and uses a ModificationBarrier4 command buffer for structural changes to guarantee thread-safety and correct job dependencies.


Fields

  • private ModificationBarrier4 m_ModificationBarrier This is the system providing a command buffer to perform structural changes (AddComponent/CreateEntity/etc.) from a job producer. IgniteSystem obtains it in OnCreate and uses it to create a producer command buffer for the IgniteFireJob.

  • private EntityQuery m_IgniteQuery EntityQuery matching entities with Ignite (and Event). Used to collect archetype chunks to process ignite requests.

  • private EntityArchetype m_JournalDataArchetype Archetype for creating AddEventJournalData entities (tracking event journaling for buildings). Created in OnCreate and used by the job when it needs to spawn a journal entry.

  • private TypeHandle __TypeHandle Container for precomputed component/buffer handles (ComponentTypeHandle, ComponentLookup, BufferLookup) that are assigned once for the system and passed into the job for safe access.

  • private struct IgniteFireJob Nested Burst-compiled IJob that performs the bulk of the logic:

  • Reads Ignite components from chunks and builds a NativeParallelHashMap containing the highest-intensity OnFire per target entity.
  • Merges with existing OnFire state if present, preserving earliest requestFrame and rescueRequest flags and comparing events.
  • Adds TargetElement to the event's target buffer for newly-affected events.
  • Uses the provided EntityCommandBuffer to add OnFire and BatchesUpdated to newly affected entities.
  • Adds BatchesUpdated to installed upgrade entities when their parent building becomes on fire (so upgrades can react).
  • Creates AddEventJournalData entities (using m_JournalDataArchetype) for buildings that are newly affected.

Fields inside IgniteFireJob include: - m_Chunks (NativeList) — chunk list to iterate. - m_IgniteType (ComponentTypeHandle) — handle to read Ignite components. - m_PrefabRefData (ComponentLookup) — used to validate targets. - m_BuildingData (ComponentLookup) — used to check whether target is a building for journaling. - m_InstalledUpgrades (BufferLookup) — to update upgrade entities when a building is set on fire. - m_OnFireData (ComponentLookup) — read/write lookup for existing OnFire components. - m_TargetElements (BufferLookup) — buffer lookup to add targets to the event's runtime target list. - m_JournalDataArchetype (EntityArchetype) — archetype used to create journal entities. - m_CommandBuffer (EntityCommandBuffer) — command buffer returned by ModificationBarrier4 for structural changes.

  • private struct TypeHandle Contains component/buffer handles used by the system and a method to assign them via the SystemState. Members:
  • __Game_Events_Ignite_RO_ComponentTypeHandle : ComponentTypeHandle
  • __Game_Prefabs_PrefabRef_RO_ComponentLookup : ComponentLookup
  • __Game_Buildings_Building_RO_ComponentLookup : ComponentLookup
  • __Game_Buildings_InstalledUpgrade_RO_BufferLookup : BufferLookup
  • __Game_Events_OnFire_RW_ComponentLookup : ComponentLookup
  • __Game_Events_TargetElement_RW_BufferLookup : BufferLookup

Method: - __AssignHandles(ref SystemState state) — populates the handles from the given state.

Properties

  • None (no public properties exposed by this system).

Constructors

  • public IgniteSystem() Default constructor. The system relies on OnCreate to initialize dependencies and queries. Marked with [Preserve] on creation methods to avoid stripping.

Methods

  • protected override void OnCreate() : System.Void Creates required resources:
  • Retrieves/creates the ModificationBarrier4 system for safe structural changes from jobs.
  • Builds an EntityQuery to match Ignite + Event components.
  • Creates the m_JournalDataArchetype (AddEventJournalData + Event).
  • Calls RequireForUpdate(m_IgniteQuery) so the system only updates when there are Ignite entities.

Important notes: - This method sets up the archetype and query used by the job. - It ensures the system will be executed only when ignite events are present.

  • protected override void OnUpdate() : System.Void Main scheduling logic:
  • Converts the m_IgniteQuery into a NativeList asynchronously (ToArchetypeChunkListAsync) and obtains its dependency JobHandle (outJobHandle).
  • Constructs and schedules IgniteFireJob with the chunks and prepared component/buffer lookups via InternalCompilerInterface.GetComponentTypeHandle / GetComponentLookup / GetBufferLookup using __TypeHandle and the system's CheckedStateRef.
  • Combines dependencies between the query conversion and current system dependency, disposes the chunk list after job completion, and registers the job handle with m_ModificationBarrier via AddJobHandleForProducer.
  • Stores the scheduled job handle into base.Dependency.

Important notes: - The job is Burst-compiled and uses only thread-safe lookups and the command buffer for structural changes. - The NativeList is disposed with a dependency on the scheduled job (chunks.Dispose(jobHandle)).

  • private void __AssignQueries(ref SystemState state) : System.Void Stub used by compiler-time generated code to assign queries/initial state handles. In this implementation it only creates and disposes an EntityQueryBuilder (no runtime behavior needed beyond existing OnCreate logic). Called by OnCreateForCompiler.

  • protected override void OnCreateForCompiler() : System.Void Called by compiler-generated startup path. It calls __AssignQueries and __TypeHandle.__AssignHandles to ensure type handles are assigned when the system is created by codegen paths.

  • private struct TypeHandle.__AssignHandles(ref SystemState state) : System.Void Populates the TypeHandle's component and buffer handles using the provided SystemState. Called at initialization so handles can be fetched each frame for job construction.

  • IgniteFireJob.Execute() : System.Void (method on the nested IJob) Contains the core processing:

  • Counts total components across chunks to size the NativeParallelHashMap.
  • Iterates all Ignite components; for each ignite:
    • Validates the target has a PrefabRef (m_PrefabRefData.HasComponent).
    • Builds an OnFire candidate and either adds it to the hash map or updates an existing entry if the candidate has higher intensity.
    • If the target already has an OnFire component, the candidate is merged respecting earlier requestFrame/rescueRequest and switching events (adding the entity to the new event's target buffer and creating journal data).
  • After merging, iterates the hash map keys and:
    • Updates existing OnFire components in-place (m_OnFireData).
    • For new OnFire targets, adds OnFire and BatchesUpdated via m_CommandBuffer, and creates journal data for buildings.
    • When adding OnFire to a building, iterates its InstalledUpgrade buffer (if any) and adds BatchesUpdated to each upgrade entity to mark them for updates.
  • Uses AddJournalData helper to spawn AddEventJournalData entities for buildings.

Thread-safety and performance notes: - Uses NativeParallelHashMap to aggregate results safely inside a single-threaded job Execute (IJob), but sized based on total items to avoid reallocation. - Uses m_CommandBuffer for all structural changes so this job can be Burst-compiled and multi-thread friendly for reads. - PrefabRef presence is used to filter valid targets.

  • IgniteFireJob.AddJournalData(Entity target, OnFire onFire) : System.Void Helper that creates an entity using m_JournalDataArchetype and sets an AddEventJournalData component for building targets only.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier4>();
    m_IgniteQuery = GetEntityQuery(ComponentType.ReadOnly<Ignite>(), ComponentType.ReadOnly<Game.Common.Event>());
    m_JournalDataArchetype = base.EntityManager.CreateArchetype(
        ComponentType.ReadWrite<AddEventJournalData>(),
        ComponentType.ReadWrite<Game.Common.Event>());
    RequireForUpdate(m_IgniteQuery);
}

[Preserve]
protected override void OnUpdate()
{
    JobHandle outJobHandle;
    var chunks = m_IgniteQuery.ToArchetypeChunkListAsync(Allocator.TempJob, out outJobHandle);
    var job = new IgniteFireJob {
        m_Chunks = chunks,
        m_IgniteType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Events_Ignite_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_PrefabRefData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Prefabs_PrefabRef_RO_ComponentLookup, ref base.CheckedStateRef),
        m_BuildingData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Buildings_Building_RO_ComponentLookup, ref base.CheckedStateRef),
        m_InstalledUpgrades = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Buildings_InstalledUpgrade_RO_BufferLookup, ref base.CheckedStateRef),
        m_OnFireData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Events_OnFire_RW_ComponentLookup, ref base.CheckedStateRef),
        m_TargetElements = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Events_TargetElement_RW_BufferLookup, ref base.CheckedStateRef),
        m_JournalDataArchetype = m_JournalDataArchetype,
        m_CommandBuffer = m_ModificationBarrier.CreateCommandBuffer()
    };
    JobHandle jobHandle = IJobExtensions.Schedule(job, JobHandle.CombineDependencies(base.Dependency, outJobHandle));
    chunks.Dispose(jobHandle);
    m_ModificationBarrier.AddJobHandleForProducer(jobHandle);
    base.Dependency = jobHandle;
}

Additional notes for modders: - IgniteSystem expects Ignite components to be emitted as events (Game.Common.Event) targeting entities with PrefabRef. When creating Ignite entities from other code, ensure you set m_Target to a valid entity with a PrefabRef. - The system uses CommandBuffer to add OnFire components; if you need synchronous immediate changes, you must not depend on this system's job completion before your code runs (use dependency handles or schedule after base.Dependency). - Be careful to respect Burst and job-safe access patterns: use ComponentLookups / BufferLookups and the provided command buffer for structural writes.