Skip to content

Game.Net.LaneObjectUpdater

Assembly: Assembly-CSharp
Namespace: Game.Net

Type: struct

Base: System.ValueType

Summary:
LaneObjectUpdater is a lightweight command-buffer style updater used by the networking/objects code to apply batched changes to lane-attached objects (LaneObject) and tree objects stored in a spatial search tree. It gathers add/remove/update actions into native queues, then schedules two Burst-compiled jobs: a parallel-for job that updates per-lane buffers (LaneObject buffer) and a single job that updates a moving QuadTree search structure for tree objects. The struct handles allocation of the native queues, prepares BufferLookup access, schedules the jobs, and disposes of the native containers once jobs are scheduled.


Fields

  • private Game.Objects.SearchSystem m_SearchSystem
    Used to get and write the moving search tree for tree objects. Obtained from the world in the constructor.

  • private BufferLookup<LaneObject> m_LaneObjects
    BufferLookup used to access per-lane LaneObject DynamicBuffers when the Apply method runs. Updated each Apply via m_LaneObjects.Update(system).

  • private NativeParallelQueue<LaneObjectAction> m_LaneActionQueue
    NativeParallelQueue that stores lane object actions (adds/removes/updates) pushed by the returned LaneObjectCommandBuffer. Used as the work source for the parallel UpdateLaneObjectsJob.

  • private NativeQueue<TreeObjectAction> m_TreeActionQueue
    NativeQueue that stores tree object actions (adds/removes/updates) pushed by the returned LaneObjectCommandBuffer. Consumed by UpdateTreeObjectsJob.

  • (nested) private struct UpdateLaneObjectsJob : IJobParallelFor
    Burst-compiled parallel-for job that reads per-index lane action queues and applies actions to lane buffers via NetUtils.AddLaneObject / RemoveLaneObject / UpdateLaneObject.

  • (nested) private struct UpdateTreeObjectsJob : IJob
    Burst-compiled single job that dequeues TreeObjectAction items and updates the NativeQuadTree search tree accordingly.


Properties

  • None. This struct exposes no public properties.

Constructors

  • public LaneObjectUpdater(SystemBase system)
    Initializes the updater for the provided SystemBase:
  • caches the Game.Objects.SearchSystem from the world,
  • obtains a BufferLookup from the system,
  • initializes the native queue fields to their default (not yet allocated). This prepares the updater to Begin() a command buffer when changes need to be recorded.

Methods

  • public LaneObjectCommandBuffer Begin(Allocator allocator)
    Allocates and initializes the native queues used to record actions:
  • creates a NativeParallelQueue with the given allocator,
  • creates a NativeQueue with the given allocator,
  • returns a LaneObjectCommandBuffer that provides writers to enqueue lane and tree actions. Use Begin before recording actions for the current frame/update and ensure Apply is called later to schedule work.

  • public JobHandle Apply(SystemBase system, JobHandle dependencies)
    Schedules the jobs that apply the recorded actions and returns the JobHandle for the scheduled work. Behavior:

  • calls m_LaneObjects.Update(system) to refresh the BufferLookup state,
  • creates UpdateLaneObjectsJob with the lane-action reader and BufferLookup; schedules it as an IJobParallelFor using m_LaneActionQueue.HashRange,
  • obtains a moving search tree from m_SearchSystem (GetMovingSearchTree) — this may return an additional dependency,
  • creates UpdateTreeObjectsJob with the tree-action queue and the obtained search tree; schedules it as an IJob (combined dependencies),
  • disposes of the native queues with the corresponding job handles (so memory is released when jobs complete),
  • registers the tree writer with m_SearchSystem via AddMovingSearchTreeWriter(jobHandleForTreeJob),
  • returns the JobHandle corresponding to the lane-object job (the method returns jobHandle; note tree job is registered separately with the search system writer). Important: callers should pass in any dependency JobHandle that must complete before these updates, and should use the returned JobHandle(s) appropriately to ensure ordering.

  • (nested) UpdateLaneObjectsJob.Execute(int index)
    Implements per-index work for the parallel job:

  • obtains the per-index enumerator from the NativeParallelQueue.Reader,
  • for each LaneObjectAction:
    • tries to get the LaneObject buffer for the target lane; if none, skips,
    • if add == remove: this is an update-only action — calls NetUtils.UpdateLaneObject if non-null,
    • if remove != Entity.Null: calls NetUtils.RemoveLaneObject,
    • if add != Entity.Null: calls NetUtils.AddLaneObject with the provided curve position,
  • disposes the enumerator when done. This job performs safe batched modifications to per-lane DynamicBuffers in parallel.

  • (nested) UpdateTreeObjectsJob.Execute()
    Dequeues TreeObjectAction items from the NativeQueue and updates the NativeQuadTree:

  • if add == remove: treat as an update — call m_SearchTree.Update with the provided bounds,
  • if remove != Entity.Null: call m_SearchTree.TryRemove,
  • if add != Entity.Null: call m_SearchTree.TryAdd; if TryAdd fails and entity appears already present, logs a Debug message with the entity index and center coordinates. This job updates the spatial index for tree-like objects.

Usage Example

// Example usage inside a SystemBase (mod/game system):
private LaneObjectUpdater m_LaneObjectUpdater;

protected override void OnCreate()
{
    base.OnCreate();
    m_LaneObjectUpdater = new LaneObjectUpdater(this);
}

public void SomeMethodRecordingChanges()
{
    // Begin a command buffer using the Temp allocator (or appropriate allocator)
    var cmdBuffer = m_LaneObjectUpdater.Begin(Allocator.TempJob);

    // Record actions via the returned command buffer:
    // cmdBuffer.AddLaneObject(laneEntity, laneObjectEntity, curvePosition);
    // cmdBuffer.RemoveLaneObject(laneEntity, laneObjectEntity);
    // cmdBuffer.UpdateTreeObject(treeEntity, bounds);
    // ... (record multiple actions across game logic)

    // When ready to apply, schedule jobs from within the system update:
    JobHandle deps = default; // any prior dependencies
    JobHandle handle = m_LaneObjectUpdater.Apply(this, deps);

    // Optionally combine handle with other work:
    // handle = JobHandle.CombineDependencies(handle, otherHandle);
    // return handle from OnUpdate or store it for synchronization
}

Notes and tips: - The Begin method allocates native queues — prefer Allocator.TempJob or Allocator.Persistent according to life-time; Apply disposes the queues when jobs are scheduled, so do not reuse them after Apply unless you call Begin again. - Actions are recorded into lane and tree queues via the returned LaneObjectCommandBuffer writer APIs; those writers are thread-safe if used as intended (parallel writer for tree queue and writer for parallel queue). - The implementation relies on NetUtils methods and the Game.Objects.SearchSystem for modifications; ensure the appropriate systems and utilities are present and compatible when modding.