Skip to content

Game.Serialization.PrimaryPrefabReferencesSystem

Assembly: Assembly-CSharp
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
PrimaryPrefabReferencesSystem is an ECS system used by the game to validate and fix Entity prefab references after load/serialization and during runtime when prefab entity IDs may need remapping. It obtains a PrefabReferences struct (from CheckPrefabReferencesSystem) and runs a set of Burst-compiled IJobChunk jobs in parallel to iterate relevant archetype chunks and call PrefabReferences.Check on component fields and buffer elements that hold prefab Entity references. It also calls PatchReferences on several other systems (CityConfigurationSystem, ClimateSystem, TerrainMaterialSystem) to let them correct their own references. The system uses cached EntityQuery objects and a generated TypeHandle to obtain component/buffer handles efficiently and coordinates job dependencies via JobHandle combinations and AddPrefabReferencesUser on the CheckPrefabReferencesSystem.


Fields

  • private CheckPrefabReferencesSystem m_CheckPrefabReferencesSystem
    Reference to the system that produces PrefabReferences and coordinates remapping checks.

  • private CityConfigurationSystem m_CityConfigurationSystem
    Used to call PatchReferences so city configuration data can update any prefab references.

  • private ClimateSystem m_ClimateSystem
    Used to call PatchReferences to update climate-related prefab references.

  • private TerrainMaterialSystem m_TerrainMaterialSystem
    Used to call PatchReferences to update terrain material related prefab references.

  • private EntityQuery m_PrefabRefQuery
    Query for entities with PrefabRef components (excludes certain temp/effect/net types).

  • private EntityQuery m_SetLevelQuery
    Query for entities with UnderConstruction components.

  • private EntityQuery m_CompanyDataQuery
    Query for entities with CompanyData components.

  • private EntityQuery m_PolicyQuery
    Query for entities that have Policy buffer components.

  • private EntityQuery m_ActualBudgetQuery
    (Declared but not used in the shown code — present as a field.)

  • private EntityQuery m_ServiceBudgetQuery
    Query for entities with ServiceBudgetData buffers.

  • private EntityQuery m_VehicleModelQuery
    Query for VehicleModel components.

  • private EntityQuery m_EditorContainerQuery
    Query for Game.Tools.EditorContainer components.

  • private EntityQuery m_AtmosphereQuery
    Query for AtmosphereData components.

  • private EntityQuery m_BiomeQuery
    Query for BiomeData components.

  • private EntityQuery m_ChirpQuery
    Query for Game.Triggers.Chirp components and their buffer ChirpEntity.

  • private EntityQuery m_SubReplacementQuery
    Query for SubReplacement buffers (network substitution data).

  • private TypeHandle __TypeHandle
    Generated struct that caches ComponentTypeHandle / BufferTypeHandle / ComponentLookup instances used by the IJobChunk jobs. It includes an __AssignHandles method to populate handles from a SystemState.

  • Nested IJobChunk structs (Burst compiled):

  • FixPrefabRefJob — fixes PrefabRef.m_Prefab
  • FixUnderConstructionJob — fixes UnderConstruction.m_NewPrefab
  • FixCompanyDataJob — fixes CompanyData.m_Brand
  • FixPolicyJob — fixes elements in Policy buffers
  • FixServiceBudgetJob — fixes ServiceBudgetData buffers (m_Service)
  • FixAtmosphereJob — fixes AtmosphereData.m_AtmospherePrefab
  • FixBiomeJob — fixes BiomeData.m_BiomePrefab
  • FixVehicleModelJob — fixes VehicleModel.m_PrimaryPrefab and m_SecondaryPrefab
  • FixEditorContainerJob — fixes EditorContainer.m_Prefab
  • FixChirpJob — fixes Chirp.m_Sender and ChirpEntity.m_Entity (only when a PrefabData component exists on the referenced entity)
  • FixSubReplacementJob — fixes SubReplacement buffer entries (m_Prefab)

Each job iterates chunks/buffers and calls PrefabReferences.Check(ref entity) when a referenced Entity is non-null (or when PrefabData exists in the Chirp case).

Properties

  • No public properties are declared on this system.

Constructors

  • public PrimaryPrefabReferencesSystem()
    Default parameterless constructor. Systems are normally created/managed by the Worlds/EntityManager (via World.GetOrCreateSystemManaged).

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: obtains references to dependent systems (CheckPrefabReferencesSystem, CityConfigurationSystem, ClimateSystem, TerrainMaterialSystem) and constructs EntityQuery instances for all component/buffer types that contain prefab references. This sets up queries used by the Burst jobs.

  • protected override void OnUpdate() : System.Void
    Main update method. It:

  • Retrieves PrefabReferences from CheckPrefabReferencesSystem (and an initial JobHandle dependency).
  • Combines that dependency with this system's base.Dependency.
  • Constructs each job struct instance, providing component/buffer handles from the TypeHandle and the PrefabReferences instance.
  • Schedules all jobs as parallel IJobChunk work using JobChunkExtensions.ScheduleParallel(...) and their respective EntityQuery.
  • Completes immediate dependencies required to call PatchReferences on other systems (m_CityConfigurationSystem, m_ClimateSystem, m_TerrainMaterialSystem).
  • Combines job handles and registers the combined dependency with CheckPrefabReferencesSystem via AddPrefabReferencesUser(...) to ensure remapping work is tracked.
  • Stores the final combined JobHandle into base.Dependency.

The method ensures all prefab-referencing fields are validated/updated in parallel and coordinates dependencies properly so referencing systems can apply their own patches.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Generated helper used at compile-time to assign queries. In the extracted source it only constructs and disposes a temporary EntityQueryBuilder (this is part of generated boilerplate).

  • protected override void OnCreateForCompiler() : System.Void
    Compiler-generated method used to initialize TypeHandle and queries in contexts where code generation expects that helper. It calls __AssignQueries and __TypeHandle.__AssignHandles.

  • TypeHandle.__AssignHandles(ref SystemState state) : System.Void
    Generated method (inside the nested TypeHandle struct) that populates ComponentTypeHandle, BufferTypeHandle and ComponentLookup fields using state.GetComponentTypeHandle / GetBufferTypeHandle / GetComponentLookup. This is used to prepare handles for job execution.

  • Nested IJobChunk Execute methods (per job)
    Each job implements Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) to iterate native arrays or buffers and call m_PrefabReferences.Check(ref someEntity) for each relevant referenced Entity.

Usage Example

// Systems are usually managed by the World/EntityManager. You typically don't construct this system directly.
// Example: get the system and ensure it exists before a custom system that uses prefab references.
protected override void OnCreate()
{
    base.OnCreate();
    var primaryPrefabSystem = World.GetOrCreateSystemManaged<Game.Serialization.PrimaryPrefabReferencesSystem>();
    // The system will run automatically by the ECS; you can also use it to ensure prefab remapping work
    // has been registered via CheckPrefabReferencesSystem if you implement custom loading/serialization logic.
}

Notes and tips for modders: - This system runs Burst-compiled IJobChunk jobs and expects PrefabReferences.Check to be safe for parallel use (it is provided by CheckPrefabReferencesSystem). - If your custom components hold Entity references to prefabs, follow the game's pattern: register/check them through CheckPrefabReferencesSystem or expose PatchReferences hooks so remapping is applied consistently. - Avoid manipulating the internal TypeHandle or scheduling conflicting jobs that mutate the same components without proper dependency chaining — use the same dependency patterns (JobHandle.CombineDependencies / AddPrefabReferencesUser) to coordinate with prefab-fixing jobs.