Skip to content

Game.Prefabs.ObjectInitializeSystem

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
Initializes object prefabs (placeable objects, geometry prefabs, markers, asset stamps, spawnable placeholders, sub-objects, sub-nets, sub-lanes, sub-areas, character elements, mesh groups, stack/quantity/creature metadata, LOD, bounds and related flags) when prefabs are created/loaded. Runs both immediate CPU initialization and schedules Burst jobs to compute placeholder/subobject requirements and sub-net snapping. Sets up Entity buffers and component data used by runtime object systems (rendering, spawning, placement, physics rules, etc.). Uses multiple nested job structs (Burst-compiled) to parallelize parts of the initialization for performance.


Fields

  • private EntityQuery m_PrefabQuery
    Initial query used to iterate prefab entities that contain PrefabData + ObjectData and that are created/updated. Required for update scheduling.

  • private EntityQuery m_PlaceholderQuery
    Query used to find placeholder relations (entities with PlaceholderObjectElement buffers and ObjectData, excluding Deleted). Used by FixPlaceholdersJob.

  • private PrefabSystem m_PrefabSystem
    Cached reference to the PrefabSystem (retrieved in OnCreate) used to get prefab instances, entity IDs and prefab components.

  • private TypeHandle __TypeHandle
    Internal aggregated type handle structure used to cache component, buffer and lookup handles across jobs and main thread work. Contains many ComponentTypeHandle/BufferTypeHandle/ComponentLookup fields assigned in __AssignHandles.


Properties

  • None

Constructors

  • public ObjectInitializeSystem()
    Default constructor. The system relies on OnCreate / OnCreateForCompiler to set up required queries, the TypeHandle and to get the PrefabSystem.

Methods

  • protected override void OnCreate()
    Sets up queries and retrieves the PrefabSystem. Creates:
  • m_PrefabQuery: selects entities with PrefabData + ObjectData and either Created or Deleted
  • m_PlaceholderQuery: selects object entities that have PlaceholderObjectElement buffers and are not Deleted Marks the prefab query as required for update (RequireForUpdate).

  • protected override void OnUpdate()
    Main initialization routine that:

  • Collects matching archetype chunks for m_PrefabQuery.
  • Iterates chunks on the main thread to initialize a large set of Object-related component data (ObjectGeometryData, PlaceableObjectData, SpawnableObjectData, AssetStampData, GrowthScaleData, StackData, QuantityObjectData, CreatureData, BuildingTerraformData, BuildingExtensionData, etc.).
  • Uses PrefabSystem to access prefab components (ObjectPrefab, ObjectSubObjects, ObjectSubNets, ObjectSubLanes, ObjectSubAreas, SpawnableObject, AssetStampPrefab etc.) and to convert prefab references to Entities.
  • Computes bounds, LODs, mesh groups, character elements, stack/quantity masks, flag settings (GeometryFlags, PlacementFlags), and fills dynamic buffers (SubMesh, SubMeshGroup, SubObject, SubNet, SubLane, SubArea, SubAreaNode, CharacterElement).
  • Schedules Burst jobs:
    • FixPlaceholdersJob (when necessary) to remove placeholder references whose target entity is Deleted.
    • FindPlaceholderRequirementsJob to collect requirement/forbid flags from placeholder target objects and store them on PlaceholderObjectData.
    • FindSubObjectRequirementsJob to compute requirement masks for object subobjects recursively.
    • InitializeSubNetsJob to determine snapping for sub-nets and set PlaceableObjectData flags accordingly.
  • Combines job dependencies and sets base.Dependency appropriately. Disposes the chunk array with dependency.

  • private void InitializePrefab(AssetStampPrefab stampPrefab, ref AssetStampData assetStampData, ref PlaceableObjectData placeableObjectData, ref ObjectGeometryData objectGeometryData)
    Initializes AssetStamp-specific data: size, costs, geometry bounds, pivot, LOD and layers appropriate for large stamp prefabs.

  • private static MeshGroupFlags GetMeshGroupFlag(ObjectState state, bool inverse)
    Returns mesh group flags used for mesh grouping/requirements depending on the object state and an inverse flag (used for overridden character groups). Maps ObjectState enums to MeshGroupFlags.

  • private void InitializePrefab(ObjectGeometryPrefab objectPrefab, PlaceableObjectData placeableObjectData, ref ObjectGeometryData objectGeometryData, ref GrowthScaleData growthScaleData, ref StackData stackData, ref QuantityObjectData quantityObjectData, ref CreatureData creatureData, DynamicBuffer<SubMesh> meshes, DynamicBuffer<SubMeshGroup> meshGroups, DynamicBuffer<CharacterElement> characterElements, bool isPlantObject, bool isHumanObject, bool isBuildingObject, bool isVehicleObject, bool isCreatureObject)
    Main heavy initializer for geometry-based object prefabs. Responsibilities:

  • Iterate prefab mesh definitions and character groups to create SubMesh entries and SubMeshGroups.
  • Compute combined bounds, m_MinLod, mesh layers, LOD/shadow LOD for meshes.
  • Update stack bounds and stack metadata (StackData) using StackProperties where applicable.
  • Populate CharacterElement entries and set creature activity masks and corrective clip indices where applicable.
  • Apply decal/base flags based on object type and render prefab properties.
  • Compute object size, pivot and set geometry flags (Standing, WalkThrough, Physical, Circular, Marker etc.).
  • Adjust growth scale sizes for plant/human-specific rules.
  • Post-process mesh groups to propagate "RequireHome" where appropriate for certain combinations of flags.

  • private ushort GetRandomSeed(string name)
    Simple deterministic string hash used to produce a pseudo-random ushort seed per mesh/character (used when selecting character meshes).

  • private void UpdateStackBounds(ref Bounds1 stackBounds, ref Bounds3 meshBounds, StackProperties properties)
    Adjusts stack bounds and mesh bounds according to stack direction and ordering (First/Middle/Last), using start/end overlaps and ensures correct bounds for stacking logic.

  • private void InitializePrefab(MarkerObjectPrefab objectPrefab, PlaceableObjectData placeableObjectData, ref ObjectGeometryData objectGeometryData, DynamicBuffer<SubMesh> meshes)
    Initializes marker prefabs (single mesh marker objects): sets LODs, layers, bounds, pivot, sets GeometryFlags.Marker and circular handling.

  • private void __AssignQueries(ref SystemState state)
    Internal method used by the generated compiler path. In this implementation it just instantiates and disposes a temporary EntityQueryBuilder (keeps a placeholder for the generated pipeline).

  • protected override void OnCreateForCompiler()
    Compiler-time initializer used by DOTS codegen path: calls __AssignQueries and assigns all TypeHandle handles via __TypeHandle.__AssignHandles.

  • Nested job structs (Burst-compiled):

  • FixPlaceholdersJob : IJobChunk
    Removes entries from PlaceholderObjectElement buffers that refer to Deleted entities (keeps buffers consistent when placeholder targets are removed).
  • InitializeSubNetsJob : IJobParallelFor
    For each chunk, inspects SubNet buffers, counts node usages and sets snapping flags when a sub-net endpoint is unique and the referenced NetData allows snapping (skips certain layer types). Sets PlaceableObjectData placement flags (SubNetSnap) when any sub-net has snapping.
  • FindPlaceholderRequirementsJob : IJobParallelFor
    Collects ObjectRequirement flags (require/forbid) from objects referenced by placeholders and writes a combined m_RequirementMask on the PlaceholderObjectData of the placeholder entity.
  • FindSubObjectRequirementsJob : IJobParallelFor
    Recursively computes requirement masks for object sub-objects by inspecting subobject prefabs and placeholder-prefabs and writes the combined mask to ObjectGeometryData.m_SubObjectMask.

Notes: - The system heavily relies on PrefabSystem to convert authoring references (prefab classes) into runtime Entities and to fetch component data from prefab entities. - Many operations are sensitive to component presence (e.g., whether a chunk contains SpawnableObjectData, StackData, CreatureData, PillarData, etc.)—the code branches accordingly. - LOD, shadow LOD and MeshData are updated for referenced render prefab meshes where required.


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire the PrefabSystem the system will use during OnUpdate()
    m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();

    // Set up queries (this is already done by the system, example shown for custom systems)
    m_PrefabQuery = GetEntityQuery(new EntityQueryDesc {
        All = new ComponentType[] {
            ComponentType.ReadOnly<PrefabData>(),
            ComponentType.ReadOnly<ObjectData>()
        },
        Any = new ComponentType[] {
            ComponentType.ReadOnly<Created>(),
            ComponentType.ReadOnly<Deleted>()
        }
    });

    RequireForUpdate(m_PrefabQuery);
}