Skip to content

Game.Pathfind.PathfindTargetBuffer

Assembly:
Namespace: Game.Pathfind

Type: public struct PathfindTargetBuffer

Base: System.ValueType, IPathfindTargetBuffer

Summary:
A thin, value-type wrapper around Unity.Collections.UnsafeQueue.ParallelWriter that provides a simple Enqueue method for adding PathTarget entries. Designed to be used in pathfinding producer code (including jobs) where a thread-safe ParallelWriter is required. The struct itself holds the ParallelWriter and exposes an implicit conversion operator to create the wrapper from a ParallelWriter instance.


Fields

  • private UnsafeQueue<PathTarget>.ParallelWriter m_Queue
    Holds the underlying thread-safe writer used to enqueue PathTarget items. This field must be initialized (for example via the provided implicit conversion from UnsafeQueue.ParallelWriter) before calling Enqueue. If left uninitialized, calling Enqueue will result in undefined behavior or runtime errors.

Properties

  • No public properties.
    This struct exposes behavior via methods and an implicit conversion operator only.

Constructors

  • No explicit constructors defined.
    The default struct constructor exists (zero-initializes m_Queue), but you should initialize the wrapper with the implicit conversion operator or by assigning a ParallelWriter to m_Queue before use.

Methods

  • public void Enqueue(PathTarget pathTarget)
    Enqueues the provided PathTarget into the underlying UnsafeQueue ParallelWriter. Intended to be called from producer code (including within jobs) to record pathfinding targets.

  • public static implicit operator PathfindTargetBuffer(UnsafeQueue<PathTarget>.ParallelWriter queue)
    Creates a PathfindTargetBuffer from a UnsafeQueue.ParallelWriter. Use this conversion to easily obtain a buffer wrapper that can be passed around to producer code or jobs.

Usage Example

// Create an UnsafeQueue<PathTarget> (lifetime and allocator must be managed appropriately).
var queue = new UnsafeQueue<PathTarget>(16, Allocator.TempJob);

// Get a ParallelWriter for use from multiple threads/jobs.
var parallelWriter = queue.AsParallelWriter();

// Implicitly convert the ParallelWriter to PathfindTargetBuffer
PathfindTargetBuffer buffer = parallelWriter;

// Enqueue a PathTarget (PathTarget is assumed to be a user-defined struct)
var target = new PathTarget { /* initialize fields */ };
buffer.Enqueue(target);

// When finished, dispose of the queue to free native memory.
queue.Dispose();

Notes and tips: - Ensure the underlying UnsafeQueue is created with the correct allocator and lifetime (e.g., Allocator.TempJob or Allocator.Persistent) and is disposed when no longer needed. - This wrapper is intended for producer-side code. Consumers should read from the corresponding UnsafeQueue reader. - Because this relies on Unity's low-level unsafe collections and ParallelWriter, misuse (uninitialized writer, concurrent misuse, use after dispose) can cause crashes or memory corruption.