Game.BudgetApplySystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
Applies the city budget each system update. Collects income and expenses from CityServiceBudgetSystem, converts them into per-update changes, enqueues Statistic events for each income/expense source, and updates the City's PlayerMoney component. The core work is performed inside a Burst-compiled IJob (BudgetApplyJob) that reads income/expense arrays and writes the PlayerMoney component and statistics queue.
Fields
-
public static readonly int kUpdatesPerDay
Number of logical updates considered one "day" for budgeting calculations (1024). Used to scale per-update changes to daily values and to divide the summed income/expenses before applying them to PlayerMoney. -
private CitySystem m_CitySystem
Cached reference to the CitySystem used to obtain the City entity (m_City) for PlayerMoney lookups/updates. -
private CityServiceBudgetSystem m_CityServiceBudgetSystem
Reference to the system that provides income and expense NativeArraydata. The arrays are obtained in OnUpdate and passed to the job. -
private CityStatisticsSystem m_CityStatisticsSystem
Reference to the system that supplies the StatisticsEvent queue where per-source income/expense events are enqueued. -
private TypeHandle __TypeHandle
Container for component lookup handles (currently PlayerMoney). It's assigned in OnCreateForCompiler and used in OnUpdate to obtain a ComponentLookupfor the job via InternalCompilerInterface.
Properties
- This type exposes no public properties.
Constructors
public BudgetApplySystem()
Default constructor. No special initialization; system setup is performed in OnCreate/OnCreateForCompiler.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system's update interval as 262144 / kUpdatesPerDay. With the defined constant (1024) this resolves to 256 ticks. This controls how often the system is scheduled relative to the engine's update phases. -
[Preserve] protected override void OnCreate()
Called when the system is created. Acquires references to CitySystem, CityServiceBudgetSystem and CityStatisticsSystem via World.GetOrCreateSystemManaged and stores them in the corresponding fields. -
[Preserve] protected override void OnUpdate()
Prepares and schedules the Burst-compiled BudgetApplyJob: - Obtains a ComponentLookup
via InternalCompilerInterface and the stored TypeHandle. - Gets the expense and income NativeArray
from CityServiceBudgetSystem (with their dependencies). - Gets the StatisticsEvent queue from CityStatisticsSystem (with its dependency) and uses its ParallelWriter.
- Fills the job struct with these references and schedules it with combined dependencies.
-
Registers the returned job dependency with CityServiceBudgetSystem (as an array reader) and CityStatisticsSystem (as a writer) to ensure correct dependency chaining.
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Generated helper used by the compiler path to assign EntityQuery objects. Here it builds and immediately disposes an empty EntityQueryBuilder (no queries used by this system). -
protected override void OnCreateForCompiler()
Called by compiler-generated code paths to initialize query assignments and component lookup handles by calling __AssignQueries and __TypeHandle.__AssignHandles. -
private struct TypeHandle
Holds component lookup handles used by this system: public ComponentLookup<PlayerMoney> __Game_City_PlayerMoney_RW_ComponentLookup
— read/write lookup for PlayerMoney.-
public void __AssignHandles(ref SystemState state)
— assigns the lookup from the provided SystemState (calls state.GetComponentLookup()). -
private struct BudgetApplyJob : IJob
Burst-compiled job that performs the core logic: - Fields:
public NativeArray<int> m_Income
— income values per IncomeSource.public NativeArray<int> m_Expenses
— expense values per ExpenseSource.public ComponentLookup<PlayerMoney> m_PlayerMoneys
— component lookup for the City entity PlayerMoney component.public NativeQueue<StatisticsEvent>.ParallelWriter m_StatisticsEventQueue
— parallel writer to enqueue statistics events.public Entity m_City
— the City entity whose PlayerMoney will be updated.
- Execute():
- Iterates ExpenseSource indices (0..14), reads each expense via CityServiceBudgetSystem.GetExpense(...), subtracts it from an accumulator and enqueues a StatisticsEvent for each expense (statistic = Expense, change = abs(expense / kUpdatesPerDay), parameter = expense source index).
- Iterates IncomeSource indices (0..13), reads each income via CityServiceBudgetSystem.GetIncome(...), adds it to accumulator and enqueues a StatisticsEvent for each income (statistic = Income, change = abs(income / kUpdatesPerDay), parameter = income source index).
- Reads the PlayerMoney component for the city, calls Add(accumulator / kUpdatesPerDay) to apply the per-update net change to city funds, and writes the PlayerMoney back to the ComponentLookup.
Notes: - The job uses math.abs on income/expense divided by kUpdatesPerDay to produce a per-update float change for statistics; the actual money change applied to PlayerMoney is the integer accumulator divided by kUpdatesPerDay. - The job is marked with [BurstCompile] to run with Burst where applicable. - Dependencies from getting income/expense arrays and statistics queue are combined with base.Dependency when scheduling.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_CitySystem = base.World.GetOrCreateSystemManaged<CitySystem>();
m_CityServiceBudgetSystem = base.World.GetOrCreateSystemManaged<CityServiceBudgetSystem>();
m_CityStatisticsSystem = base.World.GetOrCreateSystemManaged<CityStatisticsSystem>();
}
Additional notes: - This system is mostly data-driven and intended to run on the main simulation schedule; modders who want to influence budget processing should interact with CityServiceBudgetSystem (to change income/expense arrays) or CityStatisticsSystem (to read/observe events). - Ensure dependency registration (AddArrayReader / AddWriter) is respected if creating custom systems that produce or consume the same arrays/queues to avoid race conditions.