Game.InDangerSystem
Assembly:
Assembly-CSharp (typical Unity game assembly; adjust if your project uses a different assembly)
Namespace:
Game.Simulation
Type:
Class
Base:
GameSystemBase
Summary:
InDangerSystem is a simulation system that manages entities marked with the InDanger component. It runs on a fixed update interval (64 frames) and schedules a Burst-compiled IJobChunk (InDangerJob) to:
- Remove the InDanger component when the danger has ended (either by the InDanger end-frame expiring or the associated Duration event ending).
- When an entity's InDanger flags indicate it should evacuate (Evacuate | UseTransport | WaitingCitizens) and it does not already have an EvacuationRequest, the system creates a ServiceRequest entity with EvacuationRequest and RequestGroup components to kick off evacuation handling.
The system uses SimulationSystem.frameIndex to compare against recorded end frames and uses an EndFrameBarrier to issue entity commands safely from the job.
Fields
-
public const uint UPDATE_INTERVAL
Constant update interval (64). The system overrides GetUpdateInterval to run every 64 frames. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current simulation frame (frameIndex) for expiration checks. -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create an EntityCommandBuffer (AsParallelWriter) so the job can perform structural changes (remove InDanger, create EvacuationRequest entities, add EffectsUpdated, etc.) safely. -
private EntityQuery m_InDangerQuery
EntityQuery matching entities with InDanger (ReadWrite) and excluding Deleted and Temp. This query is required for the system to update. -
private EntityArchetype m_EvacuationRequestArchetype
Archetype used when creating a ServiceRequest entity that carries EvacuationRequest and RequestGroup components. -
private TypeHandle __TypeHandle
Internal struct containing cached EntityTypeHandle, ComponentTypeHandleand read-only ComponentLookup handles for Duration and EvacuationRequest used by the job.
Properties
- (none public)
Constructors
public InDangerSystem()
Default constructor. The system sets up required references and query in OnCreate rather than in the ctor.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 64 — the system update interval in frames. This controls how often the system is considered for update. -
[Preserve] protected override void OnCreate()
Initializes the system: - Gets SimulationSystem and EndFrameBarrier from the world.
- Constructs the m_InDangerQuery (InDanger read/write, exclude Deleted/Temp).
- Creates m_EvacuationRequestArchetype (ServiceRequest, EvacuationRequest, RequestGroup).
- Calls RequireForUpdate(m_InDangerQuery) so the system only runs if matching entities exist.
-
Includes an Assert.IsTrue(condition: true) call (likely placeholder or compile-time check).
-
[Preserve] protected override void OnUpdate()
Schedules the Burst-compiled InDangerJob (IJobChunk) to process the InDangerQuery in parallel. The job is provided: - EntityTypeHandle and ComponentTypeHandle
for chunk iteration. - ComponentLookup
and ComponentLookup (read-only) for existence/lookups. - Current simulation frame (m_SimulationSystem.frameIndex).
- The evacuation request archetype.
-
A parallel EntityCommandBuffer created from the EndFrameBarrier. After scheduling the job, the method adds its handle to the EndFrameBarrier as a producer and sets the system base.Dependency to the returned JobHandle.
-
private void __AssignQueries(ref SystemState state)
Compiler/authoring helper used by OnCreateForCompiler; sets up any additional queries for compiler paths (no runtime query logic beyond what's in OnCreate). -
protected override void OnCreateForCompiler()
Helper used by compiled systems — calls __AssignQueries and TypeHandle.__AssignHandles to prepare type handles for the job.
Nested types and job methods (summary):
[BurstCompile] private struct InDangerJob : IJobChunk
-
Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates entities in the chunk:- Reads the InDanger component for each entity.
- Calls IsStillInDanger to check if the InDanger should remain (compares simulation frame and Duration component end frame).
- If not still in danger: clears flags, removes InDanger component and adds EffectsUpdated via the command buffer.
- If flags indicate evacuation flow (Evacuate | UseTransport | WaitingCitizens) and the EvacuationRequest component/entity does not exist, creates a new ServiceRequest entity (using the archetype), sets EvacuationRequest and RequestGroup components on it.
- Writes the possibly-updated InDanger back to the chunk's component array.
-
private bool IsStillInDanger(ref InDanger inDanger)
Returns true when both the InDanger.m_EndFrame is greater than m_SimulationFrame and the associated Duration component exists and its m_EndFrame is greater than the m_SimulationFrame. If the referenced Duration component is missing or either end-frame has passed, the danger is considered finished. -
private void RequestEvacuationIfNeeded(int jobIndex, Entity entity, ref InDanger inDanger)
Creates a ServiceRequest entity (using m_EvacuationRequestArchetype) and sets EvacuationRequest(entity, 1f) and RequestGroup(4u) if the referenced EvacuationRequest component/entity does not already exist.
Notes on implementation: - Uses InternalCompilerInterface helpers to obtain the correct handles from cached TypeHandle when scheduling jobs. - Uses EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() to perform structural changes and component additions/removals from a parallel job safely. - The EvacuationRequest creation ensures that only one evacuation request is created for an entity that is already awaiting evacuation.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// typical initialization that InDangerSystem performs:
m_SimulationSystem = World.GetOrCreateSystemManaged<SimulationSystem>();
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
// build query for InDanger components (ReadWrite), excluding Deleted/Temp
m_InDangerQuery = GetEntityQuery(
ComponentType.ReadWrite<InDanger>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>());
// create archetype used when issuing evacuation requests
m_EvacuationRequestArchetype = EntityManager.CreateArchetype(
ComponentType.ReadWrite<ServiceRequest>(),
ComponentType.ReadWrite<EvacuationRequest>(),
ComponentType.ReadWrite<RequestGroup>());
RequireForUpdate(m_InDangerQuery);
}
Additional example: creating an InDanger on an entity (to be processed by this system)
var e = EntityManager.CreateEntity();
EntityManager.AddComponentData(e, new InDanger {
m_Flags = DangerFlags.Evacuate | DangerFlags.UseTransport | DangerFlags.WaitingCitizens,
m_Event = someEventEntity, // entity referencing a Duration component
m_EndFrame = (uint)(simulationFrame + 200),
m_EvacuationRequest = Entity.Null
});
If that entity meets the evacuation-flag conditions and no EvacuationRequest exists, InDangerSystem's job will create a ServiceRequest entity with EvacuationRequest and RequestGroup components to start evacuation handling.