Skip to content

Game.DamagedVehicleSystem

Assembly:
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Monitors stopped vehicles that are damaged or destroyed and ensures a maintenance request exists when needed. The system runs at a coarse interval (512 frames) and uses a Burst-compiled IJobChunk (DamagedVehicleJob) to efficiently iterate matching entities. It creates or removes a MaintenanceConsumer component on vehicles depending on their damage/destroyed state and creates ServiceRequest/MaintenanceRequest/RequestGroup entities via an EndFrameBarrier command buffer when maintenance is required. The job uses a ComponentLookup to avoid creating duplicate maintenance requests.


Fields

  • private const uint UPDATE_INTERVAL = 512u
    Defines the system update interval (returned by GetUpdateInterval). The system is intended to run every 512 frames.

  • private EndFrameBarrier m_EndFrameBarrier
    End-of-frame barrier used to create a command buffer (AsParallelWriter) so entity structural changes (add/remove components, create request entities) are deferred and safe with parallel jobs.

  • private EntityQuery m_DamagedQuery
    Query selecting the vehicles this system processes. Built in OnCreate to require Damaged, Stopped, Car and exclude Deleted/Temp.

  • private EntityArchetype m_MaintenanceRequestArchetype
    Archetype used when creating maintenance request entities. Includes ServiceRequest, MaintenanceRequest and RequestGroup components.

  • private TypeHandle __TypeHandle
    Compiler-generated helper struct that caches EntityTypeHandle, ComponentTypeHandles and the ComponentLookup used inside the job. Assigned in OnCreateForCompiler.

Properties

  • This system exposes no public properties.

Constructors

  • public DamagedVehicleSystem()
    Default constructor. Marked with [Preserve] in the source so it is retained by code-stripping. Initialization of handles and queries occurs in OnCreate / OnCreateForCompiler.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the system update interval: 512 frames.

  • [Preserve] protected override void OnCreate()
    Sets up the system:

  • Retrieves/creates the EndFrameBarrier.
  • Builds m_DamagedQuery: requires Damaged, Stopped, Car and excludes Deleted and Temp.
  • Creates m_MaintenanceRequestArchetype with ServiceRequest, MaintenanceRequest and RequestGroup components.
  • Calls RequireForUpdate(m_DamagedQuery) so the system runs only when matching entities exist.
  • Contains an Assert.IsTrue(true) call present in the original source (no-op).

  • [Preserve] protected override void OnUpdate()
    Creates and schedules the Burst-compiled DamagedVehicleJob in parallel over m_DamagedQuery. The job is provided:

  • Entity and component type handles (via InternalCompilerInterface.Get* helpers).
  • ComponentLookup to check for existing requests.
  • The maintenance request archetype.
  • A parallel command buffer created from m_EndFrameBarrier. After scheduling, the job handle is registered with m_EndFrameBarrier (AddJobHandleForProducer) and stored in base.Dependency.

  • protected override void OnCreateForCompiler()
    Compiler helper: calls __AssignQueries and assigns handles via __TypeHandle.__AssignHandles. Used during codegen/compilation patterns.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler-generated stub used during system creation. In the original source it creates and disposes an EntityQueryBuilder(Allocator.Temp) (no runtime effect).

  • Nested: private struct TypeHandle
    Holds EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle and ComponentLookup. Provides __AssignHandles(ref SystemState) to initialize these handles from a SystemState.

  • Nested & important: private struct DamagedVehicleJob : IJobChunk (BurstCompile)
    Job details:

  • Fields: EntityTypeHandle, ComponentTypeHandles for Destroyed, MaintenanceConsumer, Damaged; ComponentLookup; EntityArchetype m_MaintenanceRequestArchetype; EntityCommandBuffer.ParallelWriter m_CommandBuffer.
  • Execute logic: For each chunk it obtains arrays for Entity, Destroyed, MaintenanceConsumer and Damaged and then:
    • If chunk contains Destroyed components: if destroyed.m_Cleared < 1f request maintenance or remove MaintenanceConsumer if cleared.
    • Else if chunk contains Damaged components: if any damage > 0 request maintenance or remove MaintenanceConsumer if no damage.
    • When a vehicle needs maintenance and does not already have a MaintenanceConsumer, the job either adds a MaintenanceConsumer (via m_CommandBuffer.AddComponent) or removes it.
  • RequestMaintenanceIfNeeded(jobIndex, entity, maintenanceConsumer): calls m_MaintenanceRequestData.HasComponent(maintenanceConsumer.m_Request) to check if a MaintenanceRequest entity already exists. If not, it creates one using m_MaintenanceRequestArchetype and sets:
    • MaintenanceRequest(entity, 100)
    • RequestGroup(32u)
  • The job uses the command buffer to safely create entities and add/remove components from a parallel job.

Notes about the job: - Burst compiled for performance. - Uses ComponentLookup.HasComponent to prevent duplicate requests. - Creates ServiceRequest/MaintenanceRequest/RequestGroup entities with fixed parameter values in the source (100 and 32).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical setup: cache the end-frame barrier and build the query/archetype as in the system.
    m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
    m_DamagedQuery = GetEntityQuery(
        ComponentType.ReadOnly<Damaged>(),
        ComponentType.ReadOnly<Stopped>(),
        ComponentType.ReadOnly<Car>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>()
    );
    m_MaintenanceRequestArchetype = EntityManager.CreateArchetype(
        ComponentType.ReadWrite<ServiceRequest>(),
        ComponentType.ReadWrite<MaintenanceRequest>(),
        ComponentType.ReadWrite<RequestGroup>()
    );
    RequireForUpdate(m_DamagedQuery);
}

Additional notes for modders: - The system runs relatively rarely (every 512 frames). If you need faster response times for a custom behavior, override GetUpdateInterval or create a separate system. - Maintenance request defaults are hard-coded in the job: MaintenanceRequest(entity, 100) and RequestGroup(32u). To change request priority or group size, modify the job or intercept created requests elsewhere. - The system creates/removes the MaintenanceConsumer component to mark vehicles that require maintenance. You can read/observe that component elsewhere to influence vehicle routing or dispatching. - Because entity structural changes are enqueued via EndFrameBarrier and an AsParallelWriter command buffer, avoid making structural changes directly from the job; instead replicate the pattern used here.