Skip to content

Game.Creatures.TripResetSystem

Assembly: Assembly-CSharp
Namespace: Game.Creatures

Type: class

Base: GameSystemBase

Summary: TripResetSystem is a managed ECS system responsible for processing ResetTrip components and applying the reset logic for creature entities (humans, animals, pets, residents). It schedules a Burst-compiled IJobChunk (CreatureTripResetJob) that iterates over entities with ResetTrip and: - Skips creatures marked Deleted. - Clears EndOfPath flags on HumanCurrentLane and AnimalCurrentLane where present. - Restores or removes Divert components and updates PathOwner state (including copying path buffers when available). - Updates Target components and associated resident/pet flags/timers. - Adds or updates CurrentBuilding / Arrived state for associated citizens and sets Pet leader flags for group members. - Sets or updates TravelPurpose on the citizen entity. - Prepends a NextPurpose into the TripNeeded buffer (if provided). - Adds a TripSource component (with an optional delay) to the creature when a source entity is provided.

The system uses a ModificationBarrier4 EntityCommandBuffer to defer structural changes and runs its job in parallel via JobChunk scheduling. It depends on the presence of ResetTrip components (RequireForUpdate).


Fields

  • private ModificationBarrier4 m_ModificationBarrier A reference to the ModificationBarrier4 system used to create an EntityCommandBuffer (parallel writer) for deferred structural and component changes performed by the job.

  • private EntityQuery m_ResetQuery EntityQuery that selects entities containing the ResetTrip component. The system uses RequireForUpdate(m_ResetQuery) to only run when ResetTrip entities exist.

  • private TypeHandle __TypeHandle Internal struct instance that caches the EntityTypeHandle, ComponentTypeHandles, ComponentLookup and BufferLookup objects used by the scheduled job. These handles are assigned and used via InternalCompilerInterface to get thread-safe access to component data.

  • private struct CreatureTripResetJob (nested type) Burst-compiled IJobChunk implementation that contains the runtime-per-chunk logic for processing ResetTrip instances. It holds handles/lookups for:

  • EntityTypeHandle
  • ComponentTypeHandle (read-only)
  • ComponentLookup (read-only)
  • ComponentLookup (read-only)
  • BufferLookup (read-only)
  • BufferLookup (read-only)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • ComponentLookup (read/write)
  • BufferLookup (read/write)
  • EntityCommandBuffer.ParallelWriter (for structural/component changes)

Each of the above is used by Execute(...) to apply the reset logic per ResetTrip entry.

  • private struct TypeHandle (nested type) Generated helper storing per-type handles and lookups and providing __AssignHandles(ref SystemState) to initialize them.

Properties

  • None. This system does not expose public properties; it relies on internal handles/fields and the scheduled job to perform its work.

Constructors

  • public TripResetSystem() Default constructor. The system is marked with Preserve attributes for code stripping safety.

Methods

  • protected override void OnCreate() : System.Void Initializes the system:
  • Calls base.OnCreate().
  • Retrieves the ModificationBarrier4 system instance via base.World.GetOrCreateSystemManaged() and stores it in m_ModificationBarrier.
  • Creates an EntityQuery for ResetTrip and stores it in m_ResetQuery.
  • Calls RequireForUpdate(m_ResetQuery) so the system only updates when ResetTrip entities exist.

  • protected override void OnUpdate() : System.Void Prepares and schedules the CreatureTripResetJob:

  • Fills the job struct fields by obtaining EntityTypeHandle, component handles and lookups via InternalCompilerInterface and the cached __TypeHandle.
  • Acquires a parallel EntityCommandBuffer from m_ModificationBarrier.
  • Schedules the job using JobChunkExtensions.Schedule(...) on m_ResetQuery with base.Dependency and sets base.Dependency to the returned JobHandle.
  • Calls m_ModificationBarrier.AddJobHandleForProducer(base.Dependency) to let the barrier know about the scheduled job.

  • private void __AssignQueries(ref SystemState state) : System.Void Internal method used by the generated OnCreateForCompiler to initialize queries for the compiler. In this class it currently creates and disposes an EntityQueryBuilder (no-op placeholder for compiler-generated query setup).

  • protected override void OnCreateForCompiler() : System.Void Compiler helper used in source-generated systems:

  • Calls base.OnCreateForCompiler().
  • Calls __AssignQueries(ref base.CheckedStateRef) to set up queries for the compiler.
  • Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to initialize cached type handles/lookups used later in OnUpdate.

  • private struct CreatureTripResetJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void Core per-chunk execution for ResetTrip processing. For each ResetTrip entry in the chunk:

  • Retrieves the creature entity referenced by ResetTrip.m_Creature.
  • Skips if the creature has Deleted component.
  • Clears CreatureLaneFlags.EndOfPath from HumanCurrentLane and AnimalCurrentLane if those components exist for the creature.
  • If the creature has a Target component:
    • Handles Divert logic: if ResetTrip specifies a divert purpose, either updates existing Divert component or adds a new Divert component using the command buffer. Tracks whether Divert was changed (flag) or already matched (flag2).
    • Updates PathOwner flags: clears Failed, then either copies the path buffer from the ResetTrip entity into the creature's path buffer (if HasDivertPath/conditions satisfied) or marks path as DivertObsolete / CachedObsolete / Obsolete depending on flags and pending state.
    • If ResetTrip.m_Target differs from current Target: updates Resident and Pet flags/timers, updates PathOwner state similarly (copying path if possible), and writes the new Target component.
  • Handles resident-specific behavior:
    • If resetTrip.m_Arrived is set and resident has valid m_Citizen, and that citizen has a TripNeeded buffer: sets CurrentBuilding on the citizen and enables the Arrived component via command buffer.
    • If creature is group leader and has group creatures, marks PetFlags.LeaderArrived on group pets.
    • Sets or updates TravelPurpose on the citizen if resetTrip specifies a travel purpose.
    • If resetTrip specifies NextPurpose and citizen has TripNeeded buffer: creates a new TripNeeded buffer (written via command buffer) where the NextPurpose entry is inserted at the front followed by existing TripNeeded entries.
  • If resetTrip.m_Source is set (non-null), adds a TripSource(resetTrip.m_Source, resetTrip.m_Delay) to the creature via the command buffer.

The job uses try-get lookups for components and buffers to avoid exceptions for missing components and applies changes either via direct ComponentLookup writes (for non-structural updates) or via the parallel EntityCommandBuffer for structural changes (add/remove components, SetBuffer, SetComponentEnabled).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Initialize the modification barrier used to create deferred command buffers
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier4>();
    // Ensure system runs only when there are ResetTrip components
    m_ResetQuery = GetEntityQuery(ComponentType.ReadOnly<ResetTrip>());
    RequireForUpdate(m_ResetQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // Setup and schedule the Burst-compiled IJobChunk that processes ResetTrip entries
    var job = new CreatureTripResetJob
    {
        m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
        m_ResetTripType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Creatures_ResetTrip_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        // ... other handle initializations ...
        m_CommandBuffer = m_ModificationBarrier.CreateCommandBuffer().AsParallelWriter()
    };

    base.Dependency = JobChunkExtensions.Schedule(job, m_ResetQuery, base.Dependency);
    m_ModificationBarrier.AddJobHandleForProducer(base.Dependency);
}

Notes and tips: - TripResetSystem relies heavily on ComponentLookup and BufferLookup access; when inspecting or patching, ensure your mod is compatible with the parallel usage and read/write flags. - Structural changes (adding/removing components, setting buffers) are performed via the command buffer returned by ModificationBarrier4 — modifications happen safely after the job completes. - The job uses PathUtils.CopyPath to transfer path buffers; copying only occurs when both source and target buffers are available and PathOwner state indicates it's safe to do so. - Because this system is Burst-compiled and uses low-level lookups, debugging requires care: either disable Burst or replicate behavior in a small test system to inspect intermediate values.