Skip to content

Game.SubElementDeleteSystem

Assembly:
Namespace: Game.Objects

Type: class

Base: GameSystemBase

Summary:
SubElementDeleteSystem is an ECS system that ensures sub-elements owned by other game objects are correctly removed or marked updated when their owner is deleted (or when ownership changes). It schedules two Burst-compiled parallel jobs to: - Walk buffers of SubArea, SubNet, SubRoute and OwnedVehicle attached to owner entities and mark their referenced entities as Deleted or Updated as appropriate. - Check newly created vehicles that have an Owner and immediately delete them if their Owner entity is already marked Deleted.

The system uses a ToolReadyBarrier (an EntityCommandBuffer barrier) to enqueue structural changes (AddComponent/RemoveComponent) from jobs, and runs only when there are Deleted components present on entities that also have one of the sub-element buffers.


Fields

  • private ToolReadyBarrier m_ModificationBarrier
    Used to create an EntityCommandBuffer.ParallelWriter to perform structural changes from jobs (AddComponent / RemoveComponent) in a thread-safe manner. The barrier ensures commands are played back at the correct point in the frame.

  • private EntityQuery m_DeletedQuery
    Query that selects entities with a Deleted component and at least one of SubArea, SubNet, SubRoute or OwnedVehicle buffers; used to drive the main DeleteSubElementsJob scheduling and to RequireForUpdate the system.

  • private EntityQuery m_CreatedQuery
    Query that selects newly created vehicles with Owner (Created + Vehicle + Owner). If not empty, CheckDeletedOwnersJob is scheduled to remove vehicles whose owner is deleted.

  • private ComponentTypeSet m_AppliedTypes
    A set of component types (Applied, Created, Updated) which the job removes from entity sub-elements when marking them deleted; used when fully deleting a sub-element so it no longer appears as applied/created/updated.

  • private TypeHandle __TypeHandle
    Internal bundle of type handles (EntityTypeHandle, BufferTypeHandles, ComponentLookups, etc.) cached/assigned for use within jobs to access chunk data.

Properties

  • None.

Constructors

  • public SubElementDeleteSystem()
    Default constructor. The system relies on Unity ECS lifecycle methods (OnCreate/OnUpdate) to initialize and schedule jobs.

Methods

  • protected override void OnCreate()
    Initializes the system: obtains the ToolReadyBarrier from the world, sets up m_DeletedQuery (entities with Deleted and any of SubArea/SubNet/SubRoute/OwnedVehicle), the m_CreatedQuery (Created + Vehicle + Owner), prepares the m_AppliedTypes set (Applied, Created, Updated) and calls RequireForUpdate(m_DeletedQuery) so the system runs only when relevant deletions exist.

  • protected override void OnUpdate()
    Schedules the main DeleteSubElementsJob (Burst-compiled IJobChunk) in parallel, providing all required type handles/ComponentLookups/BufferLookups and an EntityCommandBuffer.ParallelWriter from m_ModificationBarrier. If m_CreatedQuery is not empty, it also schedules CheckDeletedOwnersJob (Burst-compiled IJobChunk) to handle newly created vehicles whose owners are deleted. Combined job handle is registered with the modification barrier via AddJobHandleForProducer.

  • protected override void OnCreateForCompiler()
    Internal helper called for compiler-driven initialization; assigns query and handle state used by generated code.

  • private void __AssignQueries(ref SystemState state)
    Internal method (empty here) intended to declare or prepare EntityQueryBuilder usage for compiler-generated systems.

  • (Nested) DeleteSubElementsJob : IJobChunk (private, BurstCompile)

  • Iterates owner chunks with buffers: SubArea, SubNet, SubRoute, OwnedVehicle.
  • For SubArea buffer entries: if the referenced area entity is not already Deleted, removes applied types and adds Deleted to that area.
  • For SubNet buffer entries:
    • If the sub-net entity has connected edges, inspects each edge. For edges not already Deleted/Updated and not owned by the owner entity (or owned but owner deleted), it marks edges and their connected nodes Updated (adds Updated). It determines whether the sub-net should be deleted (flag) by checking whether any edge still references this sub-net as start or end.
    • If there are no connected edge buffers but the sub-net has an Edge component, it calls UpdateConnectedNodes to mark connected nodes/edges Updated.
    • Finally, if the sub-net itself is not Deleted: if no remaining edges reference it, it removes applied types and adds Deleted; otherwise removes Owner and adds Updated.
  • For SubRoute buffer entries: adds Deleted to route entities if not already Deleted.
  • For OwnedVehicle buffer entries: if the vehicle is not already Deleted, retrieves its LayoutElement buffer (if any) and calls VehicleUtils.DeleteVehicle via the command buffer to delete the vehicle.
  • Uses m_DeletedData, m_UpdatedData, m_OwnerData, m_EdgeData, m_ConnectedEdges, m_LayoutElements lookups to make decisions.
  • Uses EntityCommandBuffer.ParallelWriter to enqueue structural changes.

  • (Nested) CheckDeletedOwnersJob : IJobChunk (private, BurstCompile)

  • Iterates created vehicle entities with an Owner component. If the owner entity is already marked Deleted, it retrieves LayoutElement buffer for the vehicle and calls VehicleUtils.DeleteVehicle via command buffer, ensuring vehicles created with a deleted owner are immediately removed.

  • (Nested) TypeHandle struct

  • Holds a set of EntityTypeHandle, BufferTypeHandle, ComponentLookup, BufferLookup and ComponentTypeHandle used by jobs. Has __AssignHandles(ref SystemState) to grab handles from the current state (called in OnCreateForCompiler).

  • private void __AssignQueries(ref SystemState state)
    Compiler helper (shown in class) — no runtime behavior here beyond a placeholder.

Usage Example

// This system runs automatically when an entity with Deleted + SubNet/SubArea/SubRoute/OwnedVehicle exists.
// To trigger removal of sub-elements, mark an owner entity Deleted:
EntityManager.AddComponentData(ownerEntity, new Deleted());

// After the frame, SubElementDeleteSystem will:
//  - mark associated sub-entities (areas, nets, routes) as Deleted (or Updated if still referenced)
//  - delete owned vehicles via VehicleUtils.DeleteVehicle using their LayoutElement buffers

Notes and tips: - The system performs structural changes through an EntityCommandBuffer.ParallelWriter created from the ToolReadyBarrier. Do not attempt to perform structural changes directly from jobs. - The logic assumes components and buffer element structs: - SubArea: contains m_Area (Entity) - SubNet: contains m_SubNet (Entity) - SubRoute: contains m_Route (Entity) - OwnedVehicle: contains m_Vehicle (Entity) - Owner: contains m_Owner (Entity) - Edge: contains m_Start / m_End (Entity) - ConnectedEdge buffer: contains m_Edge (Entity) - LayoutElement buffer: used by VehicleUtils.DeleteVehicle - VehicleUtils.DeleteVehicle is called to handle the vehicle deletion flow; ensure that utility exists in your mod environment or provide replacement if needed.