Skip to content

Game.Prefabs.CityServiceBuilding

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: class

Base: ComponentBase, IServiceUpgrade

Summary:
Component used on city service building prefabs. It declares and initializes ECS components and buffers related to service upkeep and service-building budgets. The component: - Exposes an array of ServiceUpkeepItem entries (m_Upkeeps) that are converted into ServiceUpkeepData entries in an entity DynamicBuffer during initialization. - Adds required ComponentTypes for prefab and archetype creation depending on configured upkeeps and whether a ServiceUpgrade is present on the prefab. - Ensures entities for service buildings have appropriate bookkeeping components such as CityServiceUpkeep, Resources, TripNeeded and vehicle-related tags (GuestVehicle, OwnedVehicle) when applicable. This class integrates prefab-level configuration with the Unity.Entities world for Cities: Skylines 2 service buildings.


Fields

  • public ServiceUpkeepItem[] m_Upkeeps
    Array of maintenance/upkeep definitions editable on the prefab. Each entry describes a resource and amount (and whether it scales with usage). During Initialize these are converted into ServiceUpkeepData elements and appended to the entity's ServiceUpkeepData DynamicBuffer.

  • (no other explicit fields)
    The class relies on the prefab and EntityManager APIs; no other private fields are declared.

Properties

  • (none)
    This component does not define public properties. Its behavior is driven by the m_Upkeeps field and prefab/component queries (e.g., prefab.TryGet()).

Constructors

  • public CityServiceBuilding()
    Default public constructor (compiler-generated). Typical usage is to attach this component to a prefab in the editor or create it via code; there is no special runtime construction logic beyond Unity/serialization behavior.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Populate the set of ComponentType entries required on the prefab entity. Behavior:
  • If m_Upkeeps contains entries OR the prefab has a ServiceConsumption component with m_Upkeep > 0, adds ServiceUpkeepData (read/write) to indicate the prefab provides upkeep data.
  • Always adds CollectedServiceBuildingBudgetData (read/write) to collect budget information for service buildings.
  • Called during prefab-to-entity conversion to ensure the resulting entity archetype contains the necessary buffers/components.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Called when creating archetypes for this prefab. Behavior:

  • If the prefab does not have a ServiceUpgrade component (GetComponent() == null), the method adds CityServiceUpkeep, Resources, TripNeeded, GuestVehicle and OwnedVehicle (all read/write) to the archetype.
  • These components are used to track upkeep state, resources, trips/visitors and vehicle ownership/guest status on service building entities.

  • public void GetUpgradeComponents(HashSet<ComponentType> components)
    Provides the component set required when this prefab is upgraded (used for service upgrades). Adds CityServiceUpkeep, Resources, TripNeeded and GuestVehicle (read/write). Intended for upgrade-related entity creation so upgraded variants have expected bookkeeping components.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Initializes entity state when the prefab is instantiated into the ECS world. Behavior:

  • Calls base.Initialize(entityManager, entity).
  • Adds a DynamicBuffer to the entity (entityManager.AddBuffer(entity)).
  • If m_Upkeeps is populated, iterates each ServiceUpkeepItem and appends a ServiceUpkeepData entry to the buffer. Each ServiceUpkeepData sets:
    • m_Upkeep: a ResourceStack constructed with a Resource from EconomyUtils.GetResource(serviceUpkeepItem.m_Resources.m_Resource) and the specified amount.
    • m_ScaleWithUsage: copied from the ServiceUpkeepItem.
  • This ensures entity instances carry the per-building upkeep resource requirements needed by simulation systems.

Remarks / Interactions: - This component is part of the prefab authoring pipeline bridging authorable prefab data (m_Upkeeps) into runtime ECS data structures used by economy and simulation systems. - It checks for the presence of ServiceConsumption on the prefab to determine whether to add upkeep buffers even when m_Upkeeps is not explicitly set. - The added components like CityServiceUpkeep, Resources, TripNeeded, GuestVehicle and OwnedVehicle are expected by other simulation systems (citizens, vehicles, economy) to manage building usage, resource consumption and vehicle/visitor routing.

Usage Example

// The class is normally added to a building prefab in the editor. At runtime the ECS conversion
// will call Initialize to populate the ServiceUpkeepData buffer. The following excerpt shows the
// relevant Initialize behavior (already implemented in the component):

public override void Initialize(EntityManager entityManager, Entity entity)
{
    base.Initialize(entityManager, entity);

    // Add the upkeep buffer to the entity
    DynamicBuffer<ServiceUpkeepData> dynamicBuffer = entityManager.AddBuffer<ServiceUpkeepData>(entity);

    // Populate from the prefab's m_Upkeeps array (set in the prefab inspector)
    if (m_Upkeeps != null)
    {
        foreach (ServiceUpkeepItem item in m_Upkeeps)
        {
            dynamicBuffer.Add(new ServiceUpkeepData
            {
                m_Upkeep = new ResourceStack
                {
                    m_Resource = EconomyUtils.GetResource(item.m_Resources.m_Resource),
                    m_Amount = item.m_Resources.m_Amount
                },
                m_ScaleWithUsage = item.m_ScaleWithUsage
            });
        }
    }
}

Additional notes: - If you want a building to automatically include upkeep entries based on ServiceConsumption (rather than explicitly populating m_Upkeeps), ensure the prefab has a ServiceConsumption with m_Upkeep > 0 — GetPrefabComponents will add the upkeep buffer in that case. - When authoring service building prefabs that support upgrades, ensure ServiceUpgrade presence is considered: GetArchetypeComponents will skip adding some components when ServiceUpgrade exists, while GetUpgradeComponents declares what upgrade instances need.