Skip to content

Game.Events.AddCriminalSystem

Assembly: Game
Namespace: Game.Events

Type: class

Base: GameSystemBase

Summary: System that processes AddCriminal components produced by events and ensures the appropriate Criminal component is added or merged on target entities. It collects all AddCriminal entries, validates targets using PrefabRef, merges multiple criminal intents for the same target (respecting Prisoner flags and event precedence), updates per-event TargetElement buffers, and issues component additions/updates through a ModificationBarrier4 command buffer. The heavy work runs inside a Burst-compiled IJob (AddCriminalJob) and uses native collections (NativeParallelHashMap, NativeList, NativeArray) for parallel-safe aggregation and merging.


Fields

  • private ModificationBarrier4 m_ModificationBarrier Handles deferred structural changes via a command buffer created for the job producer. The system uses this barrier to create an EntityCommandBuffer for adding Criminal components at the end of the job.

  • private EntityQuery m_AddCriminalQuery Query selecting entities that carry an Event and an AddCriminal component. This query is required for the system to run (RequireForUpdate) and is used to gather chunks to process.

  • private TypeHandle __TypeHandle Container for component type/lookups used by the job (ComponentTypeHandle, ComponentLookup, ComponentLookup, BufferLookup). __TypeHandle.__AssignHandles is called to populate handles before scheduling the job.

Properties

  • None.
    This system does not expose public properties; it relies on the EntityQuery, internal type handles and the modification barrier for its operation.

Constructors

  • public AddCriminalSystem() Default (preserved) constructor. Typical systems initialize internal state in OnCreate; the constructor itself does not perform complex initialization.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: obtains the ModificationBarrier4 from the world, builds the EntityQuery for Event + AddCriminal, and calls RequireForUpdate to ensure the system only runs when matching entities exist.

  • protected override void OnUpdate() : System.Void
    Main update path. Converts the AddCriminal query to an ArchetypeChunk list asynchronously, fills an AddCriminalJob with chunk data, component handles/lookups and a command buffer, schedules the job with dependencies, disposes of temporary chunk list (chained to job completion), registers the job with the modification barrier, and assigns the scheduled job to the system dependency.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper used to assign/initialize queries for compilation; in this implementation it creates an EntityQueryBuilder temporarily (no user-visible query assignment beyond OnCreate).

  • protected override void OnCreateForCompiler() : System.Void
    Compiler-time initialization that calls __AssignQueries and assigns component handles by calling __TypeHandle.__AssignHandles. Present to support generated/compiled ECS boilerplate.

Nested/Job methods (inside AddCriminalJob):

  • public void Execute() : System.Void
    Burst-compiled job entry point. Steps performed:
  • Count total entities across chunks to size a NativeParallelHashMap.
  • Iterate AddCriminal components in chunks. For each AddCriminal:
    • Check if target entity has a PrefabRef (valid target).
    • Build a Criminal instance from the AddCriminal data.
    • Merge with any existing pending Criminal in the NativeParallelHashMap (or with existing Criminal component on the entity if present) using MergeCriminals.
  • If no resulting criminals, exit early.
  • For each target key in the hash map:
    • If the entity already has a Criminal component, merge/replace it and optionally add the target entity to the TargetElement buffer on the associated event entity.
    • If the entity has no Criminal component, add the Criminal via the command buffer and optionally add the target entity to the TargetElement buffer on the associated event entity.
  • Uses CollectionUtils.TryAddUniqueValue to avoid duplicate TargetElement entries.

  • private Criminal MergeCriminals(Criminal criminal1, Criminal criminal2) : Criminal
    Merging policy used when multiple AddCriminal entries target the same entity:

  • If Prisoner flag differs between the two (XOR), prefer the one that is a prisoner (doesn't remove prisoner status if set by either).
  • If one criminal points to a non-null event entity while the other does not, prefer the one with a non-null event entity and OR in the other's flags.
  • Otherwise, combine flags (bitwise OR) and prefer the first criminal as base.

Usage Example

Demonstrates creating an AddCriminal "event" entity so AddCriminalSystem will process it. Adjust to your mod code, entity creation flow and available helper methods.

// Example: create an event entity and attach an AddCriminal component so
// AddCriminalSystem will add/merge a Criminal component to the target.

EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;

// Create a lightweight event entity (assumes Game.Common.Event is a tag component)
Entity eventEntity = em.CreateEntity();
em.AddComponent<Game.Common.Event>(eventEntity);

// Prepare a target entity (this should be a valid prefab-backed entity that has PrefabRef)
Entity targetEntity = /* obtain or create your target entity */;

// Add the AddCriminal component to the event entity.
// Fields used by the system: m_Target (entity to mark), m_Event (the event entity),
// m_Flags (CriminalFlags specifying prisoner/other state).
em.AddComponentData(eventEntity, new Game.Events.AddCriminal {
    m_Target = targetEntity,
    m_Event = eventEntity,
    m_Flags = Game.Citizens.CriminalFlags.None
});

// When AddCriminalSystem runs, it will:
// - validate targetEntity has a PrefabRef,
// - create/merge a Criminal component on targetEntity,
// - add a TargetElement(targetEntity) to the Target buffer on the event entity (if present),
// - perform structural changes via the ModificationBarrier4 command buffer.

Notes: - AddCriminalSystem expects AddCriminal components to be present together with an Event component (see its EntityQuery). Clean up or remove event entities as appropriate to avoid repeated processing. - The system uses Burst and native containers to aggregate merges; modifying Criminal/TargetElement behavior may require adjusting TypeHandle lookups or the job logic.