Skip to content

Game.Events.FaceWeatherSystem

Assembly: Assembly-CSharp (game runtime)
Namespace: Game.Events

Type: class

Base: GameSystemBase

Summary:
FaceWeatherSystem scans entities carrying FaceWeather events and applies/updates FacingWeather components on target entities (typically prefabs/buildings). It aggregates FaceWeather entries per target, keeping the highest-severity event per target, updates FacingWeather components (adding them when missing), and enqueues AddEventJournalData for buildings when the facing weather event changes. The system uses a Burst-compiled IJob to process chunks in parallel, NativeParallelHashMap for aggregation, and a ModificationBarrier4 command buffer to perform structural changes safely from the job.


Fields

  • private ModificationBarrier4 m_ModificationBarrier
    Used to obtain an EntityCommandBuffer for making structural changes (adding components, creating entities) from the scheduled job, and to correctly manage producer job handles with the main world.

  • private EntityQuery m_FaceWeatherQuery
    EntityQuery selecting entities with FaceWeather and Game.Common.Event (read-only) that drive this system's updates. The query is required for update via RequireForUpdate.

  • private EntityArchetype m_JournalDataArchetype
    Archetype used to create journal-data entities that carry AddEventJournalData and Game.Common.Event components whenever a building needs journal tracking for a weather event.

  • private TypeHandle __TypeHandle
    Holds component type handles and lookup wrappers (ComponentTypeHandle / ComponentLookup / BufferLookup) for the job. It's assigned in OnCreateForCompiler and used to obtain the correct runtime handles for job scheduling.

  • private struct FaceWeatherJob (nested)
    Burst-compiled IJob that does the heavy lifting: iterates chunks of FaceWeather components, aggregates highest-severity FacingWeather per target using a NativeParallelHashMap, then writes or updates FacingWeather components on target entities, updates target event buffers (TargetElement) where applicable, and creates journal data entities for buildings.

  • private struct TypeHandle (nested)
    Container struct that stores all ComponentTypeHandle/ComponentLookup/BufferLookup fields and provides an __AssignHandles method to populate them from a SystemState. This pattern is used to cache handles for burst safety and performance.

Properties

  • None (no public properties exposed by this system)

Constructors

  • public FaceWeatherSystem()
    Default constructor. The class uses [Preserve] on lifecycle methods; constructor itself does not perform initialization beyond the base.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: fetches/creates the ModificationBarrier4, builds the m_FaceWeatherQuery (FaceWeather + Game.Common.Event), creates the m_JournalDataArchetype (AddEventJournalData + Game.Common.Event), and calls RequireForUpdate(m_FaceWeatherQuery) so the system runs only when relevant entities exist.

  • protected override void OnUpdate() : System.Void
    Main update entry. It:

  • Asynchronously collects archetype chunks matching m_FaceWeatherQuery into a NativeList.
  • Schedules the Burst-compiled FaceWeatherJob with those chunks and component handles/lookups obtained from __TypeHandle via InternalCompilerInterface.
  • Disposes the chunk list once the scheduled job completes and registers the job handle with the ModificationBarrier4 (AddJobHandleForProducer).
  • Updates system Dependency with the scheduled job handle.

Important behaviors: - The job receives a command buffer produced by m_ModificationBarrier.CreateCommandBuffer() so structural changes are deferred safely. - JobHandle.CombineDependencies is used to combine base.Dependency and the chunk-fetch dependency.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Called from OnCreateForCompiler to set up or verify any queries needed by the compiler/runtime. In this class it currently instantiates and disposes an EntityQueryBuilder(Allocator.Temp) placeholder (generated pattern).

  • protected override void OnCreateForCompiler() : System.Void
    Compiler-time helper; calls __AssignQueries and __TypeHandle.__AssignHandles to ensure the component handles and queries are set up for the job pipeline.

Nested job methods (inside FaceWeatherJob):

  • public void Execute() : System.Void
    Executes the job logic:
  • Counts total FaceWeather entries in all chunks and creates a NativeParallelHashMap sized accordingly.
  • For each FaceWeather entry:
    • Skips if the face's target doesn't have a PrefabRef component (i.e., not a prefab instance).
    • Constructs a FacingWeather entry from the FaceWeather event and severity.
    • Aggregates per target, ensuring only the highest-severity FacingWeather is kept (compares against existing map entry or existing FacingWeather component on target).
  • If no aggregated entries, returns early.
  • For each aggregated target:
    • If a FacingWeather component already exists on the target:
    • If the event changed, add the target to the event's TargetElement buffer (if present) and call AddJournalData to enqueue journal creation.
    • Update the FacingWeather component data to the aggregated value.
    • If no FacingWeather component exists:
    • Add the target to the event's TargetElement buffer (if present), then AddComponent via the command buffer and call AddJournalData.

Notes: - Uses CollectionUtils.TryAddUniqueValue to avoid duplicate TargetElement entries. - Uses command buffer for AddComponent and CreateEntity/SetComponent operations.

  • private void AddJournalData(FacingWeather facingWeather, Entity target) : System.Void
    Called from the job to enqueue creation of an AddEventJournalData entity when the target is a building (has Building component). Uses the command buffer to create an entity with the m_JournalDataArchetype and sets AddEventJournalData with the event and tracking type (EventDataTrackingType.Damages).

Other generated / compiler helpers: - Methods annotated with MethodImpl/Preserve as in the source (e.g., __AssignHandles on TypeHandle, constructor, etc.) are infrastructure for runtime/IL2CPP/Burst compatibility and for the ECS codegen pattern.

Usage Example

// Example: create an entity that will cause FaceWeatherSystem to apply FacingWeather to a target.
// This snippet assumes you have access to an EntityManager and valid event and target entities.

EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;

// Create a FaceWeather event entity (this will be captured by the system's query)
Entity faceEvent = em.CreateEntity(typeof(FaceWeather), typeof(Game.Common.Event));
em.SetComponentData(faceEvent, new FaceWeather {
    m_Target = someTargetEntity,    // target entity that should receive FacingWeather
    m_Event = someEventEntity,      // event entity that represents the weather event
    m_Severity = 2                  // severity used to resolve conflicts
});

// After the frame/system runs, FaceWeatherSystem will aggregate and:
// - Add or update FacingWeather on someTargetEntity to reflect the highest-severity event
// - Optionally add the target to the event's TargetElement buffer
// - Create AddEventJournalData entity if the target has a Building component

Notes and modding considerations: - The system uses Burst for the FaceWeatherJob; ensure any custom structs or components are Burst-compatible if you plan to adapt or copy job logic. - Structural changes (AddComponent/CreateEntity) are done via ModificationBarrier4's command buffer; do not attempt direct structural changes from the job. - The system requires FaceWeather and Game.Common.Event components on source entities to operate; targets must have PrefabRef (to be considered valid) and may have Building and/or TargetElement buffers for additional behavior. - If you modify the involved components (FaceWeather, FacingWeather, TargetElement, AddEventJournalData), maintain binary-compatible layouts or adjust code accordingly.