Skip to content

Game.ResourcesInitializeSystem

Assembly: Assembly-CSharp (game code)
Namespace: Game.Buildings

Type: class

Base: GameSystemBase

Summary: ResourcesInitializeSystem is an ECS system responsible for initializing and updating building resource buffers and resource-related state when buildings are created or updated. It: - Finds entities that have a PrefabRef and Resources buffer and that were Created or Updated (excluding temporary or service-upgrade-only entities). - Applies initial resources from Prefab initial resource data to a building's Resources buffer. - Aggregates service upkeep data and storage targets from prefabs and their installed upgrades. - Computes resource availability for ResourceConsumer components (both on the building and on installed upgrades) by combining service upkeep data, storage targets and current resources. - Runs the work in a parallel IJobChunk (InitializeCityServiceJob) and registers a dependency reader with the ResourceSystem.

The system interacts with: - ResourceSystem to obtain resource prefabs and register a job dependency as a reader. - Prefab component lookups (InitialResourceData, ServiceUpkeepData, ResourceData). - Upgrade and Created components to incorporate installed upgrades into calculations.


Fields

  • private EntityQuery m_Additions This EntityQuery selects entities that have a Resources buffer and a PrefabRef, and that are either Created or Updated, but not Temp or ServiceUpgrade. The system requires this query to run (RequireForUpdate).

  • private ResourceSystem m_ResourceSystem Cached reference to the ResourceSystem (retrieved in OnCreate). Used to obtain ResourcePrefabs and to register a job dependency reader.

  • private TypeHandle __TypeHandle Internal generated container holding ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup instances used by the job. Populated via __AssignHandles during compiler-time setup.

  • private struct InitializeCityServiceJob Nested IJobChunk that performs the per-chunk initialization and resource availability calculations. It holds component handles/lookups passed from the system and contains:

  • Execute(...) — main job loop over chunk entities.
  • ProcessAddition(Entity prefab, DynamicBuffer resources) — applies InitialResourceData from a prefab to the Resources buffer.
  • AddStorageTargets(NativeArray storageTargets, DynamicBuffer upkeeps) — accumulates storage targets for material resources from ServiceUpkeepData.
  • IJobChunk.Execute wrapper.

  • private struct TypeHandle Nested struct with strongly-typed handles and a method __AssignHandles(ref SystemState) that initializes them from the system state.

Properties

  • (none) This system does not expose public properties. All state is internal/private and managed by the ECS world lifecycle.

Constructors

  • public ResourcesInitializeSystem() Default parameterless constructor (preserved/used by the runtime). Initialization logic that requires world/system context runs in OnCreate rather than the constructor.

Methods

  • protected override void OnCreate() : System.Void Called when the system is created. This method:
  • Caches the ResourceSystem via base.World.GetOrCreateSystemManaged().
  • Constructs the EntityQuery (m_Additions) to select entities with Resources + PrefabRef and (Created or Updated) and none of (Temp, ServiceUpgrade).
  • Calls RequireForUpdate(m_Additions) to ensure the system only runs when there are matching entities.

  • protected override void OnUpdate() : System.Void Schedules the InitializeCityServiceJob in parallel across m_Additions:

  • Initializes the job's component type handles, buffer handles and lookups via InternalCompilerInterface and the __TypeHandle.
  • Sets m_ResourcePrefabs from m_ResourceSystem.GetPrefabs().
  • Schedules the job with JobChunkExtensions.ScheduleParallel and assigns the returned job handle to base.Dependency.
  • Registers the dependency with m_ResourceSystem by calling m_ResourceSystem.AddPrefabsReader(base.Dependency).

  • protected override void OnCreateForCompiler() : System.Void Compiler-time helper invoked to perform query and handle assignments during generated code setup. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignQueries(ref SystemState state) : System.Void Generated stub that can construct or assign EntityQueryBuilder state. (In this file it only instantiates and disposes a temporary EntityQueryBuilder; real handle assignments occur in OnCreate/OnCreateForCompiler.)

  • InitializeCityServiceJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void The job's main execution:

  • Allocates temporary NativeArray storageTargets sized to EconomyUtils.ResourceCount.
  • Allocates a NativeList to accumulate upkeep data.
  • Reads PrefabRef, Resources buffer, InstalledUpgrade buffers, Created and ResourceConsumer components from the chunk.
  • For each entity in the chunk:
    • Applies initial resource values defined on the prefab (and on created upgrades) to the Resources buffer (via ProcessAddition).
    • Aggregates ServiceUpkeepData from prefab and active installed upgrades into storage targets and nativeList.
    • Computes resource availability bytes using CityServiceUpkeepSystem.GetResourceAvailability and writes into the building's ResourceConsumer component (if present).
    • For installed upgrades that are themselves ResourceConsumers, compute and update their m_ResourceAvailability (either combined upkeep or max availability).
  • Uses several lookups (m_Prefabs, m_InitialResourceDatas, m_ServiceUpkeepDatas, m_ResourceDatas, m_ResourceConsumers, m_CreatedDatas) to access prefab and component data.

  • InitializeCityServiceJob.ProcessAddition(Entity prefab, DynamicBuffer resources) : System.Void Applies initial resources from the prefab's InitialResourceData buffer to the target Resources buffer using EconomyUtils.AddResources.

  • InitializeCityServiceJob.AddStorageTargets(NativeArray storageTargets, DynamicBuffer upkeeps) : System.Void Scans ServiceUpkeepData entries and, for material resources, increments the corresponding storageTargets entry by the upkeep amount. Uses EconomyUtils.IsMaterial and EconomyUtils.GetResourceIndex.

  • TypeHandle.__AssignHandles(ref SystemState state) : System.Void Sets up all ComponentTypeHandle, BufferTypeHandle and ComponentLookup/BufferLookup fields used by InitializeCityServiceJob by querying the passed SystemState.

  • InitializeCityServiceJob.IJobChunk.Execute(...) wrapper Explicit interface implementation forwarding to the job's Execute method.

Usage Example

The system is an automatic ECS system and runs when the world contains matching entities. You generally don't call its internals directly. Example of ensuring the system exists and letting the ECS world run it:

// Obtain or create the system in your mod initialization code
var world = World.DefaultGameObjectInjectionWorld;
var resourcesInitializeSystem = world.GetOrCreateSystemManaged<Game.Buildings.ResourcesInitializeSystem>();

// The system will run automatically during the world update loop when there are
// entities that match its query (have PrefabRef + Resources and are Created/Updated).
// If you need to wait for it or ensure ordering relative to other manual jobs, you can
// access its Dependency (job handle) via the system's scheduling behavior in the ECS world.

Notes and tips: - This system relies heavily on prefab buffer lookups (InitialResourceData, ServiceUpkeepData). Adding or modifying those prefab buffers requires ensuring their data is present and correct. - Calculations are done per-chunk in a parallel job; be careful when changing component access modes (read/write) or lookups to preserve thread-safety. - Resource indexing and material checks use EconomyUtils and ResourcePrefabs obtained from ResourceSystem — modifications to those utilities may affect how storage targets and availability are computed.