Skip to content

Game.UnspawnedSystem

Assembly: Assembly-CSharp (inferred)
Namespace: Game.Objects

Type: public class UnspawnedSystem : GameSystemBase
Base: GameSystemBase

Summary:
UnspawnedSystem is a Unity ECS system that propagates the Unspawned state across related entities (sub-objects, vehicle layout elements and passengers). It schedules a parallel IJobChunk (UpdateUnspawnedJob) that iterates over chunks of Vehicles and Creatures matching the system's query, inspects DynamicBuffers (SubObject, LayoutElement, Passenger) and component lookups (Relative, Unspawned, Updated), and uses a ModificationEndBarrier command buffer to add or remove the Unspawned component and to mark entities as updated (BatchesUpdated) when their spawned state changes. The system uses a TypeHandle helper to cache the various handles/lookups required by the job and requires an EntityQuery that selects Vehicle and Creature entities with Updated/BatchesUpdated and excludes Deleted/Destroyed.


Fields

  • private EntityQuery m_UpdateQuery
    Registers the entity query used to find Vehicles and Creatures that need propagation of Unspawned state (All: Vehicle or Creature; Any: Updated or BatchesUpdated; None: Deleted or Destroyed). Required for the system's update.

  • private ModificationEndBarrier m_ModificationEndBarrier
    Barrier system used to produce an EntityCommandBuffer (AsParallelWriter) so component add/remove operations can safely be recorded from the job and executed later.

  • private TypeHandle __TypeHandle
    Holds cached EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle/ComponentLookup/BufferLookup instances used by the UpdateUnspawnedJob. Populated via __AssignHandles during system initialization.

Properties

  • None (no public properties exposed by UnspawnedSystem)

Constructors

  • public UnspawnedSystem()
    Default parameterless constructor. The system is marked with [Preserve] on lifecycle methods to keep it from being stripped.

Methods

  • protected override void OnCreate()
    Initializes system resources: obtains/creates the ModificationEndBarrier, constructs the m_UpdateQuery that selects Vehicles and Creatures needing update propagation, and calls RequireForUpdate(m_UpdateQuery) so the system only runs when relevant entities exist.

  • protected override void OnUpdate()
    Creates and schedules a parallel UpdateUnspawnedJob. The job receives:

  • Entity/Component/Buffer type handles and component/buffer lookups retrieved from __TypeHandle via InternalCompilerInterface helpers and the system's CheckedStateRef.
  • A parallel-capable EntityCommandBuffer from m_ModificationEndBarrier. After scheduling, the job handle is registered with m_ModificationEndBarrier (AddJobHandleForProducer) and assigned to base.Dependency.

  • protected override void OnCreateForCompiler()
    Compiler helper that assigns queries and type handles. Calls __AssignQueries and __AssignHandles to ensure generated handles are populated for the compiler/runtime.

  • private void __AssignQueries(ref SystemState state)
    Generated stub used to assign any generated queries (here it only creates/disposes an EntityQueryBuilder placeholder). Marked [MethodImpl(MethodImplOptions.AggressiveInlining)].

  • Nested type: private struct UpdateUnspawnedJob : IJobChunk

  • Burst compiled job that:
    • Reads entities, buffers and component lookups.
    • For each chunk, obtains native arrays/buffer accessors and determines whether the chunk's archetype currently has the Unspawned component.
    • Iterates SubObject buffers and recursively checks sub-objects via CheckSubObjects.
    • Iterates LayoutElement buffers to check linked vehicle entities and their subobjects/passengers via CheckLayout.
    • Iterates Passenger buffers and recursively checks passenger subobjects via CheckPassengers.
    • Uses UpdateUnspawned to add/remove Unspawned on related entities through the parallel command buffer and adds BatchesUpdated when needed.
  • Key methods inside job:
    • Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    • CheckSubObjects(int jobIndex, DynamicBuffer subObjects, bool isUnspawned)
    • CheckLayout(int jobIndex, Entity entity, DynamicBuffer layout, bool isUnspawned)
    • CheckPassengers(int jobIndex, DynamicBuffer passengers, bool isUnspawned)
    • UpdateUnspawned(int jobIndex, Entity entity, bool isUnspawned)
  • Updates performed using m_CommandBuffer.AddComponent / RemoveComponent and adding BatchesUpdated if the entity lacks Updated/BatchesUpdated.

  • Nested type: private struct TypeHandle

  • Holds the following handles/lookups (readonly fields):
    • EntityTypeHandle
    • ComponentTypeHandle (read-only)
    • BufferTypeHandle (read-only)
    • BufferTypeHandle (read-only)
    • BufferTypeHandle (read-only)
    • ComponentLookup (read-only)
    • ComponentLookup (read-only)
    • ComponentLookup (read-only)
    • BufferLookup (read-only)
    • BufferLookup (read-only)
  • Method __AssignHandles(ref SystemState state) initializes these handles via the provided SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Get or create the modification barrier and build the update query so system runs
    m_ModificationEndBarrier = base.World.GetOrCreateSystemManaged<ModificationEndBarrier>();
    m_UpdateQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[1] { ComponentType.ReadWrite<Vehicle>() },
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Updated>(),
            ComponentType.ReadOnly<BatchesUpdated>()
        },
        None = new ComponentType[2]
        {
            ComponentType.ReadOnly<Deleted>(),
            ComponentType.ReadOnly<Destroyed>()
        }
    }, new EntityQueryDesc
    {
        All = new ComponentType[1] { ComponentType.ReadWrite<Creature>() },
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Updated>(),
            ComponentType.ReadOnly<BatchesUpdated>()
        },
        None = new ComponentType[2]
        {
            ComponentType.ReadOnly<Deleted>(),
            ComponentType.ReadOnly<Destroyed>()
        }
    });
    RequireForUpdate(m_UpdateQuery);
}

Notes / Tips: - The UpdateUnspawnedJob is Burst-compiled and runs in parallel; all structural changes are deferred via the ModificationEndBarrier's command buffer. - This system ensures hierarchical relationships (sub-objects, passengers, layout-linked vehicles) have consistent Unspawned state with their parents. When integrating or debugging, inspect DynamicBuffer contents and component lookups to understand propagation paths.