Skip to content

Game.ZonePrefabInitializeSystem

Assembly: Game (assembly containing the game's ECS systems)
Namespace: Game.Prefabs

Type: public class

Base: GameSystemBase

Summary:
Initializes zone prefabs after they are created. This system: - Finds newly created zone prefabs (entities with Created + PrefabData + ZoneData). - Marks office prefabs by setting the ZoneFlags.Office flag on their ZoneData when the prefab metadata indicates an office. - For industrial area prefabs (excluding office-prefabs), allocates and populates a ProcessEstimate buffer per resource based on available IndustrialProcessData and WorkplaceData (queried from the world). It computes production-per-cell and worker-production-per-cell estimates using EconomyUtils and economy parameters, and links each estimate to the corresponding process entity. These estimates are used later by economic simulation systems to drive production and workforce calculations. - Depends on PrefabSystem and ResourceSystem, and reads EconomyParameterData. It requires process and economy-parameter queries for updates.


Fields

  • private Unity.Entities.EntityQuery m_PrefabGroup
    Query selecting newly created prefab zone entities. Specifically queries entities with Created (ReadOnly), PrefabData (ReadOnly) and ZoneData (ReadOnly) to find prefabs that need initialization.

  • private Unity.Entities.EntityQuery m_ProcessGroup
    Query selecting industrial process-related entities. It reads IndustrialProcessData, IndustrialCompanyData, WorkplaceData and excludes StorageCompanyData. Used to iterate all industrial processes/workplaces when building process estimates.

  • private Unity.Entities.EntityQuery m_EconomyParameterGroup
    Query to fetch EconomyParameterData singleton. Economy parameters are used when calculating production values per company/process.

  • private PrefabSystem m_PrefabSystem
    Cached reference to the game's PrefabSystem used to look up prefab metadata (e.g., whether a zone prefab is an office).

  • private ResourceSystem m_ResourceSystem
    Cached reference to the ResourceSystem used to obtain resource prefabs and resource-related data used while building trade/production cost estimates.

  • private ZonePrefabInitializeSystem.TypeHandle __TypeHandle
    A generated nested struct instance used to cache component/handle lookups for fast chunk access during OnUpdate. See nested TypeHandle documentation below.

Nested type: TypeHandle - Contains cached handles: - EntityTypeHandle __Unity_Entities_Entity_TypeHandle (ReadOnly) - ComponentTypeHandle<PrefabData> __Game_Prefabs_PrefabData_RO_ComponentTypeHandle (ReadOnly) - ComponentTypeHandle<ZoneData> __Game_Prefabs_ZoneData_RW_ComponentTypeHandle (Read/Write) - ComponentTypeHandle<BuildingPropertyData> __Game_Prefabs_BuildingPropertyData_RO_ComponentTypeHandle (ReadOnly) - BufferTypeHandle<ProcessEstimate> __Game_Zones_ProcessEstimate_RW_BufferTypeHandle (Read/Write) - ComponentLookup<ResourceData> __Game_Prefabs_ResourceData_RO_ComponentLookup (ReadOnly) - Method: - __AssignHandles(ref SystemState state) — binds the above handles against a SystemState for later use (called from OnCreateForCompiler).

Properties

  • None exposed by this system.

Constructors

  • public ZonePrefabInitializeSystem()
    Empty constructor, marked with [Preserve] attribute in the source. No runtime initialization beyond what OnCreate does.

Methods

  • protected override void OnCreate() : System.Void
    Called when the system is created. Behavior:
  • Calls base.OnCreate().
  • Caches PrefabSystem and ResourceSystem (GetOrCreateSystemManaged).
  • Builds three EntityQuery objects:
    • m_PrefabGroup: Created + PrefabData + ZoneData
    • m_ProcessGroup: IndustrialProcessData, IndustrialCompanyData, WorkplaceData; excludes StorageCompanyData
    • m_EconomyParameterGroup: EconomyParameterData
  • Calls RequireForUpdate for m_ProcessGroup and m_EconomyParameterGroup so the system only runs when those exist.

Note: This method is marked [Preserve] in the source.

  • protected override void OnUpdate() : System.Void
    Main update logic. Behavior:
  • Early exits if m_PrefabGroup is empty.
  • Converts m_PrefabGroup to chunk array and prepares component type handles via the generated TypeHandle (via InternalCompilerInterface).
  • Retrieves EconomyParameterData singleton and resource prefabs from the ResourceSystem.
  • Converts m_ProcessGroup into arrays of process entities and component data arrays (IndustrialProcessData and WorkplaceData).
  • Iterates each archetype chunk in the prefab group:
    • Reads entity, ZoneData, BuildingPropertyData and ProcessEstimate buffer accessor for the chunk.
    • For each entity in the chunk:
    • If the prefab's ZonePrefab metadata indicates m_Office, sets the ZoneData.m_ZoneFlags |= ZoneFlags.Office.
    • If the zone's AreaType is Industrial and not an office:
      • Determines a space multiplier (from BuildingPropertyData.m_SpaceMultiplier when present).
      • Ensures the ProcessEstimate dynamic buffer has an entry for each resource (adds default entries for the resource count).
      • For each IndustrialProcessData:
      • Calculates number of workers (rounded) based on space multiplier and m_MaxWorkersPerCell.
      • Calculates workplaces via EconomyUtils.CalculateNumberOfWorkplaces.
      • Computes low-education weight and worker/production metrics.
      • Builds trade cost via EconomyUtils.BuildPseudoTradeCost.
      • Computes production-per-day (and normalizes per company update tick).
      • Fills a ProcessEstimate for the corresponding resource index with:
        • m_ProductionPerCell
        • m_WorkerProductionPerCell
        • m_LowEducationWeight
        • m_ProcessEntity (link to the process entity)
      • Writes the ProcessEstimate into the buffer at the resource index.
  • Disposes temporary NativeArrays used for processes, workplaces, chunks, and process entity arrays.

  • private void __AssignQueries(ref SystemState state)
    Generated method (compiler helper). In this implementation it simply instantiates and disposes an EntityQueryBuilder — used by the compiler-generated setup path. Called from OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Compiler helper method. Calls __AssignQueries(ref CheckedStateRef) and calls __TypeHandle.__AssignHandles(ref CheckedStateRef) to bind type handles for use by the OnUpdate generated code paths.

  • private void ZonePrefabInitializeSystem.TypeHandle.__AssignHandles(ref SystemState state)
    (Described above) Binds all internal type handles (EntityTypeHandle, ComponentTypeHandle<...>, BufferTypeHandle<...>, ComponentLookup<...>) against the provided SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Cache systems used by this initializer
    m_PrefabSystem = World.GetOrCreateSystemManaged<PrefabSystem>();
    m_ResourceSystem = World.GetOrCreateSystemManaged<ResourceSystem>();

    // Set up queries
    m_PrefabGroup = GetEntityQuery(new EntityQueryDesc {
        All = new ComponentType[] {
            ComponentType.ReadOnly<Created>(),
            ComponentType.ReadOnly<PrefabData>(),
            ComponentType.ReadOnly<ZoneData>()
        }
    });
    m_ProcessGroup = GetEntityQuery(
        ComponentType.ReadOnly<IndustrialProcessData>(),
        ComponentType.ReadOnly<IndustrialCompanyData>(),
        ComponentType.ReadOnly<WorkplaceData>(),
        ComponentType.Exclude<StorageCompanyData>()
    );
    m_EconomyParameterGroup = GetEntityQuery(ComponentType.ReadOnly<EconomyParameterData>());

    // Only run when process and economy parameters are present
    RequireForUpdate(m_ProcessGroup);
    RequireForUpdate(m_EconomyParameterGroup);
}

Notes and tips for modders: - This system is responsible for preparing per-prefab production estimates used by the economy. If you add new IndustrialProcessData or change resource indices you will affect how ProcessEstimate buffers are populated. - If you create custom zone prefabs and want to influence initialization (e.g., mark offices or provide different building property multipliers), ensure the corresponding PrefabData / BuildingPropertyData are set correctly on the prefab so this system will pick them up. - The system uses several EconomyUtils helpers (GetResourceIndex, CalculateNumberOfWorkplaces, GetCompanyProductionPerDay, BuildPseudoTradeCost). When modifying production logic, inspect those helpers to understand how estimates are computed.