Game.AddAccidentSiteSystem
Assembly: Assembly-CSharp
Namespace: Game.Events
Type: class
Base: GameSystemBase
Summary:
System that collects AddAccidentSite components produced elsewhere in the simulation and applies them to target entities as AccidentSite components. Work is performed in a Burst-compiled IJob (AddAccidentSiteJob) that:
- reads all entities with AddAccidentSite,
- groups/merges multiple accident additions per target entity,
- applies/updates AccidentSite components on target entities via a command buffer,
- optionally appends the target to the event's TargetElement buffer when appropriate,
- reduces CrimeProducer.m_Crime by a multiplier (0.3) for crime-scene accidents on entities that have CrimeProducer.
The system uses SimulationSystem.frameIndex for timestamping and runs its modifications through a ModificationBarrier4.
Fields
-
private SimulationSystem m_SimulationSystem
This holds a reference to the global SimulationSystem used to read the current simulation frame (frameIndex). It is assigned in OnCreate(). -
private ModificationBarrier4 m_ModificationBarrier
A barrier system used to obtain an EntityCommandBuffer for safely adding components from jobs and to register job dependencies with the barrier. -
private EntityQuery m_ImpactQuery
Entity query that selects entities with AddAccidentSite (and the associated Event component). The query is required for update, so the system only runs when there are pending AddAccidentSite components. -
private TypeHandle __TypeHandle
Container for component type/lookup handles used by the job (component type handle for AddAccidentSite, and component lookups/buffer lookups for PrefabRef, AccidentSite, CrimeProducer and TargetElement). Its __AssignHandles method wires these handles to the current SystemState. -
private struct AddAccidentSiteJob
(nested)
Burst-compiled job that performs the main work. It: - Iterates through chunk lists produced from m_ImpactQuery,
- Builds a temporary NativeParallelHashMap
to merge multiple incoming AddAccidentSite entries per target, - Checks PrefabRef presence to ensure targets are valid prefab entities,
- When merging, keeps the event reference and accumulates flags (via MergeAccidentSites),
- If an existing AccidentSite component exists it merges and updates it; otherwise it adds a new AccidentSite component using the command buffer,
- When an accident has the CrimeScene flag and a CrimeProducer exists on the target, multiplies the crime value by 0.3f,
- Adds the target Entity into the event's TargetElement buffer if that buffer exists and the event changed. The job is scheduled from OnUpdate and runs with proper dependency chaining via the modification barrier.
Properties
- None (this system exposes no public properties)
Constructors
public AddAccidentSiteSystem()
Parameterless constructor. Marked with [Preserve] in the compiled type and used to construct the managed system instance. Initialization that requires World/System references occurs in OnCreate().
Methods
protected override void OnCreate()
Initializes the system:- Retrieves SimulationSystem and ModificationBarrier4 from the World,
- Creates the EntityQuery for AddAccidentSite & Event,
-
Calls RequireForUpdate(m_ImpactQuery) so the system only runs when there are matching entities.
-
protected override void OnUpdate()
Schedules the AddAccidentSiteJob: - Produces an asynchronous ArchetypeChunk list from m_ImpactQuery,
- Creates and populates the job struct with component type/lookup handles, current simulation frame, a command buffer from the modification barrier, etc.,
- Schedules the job combining base.Dependency and the chunk list request's JobHandle,
- Disposes the chunk list once the scheduled job is complete,
-
Registers the job with m_ModificationBarrier via AddJobHandleForProducer and updates base.Dependency.
-
protected override void OnCreateForCompiler()
Compiler helper used to assign queries and component handles when compiling to DOTS-friendly form. Calls __AssignQueries and assigns handles on __TypeHandle. -
private void __AssignQueries(ref SystemState state)
Internal/generated helper that sets up any additional queries needed by the system. In this build it creates an EntityQueryBuilder(Allocator.Temp).Dispose() — a placeholder in generated code. -
private struct TypeHandle
(nested)
Provides the stored ComponentTypeHandle/ComponentLookup/BufferLookup fields the job needs and an __AssignHandles method that grabs the actual handles from a given SystemState. -
private AccidentSite MergeAccidentSites(AccidentSite accidentSite1, AccidentSite accidentSite2)
(in AddAccidentSiteJob)
Merges two AccidentSite instances deterministically: - If exactly one of the two has a non-null m_Event, it preferentially preserves the non-null event as the resulting event and ORs in the flags from the other,
- Otherwise it keeps the first AccidentSite and ORs in flags from the second. This ensures multiple AddAccidentSite contributions for the same target are consolidated before being applied.
Usage Example
// Example (illustrative): schedule an AddAccidentSite for a target entity.
// Types and field names are taken from the compiled system usage: AddAccidentSite has m_Target, m_Event and m_Flags.
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
// create/populate an event entity elsewhere in your code, then:
Entity targetEntity = /* some building/prefab entity */;
Entity eventEntity = /* the event entity representing the accident */;
AddAccidentSite add = new AddAccidentSite {
m_Target = targetEntity,
m_Event = eventEntity,
m_Flags = AccidentSiteFlags.None // or AccidentSiteFlags.CrimeScene, etc.
};
// Add the AddAccidentSite component to queue it for processing by AddAccidentSiteSystem:
entityManager.AddComponentData(someProducerEntity, add);
Notes and tips: - The heavy lifting is done inside a Burst-compiled job (AddAccidentSiteJob) and uses a command buffer from ModificationBarrier4 to perform structural changes safely from a background thread. - The system merges multiple AddAccidentSite entries per target in-memory before applying them to reduce churn and avoid race conditions. - If an accident is flagged as a CrimeScene and the target has a CrimeProducer component, the system multiplies that producer's crime value by 0.3 (reducing crime contribution). - Because the system requires the AddAccidentSite entity query to be non-empty to run, producers should add one AddAccidentSite component to an entity to request processing; the system will remove or update AccidentSite components on target entities as needed.