Skip to content

Game.Objects.DamageSystem

Assembly: Assembly-CSharp (game code)

Namespace: Game.Objects

Type: class

Base: GameSystemBase

Summary: DamageSystem listens for Damage events (entities with Event + Damage components), accumulates per-entity damage deltas for the frame, and applies or removes the Damaged component accordingly. It batches multiple Damage events for the same target using a temporary NativeParallelHashMap inside a Burst-compiled IJob (DamageObjectsJob). Structural changes (adding/removing components like Damaged, BatchesUpdated, MaintenanceConsumer) are deferred through a ModificationBarrier2 command buffer. The system also checks for Vehicle data to remove MaintenanceConsumer when damage is cleared.


Fields

  • private ModificationBarrier2 m_ModificationBarrier Provides a barrier system used to create an EntityCommandBuffer for deferred structural changes (adding/removing components) performed by the job. This ensures changes are applied safely after job completion on the main thread.

  • private EntityQuery m_EventQuery An EntityQuery that selects entities containing Event and Damage components. The system uses this query to build an archetype chunk list of Damage events to be processed each update and calls RequireForUpdate(m_EventQuery) so the system only runs when there are matching events.

  • private TypeHandle __TypeHandle Holds component access handles used by the job: a read-only ComponentTypeHandle and ComponentLookup instances for Vehicle and Damaged. These handles are assigned in OnCreateForCompiler and then passed into the job via InternalCompilerInterface.

  • (nested) private struct DamageObjectsJob : IJob Burst-compiled job which:

  • Iterates all chunks of the Damage event query.
  • Merges damage deltas per target entity into a NativeParallelHashMap.
  • After accumulating, iterates the map keys and decides whether to add/update the Damaged component or remove it (and related MaintenanceConsumer for vehicles), emitting structural changes through a provided EntityCommandBuffer. Note: the job allocates a NativeParallelHashMap with Allocator.Temp and uses ComponentLookup and ComponentTypeHandle to read/write components. It uses an EntityCommandBuffer created from m_ModificationBarrier for deferred structural changes.

Properties

  • This system does not expose public properties.

Constructors

  • public DamageSystem() Default constructor; no special initialization beyond base constructor.

Methods

  • protected override void OnCreate() : System.Void Creates and stores a reference to ModificationBarrier2 from the World and creates the m_EventQuery to select Event+Damage entities. Requires the query for update (RequireForUpdate) so the system runs only when there are damage events to process.

  • protected override void OnUpdate() : System.Void Builds an asynchronous archetype chunk list for the m_EventQuery then schedules the Burst-compiled DamageObjectsJob. It supplies the job with:

  • chunks (NativeList)
  • the Damage ComponentTypeHandle (read-only)
  • ComponentLookup and ComponentLookup
  • an EntityCommandBuffer from the modification barrier The method disposes of the chunks list with jobHandle and registers the jobHandle with the modification barrier (AddJobHandleForProducer). The system's base.Dependency is updated to the job's handle.

  • protected override void OnCreateForCompiler() : System.Void Compiler helper that assigns queries and component handles (via __AssignQueries and __TypeHandle.__AssignHandles) into the system state; used when systems are generated/compiled.

  • private void __AssignQueries(ref SystemState state) : System.Void Internal helper used by OnCreateForCompiler to set up queries. (In the decompiled code it creates and disposes an EntityQueryBuilder; this is part of generated code structure.)

  • (nested) TypeHandle.__AssignHandles(ref SystemState state) Assigns the ComponentTypeHandle (read-only) and ComponentLookup and ComponentLookup used by the job via the provided SystemState.

Usage Example

// Example: create a Damage event for a target entity so DamageSystem will pick it up.
// Assumes you have an EntityManager and a target Entity (targetEntity).
var em = World.DefaultGameObjectInjectionWorld.EntityManager;

// Create a transient event entity and add the Event + Damage components.
// Damage is a struct with fields: Entity m_Object; float m_Delta; (matches game's definition)
Entity damageEvent = em.CreateEntity();
em.AddComponentData(damageEvent, new Event());
em.AddComponentData(damageEvent, new Damage { m_Object = targetEntity, m_Delta = 25f });

// DamageSystem will process this event during its next update, accumulating damage on targetEntity,
// adding or updating its Damaged component and scheduling any structural changes with the modification barrier.

Notes, implementation details and modding tips: - The system batches multiple damage events for the same target inside the job to avoid multiple structural changes and to compute net damage for the frame. - Structural changes (AddComponent/RemoveComponent) are deferred using a ModificationBarrier2 command buffer — these changes only apply after the job completes. - The job allocates a NativeParallelHashMap with Allocator.Temp. If you adapt similar logic in your own systems, prefer Allocator.TempJob for allocations created inside jobs to avoid accidental invalid allocations on worker threads; also consider reusing/updating pools to reduce per-frame allocations. - The job is Burst-compiled for performance and uses ComponentTypeHandle/ComponentLookup to access component data efficiently. - When damaged.m_Damage <= 0 the system removes Damaged and adds BatchesUpdated, and if the entity is a Vehicle it also removes MaintenanceConsumer — be aware of these interactions when creating or removing those components in other systems. - If you are debugging damage not being applied, ensure your Damage event entities include both Event and Damage components so the m_EventQuery picks them up. - Heavy spike of damage events may allocate and iterate over many chunks — consider throttling event generation or merging events earlier if you hit performance problems.