Skip to content

Game.Areas.SearchSystem

Assembly:
(assembly not specified in source; part of the game's managed code)

Namespace: Game.Areas

Type: class

Base: GameSystemBase, IPreDeserialize

Summary:
SearchSystem maintains a spatial search index (a NativeQuadTree of AreaSearchItem → QuadTreeBoundsXZ) for area geometry (triangles) used by the game to quickly find areas/triangles for queries (visibility, raycasts, culling, etc.). It watches Area entities (buffers Node and Triangle) and keeps the quad tree in-sync by scheduling a chunk job (UpdateSearchTreeJob) that adds/updates/removes triangle bounds. The system also holds a NativeParallelHashMap to remember the triangle counts per area so it can remove stale entries when triangles are removed. PreDeserialize clears the tree and marks the system to rebuild everything on the next update.

Notes: - Uses Unity.Entities job/chunk APIs and Burst-compiled UpdateSearchTreeJob for chunk-by-chunk indexing. - Provides explicit reader/writer dependency helpers (AddSearchTreeReader/AddSearchTreeWriter) so other systems/jobs can integrate with the search tree safely. - Native containers are allocated with Allocator.Persistent and disposed in OnDestroy.


Fields

  • private EntityQuery m_UpdatedAreasQuery
    Query for area entities that have been updated or deleted; used to schedule incremental updates to the search tree.

  • private EntityQuery m_AllAreasQuery
    Query for all area entities (excluding Temp) used when rebuilding/initializing the tree (e.g., after deserialization).

  • private NativeQuadTree<AreaSearchItem, QuadTreeBoundsXZ> m_SearchTree
    The spatial index storing triangle bounds keyed by AreaSearchItem(entity, triangleIndex).

  • private NativeParallelHashMap<Entity, int> m_TriangleCount
    Stores the current known number of triangles per entity so the job can remove entries for triangles that were deleted.

  • private JobHandle m_ReadDependencies
    Aggregated JobHandle for all jobs that read from the search tree; used to compute correct dependency when giving read access.

  • private JobHandle m_WriteDependencies
    Aggregated JobHandle for all jobs that write to the search tree; used to compute correct dependency when giving writer access.

  • private bool m_Loaded
    Flag used during deserialization: PreDeserialize sets this true to force a full rebuild on the next OnUpdate. GetLoaded() returns true once then clears it.

  • private TypeHandle __TypeHandle
    Internal struct instance that stores Entity/Buffer/Component handles (created via __AssignHandles); used to avoid repeatedly asking the SystemState for handles.

  • Nested types (not listed above as fields but part of implementation):

  • UpdateSearchTreeJob (Burst-compiled IJobChunk) — the chunk job that updates the quad tree.
  • TypeHandle (private struct) — helper to cache and assign the various type handles used by the job.

Properties

  • This system does not expose public C# properties. Interaction is done via the public API methods listed below (GetSearchTree, AddSearchTreeReader/Writer, PreDeserialize).

Constructors

  • public SearchSystem()
    Default constructor. The system uses the Unity/Colossal preservation attributes and its initialization is done in OnCreate().

Methods

  • protected override void OnCreate() : System.Void
    Initializes queries, allocates the NativeQuadTree and NativeParallelHashMap (persistent), and prepares the system to operate.

  • protected override void OnDestroy() : System.Void
    Disposes of m_SearchTree and m_TriangleCount and calls base.OnDestroy().

  • private bool GetLoaded() : System.Boolean
    Helper used by OnUpdate/PreDeserialize. If m_Loaded is true, GetLoaded clears it and returns true (signals a full rebuild is needed); otherwise returns false.

  • protected override void OnUpdate() : System.Void
    Main update loop. Determines whether to process only updated areas or all areas (when m_Loaded is set), constructs UpdateSearchTreeJob with the necessary type handles and native containers, schedules the job via JobChunkExtensions.Schedule, and registers the job handle as a writer by calling AddSearchTreeWriter.

Behavior summary for UpdateSearchTreeJob: - If chunk has Deleted: removes all triangle entries for that entity from the quad tree and cleans up m_TriangleCount. - If loaded (full rebuild) or chunk has Created: adds all triangle entries for the entity to the tree and records triangle count. - Otherwise: compares existing triangle count with current buffer length to update existing entries, remove now-missing entries, and add newly created triangle entries. - Uses PrefabRef to resolve AreaGeometryData (via ComponentLookup) and AreaUtils.GetTriangle3/GetBounds to compute bounds/LOD for QuadTreeBoundsXZ. - Honors various BoundsMask flags and the Batch component to set layer flags.

  • public NativeQuadTree<AreaSearchItem, QuadTreeBoundsXZ> GetSearchTree(bool readOnly, out JobHandle dependencies)
    Returns a reference to the search tree. Computes dependencies: if readOnly is true, only the write dependencies are returned so readers wait for writers; otherwise combines read+write dependencies.

  • public NativeQuadTree<AreaSearchItem, QuadTreeBoundsXZ> GetSearchTree(bool readOnly, out JobHandle dependencies, out NativeParallelHashMap<Entity, int> triangleCount)
    Overload that additionally returns the triangleCount hash map. Same dependency behavior as above.

  • public void AddSearchTreeReader(JobHandle jobHandle) : System.Void
    Register a job that will read from the search tree. The system will combine the provided JobHandle into m_ReadDependencies to track readers.

  • public void AddSearchTreeWriter(JobHandle jobHandle) : System.Void
    Register a job that will write to the search tree. The system will combine the provided JobHandle into m_WriteDependencies to track writers.

  • public void PreDeserialize(Context context) : System.Void
    Called before deserializing scene/entity data. Completes any outstanding dependencies, clears the search tree, and sets m_Loaded = true so the next OnUpdate will do a full rebuild of the index.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Internal helper used by the compiler-time OnCreateForCompiler path to initialize/verify queries (called during system creation for the injected system). Implementation is minimal in this decompiled view.

  • protected override void OnCreateForCompiler() : System.Void
    Called by the generated system creation path to call __AssignQueries and assign type handles.

  • UpdateSearchTreeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Implements the per-chunk logic described above — adding/updating/removing triangle entries in m_SearchTree and maintaining m_TriangleCount. Burst compiled for performance.

  • TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the EntityTypeHandle, BufferTypeHandles (Node, Triangle), and ComponentTypeHandles (Created, Deleted, Batch, PrefabRef) plus the ComponentLookup for AreaGeometryData from the provided SystemState. Marked AggressiveInlining.


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // Example: obtain SearchSystem and register a job as a reader to avoid races.
    var world = World.DefaultGameObjectInjectionWorld;
    var searchSystem = world?.GetExistingSystemManaged<Game.Areas.SearchSystem>();
    if (searchSystem != null)
    {
        // Suppose we schedule some job that reads the search tree:
        JobHandle myReadJobHandle = /* schedule job that reads the search tree */ default;

        // Register as a reader so the SearchSystem knows about the dependency.
        searchSystem.AddSearchTreeReader(myReadJobHandle);
    }
}

Notes on correct usage: - When you schedule jobs that access the search tree directly, always call AddSearchTreeReader/AddSearchTreeWriter on the SearchSystem with the job handle so the system can correctly sequence its own update jobs and avoid race conditions. - If you need direct access to the NativeQuadTree for a job, call GetSearchTree(readOnly, out dependencies[, out triangleCount]) and use the returned dependencies when scheduling your job. - PreDeserialize is used internally to force a full rebuild after loading; you typically don't call it manually unless implementing custom serialization flows.