Skip to content

Game.Pathfind.PathOwnerTargetMovedSystem

Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Pathfind

Type: class PathOwnerTargetMovedSystem

Base: GameSystemBase

Summary:
System that responds to PathTargetMoved events and updates PathOwner entities whose Target references the moved entity. For each PathTargetMoved event it schedules a Burst-compiled IJobChunk (CheckPathOwnerTargetsJob) over all PathOwner entities; when a PathOwner references the moved target the job either clears a randomly selected future PathElement's m_Target (if the element index is still in range) or marks the PathOwner as obsolete (sets the PathFlags.Obsolete bit). The system uses ECS queries and schedules jobs in parallel; it also obtains component and buffer type handles via an internal TypeHandle helper.


Fields

  • private EntityQuery m_EventQuery
    Holds the query used to collect PathTargetMoved event entities (requires PathTargetMoved + Event). Used in OnCreate and OnUpdate to gather events that should trigger processing.

  • private EntityQuery m_PathOwnerQuery
    Holds the query that selects PathOwner entities that will be scanned by the job. The query requires PathOwner, PathElement (buffer) and Target, and excludes GroupMember.

  • private TypeHandle __TypeHandle
    Internal struct that caches ComponentTypeHandle/BufferTypeHandle instances used by the job. Populated via __AssignHandles(ref SystemState) during system creation/compilation path.

Properties

  • This type declares no public properties.

Constructors

  • public PathOwnerTargetMovedSystem()
    Default constructor (preserved). No custom initialization beyond base constructor.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system queries:
  • m_EventQuery = query for PathTargetMoved + Event (read-only)
  • m_PathOwnerQuery = query for PathOwner, PathElement buffer, Target (excludes GroupMember)
    Calls RequireForUpdate for both queries so the system only runs when there are relevant entities.

  • protected override void OnUpdate() : System.Void
    Main update loop:

  • Collects PathTargetMoved event data into a temporary NativeArray (Allocator.TempJob).
  • For each PathTargetMoved entry, constructs a CheckPathOwnerTargetsJob, sets up component/buffer type handles via the cached __TypeHandle, assigns a random seed and the moved entity (m_MovedEntity), and schedules the job in parallel over m_PathOwnerQuery using JobChunk scheduling.
  • Ensures the temporary NativeArray is disposed in a finally block.

  • protected override void OnCreateForCompiler() : System.Void
    Called for compiler-specific initialization; it assigns queries and component handles used by the system via __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Compiler helper that (in this build) creates an EntityQueryBuilder and disposes it. Present to satisfy generated code patterns; real handle assignment of component types happens in __TypeHandle.__AssignHandles.

  • private struct TypeHandle.__AssignHandles(ref SystemState state) : System.Void
    Sets up the following handles on the TypeHandle instance:

  • ComponentTypeHandle (read-only)
  • ComponentTypeHandle (read/write)
  • BufferTypeHandle (read/write)
    These handles are then used to get actual handles for the job via InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle.

  • private struct CheckPathOwnerTargetsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void
    JobChunk implementation (Burst compiled) that runs per-chunk:

  • Retrieves NativeArray, NativeArray, and BufferAccessor for the chunk using the provided handles.
  • Initializes a Random from m_RandomSeed using the chunk index.
  • Iterates entities in the chunk; when a Target component equals m_MovedEntity:
    • If PathOwner.m_ElementIndex < PathElement buffer length: chooses a random index in [m_ElementIndex, buffer.Length), sets that PathElement.m_Target = Entity.Null.
    • Else: sets PathOwner.m_State |= PathFlags.Obsolete and writes it back to the PathOwner array.
  • This effectively removes references to the moved entity from future path elements or marks the owner obsolete if the element index is already past the buffer.

Notes and implementation details: - The job uses BurstCompile for performance and runs via JobChunkExtensions.ScheduleParallel. - RandomSeed.Next() is called per event to produce seeds for the job; each event schedules a separate job. - Temporary NativeArray is created with Allocator.TempJob and properly disposed in a finally block to avoid leaks. - The PathOwner query excludes GroupMember, so group-member path owners are not processed by this system.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_EventQuery = GetEntityQuery(ComponentType.ReadOnly<PathTargetMoved>(), ComponentType.ReadOnly<Event>());
    m_PathOwnerQuery = GetEntityQuery(
        ComponentType.ReadOnly<PathOwner>(),
        ComponentType.ReadOnly<PathElement>(),
        ComponentType.ReadOnly<Target>(),
        ComponentType.Exclude<GroupMember>());
    RequireForUpdate(m_EventQuery);
    RequireForUpdate(m_PathOwnerQuery);
}

[Preserve]
protected override void OnUpdate()
{
    var events = m_EventQuery.ToComponentDataArray<PathTargetMoved>(Allocator.TempJob);
    try
    {
        var jobData = new CheckPathOwnerTargetsJob {
            m_TargetType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Common_Target_RO_ComponentTypeHandle, ref base.CheckedStateRef),
            m_PathOwnerType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Pathfind_PathOwner_RW_ComponentTypeHandle, ref base.CheckedStateRef),
            m_PathElementType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Pathfind_PathElement_RW_BufferTypeHandle, ref base.CheckedStateRef)
        };

        for (int i = 0; i < events.Length; i++)
        {
            jobData.m_RandomSeed = RandomSeed.Next();
            jobData.m_MovedEntity = events[i].m_Target;
            base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_PathOwnerQuery, base.Dependency);
        }
    }
    finally
    {
        events.Dispose();
    }
}

If you want, I can also produce a small diagram of the queries and job flow or extract the nested job struct into its own documented section.