Game.Objects.SearchSystem
Assembly: Assembly-CSharp
Namespace: Game.Objects
Type: class
Base: GameSystemBase, IPreDeserialize
Summary:
SearchSystem maintains spatial search data structures (quadtrees) for in-game objects to support efficient spatial queries (e.g., selection, visibility, collisions). It updates two NativeQuadTree
Fields
-
private ToolSystem m_ToolSystem
m_ToolSystem is a reference to the game's ToolSystem (retrieved from the World) used here to check editor mode (affects how bounds masks are computed). -
private EntityQuery m_UpdatedStaticsQuery
EntityQuery that matches static objects whose components were updated (or deleted). Used to only reprocess statics that changed. -
private EntityQuery m_AllStaticsQuery
EntityQuery matching all static objects (excluding Temp). Used when the trees need a full rebuild (e.g., after load). -
private NativeQuadTree<Entity, QuadTreeBoundsXZ> m_StaticSearchTree
QuadTree storing static objects with their bounds and metadata (BoundsMask, LOD). Allocated persistently. -
private NativeQuadTree<Entity, QuadTreeBoundsXZ> m_MovingSearchTree
QuadTree storing moving/dynamic objects with their bounds and metadata. -
private JobHandle m_StaticReadDependencies
Accumulated read dependency handle for jobs that read the static quadtree. Combined with writers to ensure safety. -
private JobHandle m_StaticWriteDependencies
Current write dependency handle for jobs that write/update the static quadtree. -
private JobHandle m_MovingReadDependencies
Accumulated read dependency handle for jobs that read the moving quadtree. -
private JobHandle m_MovingWriteDependencies
Current write dependency handle for jobs that write/update the moving quadtree. -
private bool m_Loaded
Internal flag set during PreDeserialize to indicate a full rebuild is required on the next update. -
private TypeHandle __TypeHandle
Holds cached EntityTypeHandle, ComponentTypeHandle and ComponentLookup instances used by the UpdateSearchTreeJob. Populated via __AssignHandles in OnCreateForCompiler. -
private struct UpdateSearchTreeJob
Burst-compiled IJobChunk that iterates matching chunks and either Adds/Updates/Removes entries in the provided NativeQuadTree. It: - Reads entity, transform, prefab reference, stack, owner and various marker/flag components.
- Uses ComponentLookup to fetch prefab geometry, stack and net data to compute accurate Bounds3.
- Builds a BoundsMask based on flags: IsTree, OccupyZone, NotWalkThrough, HasLot, NotOverridden, layer-based masks, and culling info.
- Adds or updates the quadtree entry (QuadTreeBoundsXZ) with bounds, mask and LOD.
- Removes entries for entities with Deleted component.
- Handles fallback cases where no prefab geometry is found by generating a small default bound and LOD.
-
Runs in Burst (fast, multi-threaded) and accepts the search tree as a native structure to update.
-
private struct TypeHandle
Container for all the component and lookup handles required by UpdateSearchTreeJob. Provides __AssignHandles(ref SystemState) which populates handles using the system state (GetEntityTypeHandle, GetComponentTypeHandle, GetComponentLookup).
Properties
- (No public properties exposed by this system)
Constructors
public SearchSystem()
Default constructor. The system initialization logic happens in OnCreate.
Methods
protected override void OnCreate()
Initializes the system:- Gets ToolSystem reference.
- Creates two EntityQuery instances: one for updated statics and one for all statics.
- Allocates both static and moving NativeQuadTree instances (persistent, initial node size 1f).
-
Prepares internal state for runtime operations.
-
protected override void OnDestroy()
Disposes resources and ensures outstanding dependencies are completed: - Completes m_StaticReadDependencies and m_StaticWriteDependencies, then disposes m_StaticSearchTree.
- Completes m_MovingReadDependencies and m_MovingWriteDependencies, then disposes m_MovingSearchTree.
-
Calls base.OnDestroy().
-
private bool GetLoaded()
Checks and clears the m_Loaded flag. Returns true if a full rebuild has been requested (m_Loaded was true), otherwise false. Used to choose between processing all statics or just updated statics. -
protected override void OnUpdate()
Main update loop that schedules the UpdateSearchTreeJob when there are statics to process: - Chooses either m_AllStaticsQuery (when loaded) or m_UpdatedStaticsQuery.
- If the query is not empty, it constructs UpdateSearchTreeJob populated with component handles/lookups, editor mode, load flag and the search tree (and obtains job dependencies for the tree).
- Schedules the job via JobChunkExtensions.Schedule, combines dependencies and assigns base.Dependency.
-
Calls AddStaticSearchTreeWriter to register the returned dependency as the current static writer.
-
public NativeQuadTree<Entity, QuadTreeBoundsXZ> GetStaticSearchTree(bool readOnly, out JobHandle dependencies)
Returns the static search tree instance and supplies appropriate JobHandle dependencies: - If readOnly is true, dependencies = m_StaticWriteDependencies (reads must wait for existing writer).
- If readOnly is false, dependencies = Combine(m_StaticReadDependencies, m_StaticWriteDependencies) (writer must wait for prior readers/writers).
-
Callers must respect the returned dependencies to avoid race conditions.
-
public NativeQuadTree<Entity, QuadTreeBoundsXZ> GetMovingSearchTree(bool readOnly, out JobHandle dependencies)
Same behavior as GetStaticSearchTree but for the moving search tree and moving read/write dependency handles. -
public void AddStaticSearchTreeReader(JobHandle jobHandle)
Registers a job that will read the static search tree by combining its handle into m_StaticReadDependencies. Use this when scheduling jobs that only read the quadtree. -
public void AddStaticSearchTreeWriter(JobHandle jobHandle)
Sets m_StaticWriteDependencies to the provided job handle (a writer). Writers replace the previous writer and readers should wait accordingly. -
public void AddMovingSearchTreeReader(JobHandle jobHandle)
Registers a reader job handle for the moving search tree (combines into m_MovingReadDependencies). -
public void AddMovingSearchTreeWriter(JobHandle jobHandle)
Sets the writer job handle for the moving search tree (m_MovingWriteDependencies). -
public void PreDeserialize(Context context)
Called before deserialization/load. Ensures both static and moving search trees are cleared safely: -
Obtains both search trees (non-readonly), completes any outstanding dependencies, clears the trees and sets m_Loaded = true so OnUpdate rebuilds everything on the next frame.
-
private void __AssignQueries(ref SystemState state)
Compiler-generated helper; in this class it currently just constructs an EntityQueryBuilder and disposes it (placeholder for query assignment at compile-time). -
protected override void OnCreateForCompiler()
Compiler helper that calls __AssignQueries and TypeHandle.__AssignHandles to initialize handles used by jobs. -
private struct UpdateSearchTreeJob.Execute(...)
(Described under the UpdateSearchTreeJob field) The actual method that iterates ArchetypeChunk contents and manipulates the NativeQuadTree entries accordingly. Handles removal for Deleted components and either Add or Update based on m_Loaded or Created flag.
Usage Example
// Example usage inside another system or MonoBehaviour that has a reference to World/system:
SearchSystem searchSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<SearchSystem>();
// 1) Read-only access to the static search tree (e.g., for spatial queries on the main thread)
JobHandle deps;
var staticTree = searchSystem.GetStaticSearchTree(readOnly: true, out deps);
// Ensure we respect dependencies (e.g., complete or combine as appropriate)
deps.Complete(); // or use in a job and call AddStaticSearchTreeReader(jobHandle)
// 2) Scheduling a writer job: after scheduling your job that modifies the tree, register it
// (yourJobHandle is the handle returned when scheduling your job)
searchSystem.AddStaticSearchTreeWriter(yourJobHandle);
// 3) Clearing/rebuilding on load is handled automatically, but you can force a pre-deserialize clear:
searchSystem.PreDeserialize(new Context());
Notes: - When using the provided Get...Tree methods, always honor the supplied JobHandle dependencies to avoid race conditions with the system's scheduled UpdateSearchTreeJob. - The UpdateSearchTreeJob is Burst-compiled and expects its component handles/lookups to be assigned by the system; do not attempt to construct that job manually without the correct handles.