Skip to content

Game.Tools.GenerateObjectsSystem

Assembly: Assembly-CSharp
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
GenerateObjectsSystem is a game system responsible for creating, updating, reusing and deleting in-game placeable objects based on CreationDefinition / ObjectDefinition / NetCourse entities. It collects creation requests, tries to match them against existing objects (to reuse / relocate / upgrade), and issues entity command buffer operations (create, set components, add/remove components) to materialize the requested objects. The system heavily uses Unity DOTS jobs (IJobChunk, IJob, IJobParallelForDefer) and Native containers to parallelize scans, matching and creation work, and integrates with other core systems like ToolSystem, SimulationSystem, ModificationBarrier1 and the Net search tree.


Fields

  • private ToolSystem m_ToolSystem
    Reference to the global ToolSystem; used to determine editor mode and other tool-related states.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem; used to obtain current simulation frame index for cost/age calculations.

  • private ModificationBarrier1 m_ModificationBarrier
    Barrier used to obtain an EntityCommandBuffer.ParallelWriter for safely applying structural changes from jobs.

  • private Game.Net.SearchSystem m_NetSearchSystem
    Reference to the Net search system used to obtain the quad-tree for net-related spatial queries.

  • private EntityQuery m_DefinitionQuery
    EntityQuery used to find pending CreationDefinition entities (with Updated) and related ObjectDefinition / NetCourse components.

  • private EntityQuery m_DeletedQuery
    EntityQuery used to iterate existing objects that are marked Deleted and Temp (used to build a map of old objects for reuse).

  • private EntityQuery m_EconomyParameterQuery
    EntityQuery used to fetch EconomyParameterData singleton required for cost calculations.

  • private ComponentTypeSet m_SubTypes
    ComponentTypeSet used to add sub-type components to created entities (SubObject / SubNet / SubArea) when needed in editor mode.

  • private ComponentTypeSet m_StoppedUpdateFrameTypes
    ComponentTypeSet containing types referencing stopped/parked update types; used when creating stopped archetype entities.

  • private NativeHashMap<OwnerDefinition, Entity> m_ReusedOwnerMap
    Persistent native hash map used to store owner mappings for reused objects (maps OwnerDefinition to entity). Exposed to other systems via GetReusedOwnerMap.

  • private JobHandle m_OwnerMapReadDeps
    JobHandle accumulating read-side dependencies for readers of the reused owner map.

  • private JobHandle m_OwnerMapWriteDeps
    JobHandle representing the write-side dependency for the owner map updates (set when the system writes to the map).

  • private TypeHandle __TypeHandle
    Internal collection of component type handles & lookups cached/assigned via __AssignHandles for use in jobs.

Properties

  • None (the system uses methods to expose data: GetReusedOwnerMap and AddOwnerMapReader).

Constructors

  • public GenerateObjectsSystem()
    Default constructor. The System is created by the DOTS world; initialization work occurs in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes references to required systems (ToolSystem, SimulationSystem, ModificationBarrier1, Net search system), creates the persistent m_ReusedOwnerMap, sets up EntityQueries, ComponentTypeSets and calls RequireForUpdate on definition and economy queries. Prepares the system to run only when there are pending definitions.

  • protected override void OnDestroy()
    Completes outstanding owner-map job dependencies and disposes of the persistent m_ReusedOwnerMap. Cleans up before system destruction.

  • public NativeHashMap<OwnerDefinition, Entity> GetReusedOwnerMap(out JobHandle dependencies)
    Returns the internal m_ReusedOwnerMap and outputs the current write-side dependency JobHandle. Other systems can call this to read owner reuse mappings. The caller should respect returned dependencies.

  • public void AddOwnerMapReader(JobHandle dependencies)
    Register a reader dependency for the reused owner map. Combine the provided dependency with internal m_OwnerMapReadDeps to avoid races.

  • protected override void OnStopRunning()
    Completes owner map job dependencies and clears m_ReusedOwnerMap contents when the system stops running (for example during domain reload or world stop).

  • protected override void OnUpdate()
    Main system update. The method:

  • Allocates temporary NativeQueue, NativeList and NativeParallelMultiHashMap to collect creation requests and map existing objects.
  • Ensures owner map job dependencies are completed and clears reused owner map.
  • Prepares and schedules three major jobs:
    • FillCreationListJob (IJobChunk): scans CreationDefinition/ObjectDefinition/NetCourse entities and enqueues creation candidates into a creation queue. Also performs net neighborhood checks (local connect / attachments / subobjects) by querying the NetSearchTree via iterators.
    • FillOldObjectsJob (IJobChunk): scans existing objects (Deleted + Temp + PrefabRef) and fills the oldObjectMap (used later to match and reuse entities).
    • CollectCreationDataJob (IJob): dequeues the creation queue into a list, sorts and deduplicates entries, fills prefab references, and attempts to match creation entries with old objects and registers reused owners.
    • CreateObjectsJob (IJobParallelForDefer): parallel job that consumes the final creation list and issues EntityCommandBuffer operations to create or update objects, set components, add Temp/Native/Attached/Elevation/etc. It handles many flags (Create, Delete, Upgrade, Repair, Relocate, Permanent, Attach, Native, Editor-mode specifics, stacking, trees, under-construction, etc.)
  • Schedules and wires job dependencies, disposes temporary containers with appropriate dependency handles, registers the producer handle with the modification barrier and net search system, and stores the owner map write dependency for other readers.

  • private void __AssignQueries(ref SystemState state)
    Internal helper used by the compiler-generated bootstrap. (In the provided code it constructs an empty EntityQueryBuilder and disposes it; real handle assignment is performed in __AssignHandles.)

  • protected override void OnCreateForCompiler()
    Used by generated code to call __AssignQueries and assign type handles (via __TypeHandle.__AssignHandles). Prepares job handles for compilation-time path.

  • public void __AssignHandles(ref SystemState state) (actually on nested TypeHandle)
    Internal method used to cache and assign all ComponentTypeHandle / ComponentLookup / BufferLookup and EntityTypeHandle used by the jobs. Called from OnCreateForCompiler.

Notes about nested types and jobs: - The system defines several nested structs used as job data and helpers: - CreationData, OldObjectKey, OldObjectValue — small helper structs used in queues, lists and multimaps. - FillOldObjectsJob (IJobChunk) — populates oldObjectMap with existing entities that can be reused. - FillCreationListJob (IJobChunk) — scans creation definitions and net courses, performs spatial queries through NetSearchTree using EdgeIterator and NodeIterator nested iterator types and enqueues candidate CreationData entries. - CollectCreationDataJob (IJob) — consolidates and deduplicates creation queue entries into a final sorted/best list, populates m_ReusedOwnerMap when matching old entities. - CreateObjectsJob (IJobParallelForDefer) — the worker that actually creates or updates entities using EntityCommandBuffer.ParallelWriter; handles many component interactions and flag semantics. These nested jobs contain many helper methods used to test attachment/local connect, find/assign old entities, compute costs, initialize tree states, build Attached components, and more.

Usage Example

// Typical system usage is automatic within the DOTS world. Example: reading the owner reuse map from another system.

public class MyReaderSystem : SystemBase
{
    protected override void OnUpdate()
    {
        var generateSystem = World.GetExistingSystemManaged<Game.Tools.GenerateObjectsSystem>();
        if (generateSystem != null)
        {
            JobHandle deps;
            var reusedOwners = generateSystem.GetReusedOwnerMap(out deps);
            // Use 'reusedOwners' only after honoring 'deps' (combine/complete as required)
            // Example: schedule a job that depends on 'deps' and uses reusedOwners as read-only input.
            // When done, call generateSystem.AddOwnerMapReader(yourJobHandle) to register your read dependency.
        }
    }
}

Additional notes / tips: - The system is heavily optimized for DOTS: creating/updating objects happens via a sequence of jobs to minimize main-thread work. If you extend or hook into this system, respect the job dependencies (GetReusedOwnerMap / AddOwnerMapReader) to avoid race conditions. - The CreateObjectsJob performs many checks and component operations; modding changes to component layouts or prefab flags may require careful updates to the job code or the TypeHandle assignments.