Game.UI.InGame.BudgetUISystem
Assembly:
Assembly-CSharp (game runtime assembly)
Namespace:
Game.UI.InGame
Type:
class
Base:
UISystemBase
Summary:
BudgetUISystem is a UI-facing ECS system that binds city budget data to the in-game UI. It gathers income and expense values from the CityServiceBudgetSystem, exposes total income/expenses and detailed per-source arrays to the UI via value bindings, and enumerates available budget item metadata from a UI configuration prefab. It also consults other game systems (government subsidies, city configuration, map tile purchase) to determine whether certain budget categories are active. The system is intended for use by the game's UI layer and depends on several simulation/prefab systems to provide data.
Fields
-
private const string kGroup = "budget"
Identifier/group name used for UI bindings. The bindings added in OnCreate use "budget" as the binding group. -
private PrefabSystem m_PrefabSystem
Used to fetch the UI configuration prefab (UIEconomyConfigurationPrefab) via GetConfig(). -
private GameModeGovernmentSubsidiesSystem m_GovernmentSubsidiesSystem
Consulted to determine whether the "Government" budget item is active (government subsidies enabled). -
private ICityServiceBudgetSystem m_CityServiceBudgetSystem
Primary source for numeric budget data (total income/expenses and per-source values). Methods used: GetTotalIncome(), GetTotalExpenses(), GetIncome(IncomeSource), GetExpense(ExpenseSource). -
private CityConfigurationSystem m_CityConfigurationSystem
Used to check city configuration flags such as unlimited money; affects activation of some budget items (e.g., "Loan Interest"). -
private MapTilePurchaseSystem m_MapTilePurchaseSystem
Used to determine if map tile upkeep is enabled (affects "Tile Upkeep" budget item). -
private EntityQuery m_ConfigQuery
EntityQuery used to fetch the UIEconomyConfigurationData singleton prefab via the PrefabSystem. -
private GetterValueBinding<int> m_TotalIncomeBinding
A binding that exposes the total income integer to the UI ("budget", "totalIncome"). It uses m_CityServiceBudgetSystem.GetTotalIncome() as the value provider and is updated each OnUpdate. -
private GetterValueBinding<int> m_TotalExpensesBinding
A binding that exposes the total expenses integer to the UI ("budget", "totalExpenses"). Uses m_CityServiceBudgetSystem.GetTotalExpenses() and is updated each OnUpdate. -
private RawValueBinding m_IncomeItemsBinding
A raw binding that writes the income item metadata (id, color, icon, active, sources) to the UI JSON stream. Bound to ("budget", "incomeItems"). -
private RawValueBinding m_IncomeValuesBinding
A raw binding that writes the numeric income values for each IncomeSource to the UI ("budget", "incomeValues"). Produces a fixed-length array (14 entries in current implementation). -
private RawValueBinding m_ExpenseItemsBinding
A raw binding that writes the expense item metadata (similar to income items) to the UI ("budget", "expenseItems"). -
private RawValueBinding m_ExpenseValuesBinding
A raw binding that writes the numeric expense values for each ExpenseSource to the UI ("budget", "expenseValues"). Produces a fixed-length array (15 entries in current implementation); values are negated (written as negative numbers) to reflect outflows. -
private Dictionary<string, Func<bool>> m_BudgetsActivations
A lookup mapping budget item IDs (as strings) to functions that indicate whether the corresponding budget category is active. Used when writing the "active" property for each budget item. Entries include "Government", "Loan Interest" (disabled when unlimitedMoney is true), and "Tile Upkeep".
Properties
- This class does not expose public properties. All bindings and dependencies are private fields and are managed internally by the system.
Constructors
public BudgetUISystem()
Default constructor. Marked with [Preserve] attribute on the class methods to prevent trimming; no explicit initialization other than that performed in OnCreate.
Methods
protected override void OnCreate()
Lifecycle method where the system initializes references and registers UI bindings. Actions performed:- Calls base.OnCreate().
- Obtains references to required systems: PrefabSystem, CityServiceBudgetSystem, GameModeGovernmentSubsidiesSystem, CityConfigurationSystem, MapTilePurchaseSystem.
- Creates an EntityQuery for UIEconomyConfigurationData to fetch the UI configuration prefab.
- Builds m_BudgetsActivations dictionary mapping budget item IDs to activation check functions.
- Adds multiple bindings:
- m_TotalIncomeBinding: GetterValueBinding
("budget","totalIncome", () => m_CityServiceBudgetSystem.GetTotalIncome()) - m_TotalExpensesBinding: GetterValueBinding
("budget","totalExpenses", () => m_CityServiceBudgetSystem.GetTotalExpenses()) - m_IncomeItemsBinding: RawValueBinding("budget","incomeItems", BindIncomeItems)
- m_IncomeValuesBinding: RawValueBinding("budget","incomeValues", BindIncomeValues)
- m_ExpenseItemsBinding: RawValueBinding("budget","expenseItems", BindExpenseItems)
- m_ExpenseValuesBinding: RawValueBinding("budget","expenseValues", BindExpenseValues)
- m_TotalIncomeBinding: GetterValueBinding
Note: Methods and delegates passed to the bindings will be invoked by the UI system to serialize data for the UI.
protected override void OnUpdate()
Called each frame (or UI update tick). Updates numeric bindings:- m_TotalIncomeBinding.Update()
- m_TotalExpensesBinding.Update()
- m_IncomeValuesBinding.Update()
-
m_ExpenseValuesBinding.Update() The RawValueBindings for items (income/expense items metadata) are not updated every tick here because that metadata is static per prefab; they are registered and will be serialized when requested.
-
private void BindIncomeItems(IJsonWriter writer)
Serializes income item metadata into the provided IJsonWriter. Behavior: - Fetches the UIEconomyConfigurationPrefab via GetConfig().
- Iterates config.m_IncomeItems (array of BudgetItem
). - For each BudgetItem writes a typed object "Game.UI.InGame.BudgetItem" with properties:
- id: budgetItem.m_ID
- color: budgetItem.m_Color
- icon: budgetItem.m_Icon
- active: computed via m_BudgetsActivations lookup; active if not present in the dictionary or its delegate returns true.
- sources: array of BudgetSource objects, each with:
- id: Enum.GetName(typeof(IncomeSource), incomeSource)
- index: numeric index cast from IncomeSource enum
-
Starts/wraps arrays appropriately (writer.ArrayBegin / ArrayEnd / TypeBegin / TypeEnd).
-
private void BindIncomeValues(IJsonWriter writer)
Writes an array of income numeric values to the writer. Implementation details: - Writes an array of length 14 (fixed in this implementation).
- For each index i from 0..13 writes m_CityServiceBudgetSystem.GetIncome((IncomeSource)i).
-
Assumes IncomeSource enum has up to 14 entries or that indices 0..13 are valid.
-
private void BindExpenseItems(IJsonWriter writer)
Serializes expense item metadata, analogous to BindIncomeItems but using config.m_ExpenseItems (BudgetItem) and writing ExpenseSource enum names and indices. Uses the same "active" logic via m_BudgetsActivations. -
private void BindExpenseValues(IJsonWriter writer)
Writes an array of expense numeric values to the writer. Implementation details: - Writes an array of length 15 (fixed in this implementation).
-
For each index i from 0..14 writes -m_CityServiceBudgetSystem.GetExpense((ExpenseSource)i) (negates values to represent outflows).
-
private UIEconomyConfigurationPrefab GetConfig()
Helper that returns the UIEconomyConfigurationPrefab singleton from PrefabSystem using m_ConfigQuery.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// BudgetUISystem initializes bindings to provide the UI with budget metadata and values.
// If you need to access the same data from another modged system, obtain references similarly:
var prefabSystem = World.GetOrCreateSystemManaged<PrefabSystem>();
var budgetSystem = World.GetOrCreateSystemManaged<CityServiceBudgetSystem>();
int totalIncome = budgetSystem.GetTotalIncome();
}
Additional notes for modders: - The system depends on several engine/game systems (PrefabSystem, CityServiceBudgetSystem, GameModeGovernmentSubsidiesSystem, CityConfigurationSystem, MapTilePurchaseSystem). If you write compatibility code, ensure those systems are available. - The BindIncomeValues and BindExpenseValues methods write fixed-length arrays (14 for incomes, 15 for expenses). If the IncomeSource or ExpenseSource enums change in different versions, this hard-coded length may need adjustment. - The "active" property of budget items is computed from m_BudgetsActivations. To add or override activation logic for additional budget items, modify that dictionary in OnCreate (or replace the system via Harmony/IL patching). - Methods and fields are marked with [Preserve] where needed to avoid code stripping; respect these attributes when creating your own subclasses or replacements.