Skip to content

Game.Buildings.BuildingPoliciesSystem

Assembly:
Assembly-CSharp.dll (core game assembly; actual assembly name may vary per build/mod)

Namespace:
Game.Buildings

Type:
public class BuildingPoliciesSystem : GameSystemBase

Base:
GameSystemBase

Summary:
BuildingPoliciesSystem is an ECS system responsible for applying and propagating building policy changes (Modify components) to building-related data structures. It inspects Modify components (policy modifications) and updates:

  • Building option masks when buildings are destroyed, on fire, or when an Extension is disabled.
  • InstalledUpgrade buffer entries for owners so installed upgrade entities reflect the current option mask.
  • Icon notifications for turned-off building options via the IconCommandSystem using a BuildingConfigurationData notification.

The system schedules a Burst-compiled IJobChunk (CheckBuildingsJob) to perform the bulk of work. It uses ComponentTypeHandle / ComponentLookup / BufferLookup to access entity components and a BuildingConfigurationData singleton for notification configuration. The IconCommandSystem command buffer is used to add/remove turned-off notifications in a job-safe manner.


Fields

  • private IconCommandSystem m_IconCommandSystem
    {{ The IconCommandSystem instance used to create an IconCommandBuffer. The system creates and passes a command buffer to the CheckBuildingsJob, and registers the job as a command buffer writer so icon commands are applied safely after the job completes. }}

  • private EntityQuery m_PolicyModifyQuery
    {{ Query used to find entities that either (a) have a Modify component (first query) or (b) represent service upgrades & buildings that were updated (second query). This EntityQuery is required for update (RequireForUpdate) so the system only runs when relevant changes exist. }}

  • private EntityQuery m_BuildingSettingsQuery
    {{ Query used to fetch the BuildingConfigurationData singleton. The system reads this singleton to obtain the notification prefab/ID used when a building's options are turned off. }}

  • private TypeHandle __TypeHandle
    {{ Internal helper struct that caches ComponentTypeHandle / ComponentLookup / BufferLookup instances for use inside jobs. Populated in OnCreateForCompiler via __AssignHandles. }}

Properties

  • None (no public properties exposed).
    {{ The system does not expose public properties. It operates via ECS queries and scheduled jobs. }}

Constructors

  • public BuildingPoliciesSystem()
    {{ Default parameterless constructor. The system is preserved and instantiated by the DOTS runtime / game world. No custom initialization is performed in the constructor; initialization happens in OnCreate. }}

Methods

  • protected override void OnCreate()
    {{ Initializes the system:
  • Retrieves or creates the IconCommandSystem from the world.
  • Constructs the m_PolicyModifyQuery which finds Modify components and entities representing service upgrades/buildings that have been updated (and are not deleted or temporary).
  • Sets up m_BuildingSettingsQuery to read BuildingConfigurationData.
  • Calls RequireForUpdate(m_PolicyModifyQuery) so the system only runs when relevant data is present. This is the normal place the system prepares its queries and any non-job-managed state. }}

  • protected override void OnUpdate()
    {{ Core runtime behavior:

  • Constructs and schedules the Burst-compiled CheckBuildingsJob as a JobChunk using the cached TypeHandle handles and lookups.
  • Passes the BuildingConfigurationData singleton and an IconCommandBuffer (created from m_IconCommandSystem) into the job.
  • Registers the scheduled job with the IconCommandSystem by calling AddCommandBufferWriter(jobHandle) so the icon command buffer is executed after the job.
  • Stores the returned JobHandle in base.Dependency. This method is where the policy-checking job gets scheduled each frame (or when the query triggers). }}

  • private void __AssignQueries(ref SystemState state)
    {{ Compiler-generated helper, currently no custom query assignments in the method body beyond a no-op builder disposal. It exists to support code generation patterns and is called from OnCreateForCompiler. }}

  • protected override void OnCreateForCompiler()
    {{ Compiler-time initialization helper:

  • Calls __AssignQueries to assign any queries required by generated code.
  • Calls __TypeHandle.__AssignHandles(ref state) to initialize the cached component type / lookup handles used by the job. This method ensures the TypeHandle is populated for job scheduling. }}

  • Nested type: CheckBuildingsJob (private struct, [BurstCompile], implements IJobChunk)
    {{ A Burst-compiled chunk job that does the heavy lifting. Key behavior:

  • When chunk contains Modify components: iterates Modify entries and for each:
    • Finds associated Building (or Extension) and BuildingOptionData (policy option).
    • If the policy option is marked Inactive and the Modify flags indicate Active, adds or removes the "turned off" icon notification using IconCommandBuffer.
    • If the building is Destroyed or OnFire (or an Extension is disabled), force sets the building's option mask to 2 (likely representing an inactive/override state).
    • If the entity is a ServiceUpgrade owner and the owner has an InstalledUpgrade buffer, updates the corresponding InstalledUpgrade.m_OptionMask to match the building's option mask.
  • When chunk is iterating plain Building entities (no Modify present): checks Destroyed/OnFire chunk flags and propagates building.m_OptionMask into Owner's InstalledUpgrade buffer entries that point to the building entity.
  • Uses ComponentLookup and BufferLookup for random-access reads and writes as needed. The job updates component buffers in a job-friendly manner and relies on the IconCommandBuffer to perform main-thread-safe icon operations. }}

Usage Example

// Example: Add a Modify component to trigger a policy change for a building.
// (Types such as Modify, PolicyFlags, and entity references exist in the game's ECS types.)

// Assume you have:
// - World.DefaultGameObjectInjectionWorld or other World reference
// - EntityManager em
// - Entity buildingEntity  (the building you want to modify)
// - Entity policyOptionEntity (a BuildingOptionData entity representing the policy)

// Create a Modify entity that toggles a policy on/off:
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity modifyEntity = em.CreateEntity();
em.AddComponentData(modifyEntity, new Modify {
    m_Entity = buildingEntity,
    m_Policy = policyOptionEntity,
    m_Flags = PolicyFlags.Active // or 0 to mark inactive
});

// The BuildingPoliciesSystem will pick up the Modify entity via its m_PolicyModifyQuery
// on the next update and will:
// - update InstalledUpgrade buffers to reflect the building's option mask,
// - add or remove the "turned off" icon notification as configured in BuildingConfigurationData,
// - enforce disabled/destroyed/on-fire behavior (option mask = 2) where applicable.

Notes: - The system relies on several game-specific component types (Modify, Building, Owner, InstalledUpgrade, BuildingConfigurationData, etc.). Ensure those types are available when interacting with the system. - The CheckBuildingsJob is Burst-compiled and uses job-safe command buffers for icon operations; do not attempt to manipulate IconCommandSystem state directly inside the job.