Game.UI.InGame.HouseholdSidebarSection
Assembly: Assembly-CSharp (game code)
Namespace: Game.UI.InGame
Type: class
Base: InfoSectionBase
Summary:
HouseholdSidebarSection provides the logic to populate and render the in-game "Household" sidebar panel for the selected entity (citizen, household or building). It computes visibility, collects household/resident/pet data using Burst-compiled jobs (CheckVisibilityJob and CollectDataJob) and exposes the results to the UI via RawMapBindings and serialization (OnWriteProperties). The class manages Native containers (NativeArray / NativeList) lifecycle and handles mapping of entities -> UI items, icons and tooltip tags (e.g., homeless shelter). It supports three sidebar variants: Citizen, Household and Building and differentiates residence vs. temporary/home shelter cases.
Fields
-
private NativeArray<int> m_Results
Internal results from CheckVisibilityJob. m_Results[0] = visible flag (1 = visible), m_Results[1] = resident count, m_Results[2] = variant indicator (0=citizen,1=household,2=building). -
private NativeArray<Entity> m_ResidenceResult
Single-element NativeArray used to store the resolved residence entity for the selected context (or Entity.Null). -
private NativeArray<HouseholdResult> m_HouseholdResult
Single-element NativeArray used to store the primary HouseholdResult (often the selected household). -
private NativeList<ResidentResult> m_ResidentsResult
Dynamic list of per-resident data gathered by CollectDataJob. Sorted and exposed to UI. -
private NativeList<Entity> m_PetsResult
Dynamic list of household pet entities gathered by CollectDataJob. -
private NativeList<HouseholdResult> m_HouseholdsResult
Dynamic list of households (HouseholdResult) gathered from a building or selection. Sorted and mapped to UI. -
private RawMapBinding<int> m_HouseholdMap
UI binding that maps household list indices to JSON items (BindHousehold callback). -
private RawMapBinding<int> m_ResidentMap
UI binding that maps resident list indices to JSON items (BindResident callback). -
private RawMapBinding<int> m_PetMap
UI binding that maps pet list indices to JSON items (BindPet callback). -
private TypeHandle __TypeHandle
Internal container of ComponentLookup/BufferLookup handles used to obtain ECS lookups for Burst jobs and other operations. Assigned in OnCreateForCompiler. -
private const string kHouseholdIcon = "Media/Game/Icons/Household.svg"
Icon path constants used when writing JSON for UI items. -
private const string kResidenceIcon = "Media/Glyphs/Residence.svg"
Icon path for normal residence. -
private const string kHomelessShelterIcon = "Media/Glyphs/HomelessShelter.svg"
Icon path for homeless shelter residence. -
private const string kPetIcon = "Media/Game/Icons/Pet.svg"
Icon path for pet entries. -
private const string kItemType = "Game.UI.InGame.HouseholdSidebarSection+HouseholdSidebarItem"
Type name used when writing per-item JSON.
Properties
-
protected override string group => "HouseholdSidebarSection"
UI binding group name used for RawMapBinding registration and identification. -
protected override bool displayForDestroyedObjects => true
This section should be displayed even if target object is destroyed. -
protected override bool displayForUnderConstruction => true
This section should be displayed even for under-construction buildings. -
private Entity residenceEntity { get; set; }
Resolved residence entity for the current selection. Set in OnProcess from m_ResidenceResult. -
private HouseholdResult household { get; set; }
Primary HouseholdResult for the current context (set in OnProcess from m_HouseholdResult). -
private HouseholdSidebarVariant variant { get; set; }
Indicates which variant the sidebar is showing (citizen, household or building). Derived from m_Results[2]. -
private bool residenceIsHomelessShelter { get; set; }
True when the residence is a homeless shelter (affects tooltip and icon).
Constructors
public HouseholdSidebarSection()
Default constructor. Real initialization of ECS lookups and Native containers occurs in the overridden OnCreate method. Constructor is marked with [Preserve] attribute in the class.
Methods
-
protected override void OnCreate()
Allocates NativeLists / NativeArrays (Persistent allocator) used by the section. Registers RawMapBinding bindings (household, resident, pet) linking to BindHousehold/BindResident/BindPet. Calls base.OnCreate() first. Must be paired with OnDestroy to dispose Native containers. -
private void BindHousehold(IJsonWriter writer, int index)
Binding callback to write a household item to JSON. Retrieves the household entity from m_HouseholdsResult and writes icon, entity id, name, and member count using WriteItem helper. -
private void BindResident(IJsonWriter writer, int index)
Binding callback to write a resident item to JSON. Retrieves resident entity from m_ResidentsResult and calls WriteItem (no custom icon). -
private void BindPet(IJsonWriter writer, int index)
Binding callback to write a pet item to JSON. Uses pet icon and calls WriteItem. -
protected override void OnDestroy()
Disposes all NativeLists and NativeArrays allocated in OnCreate. Calls base.OnDestroy(). Important to prevent memory leaks. -
protected override void Reset()
Clears the NativeList contents, resets result arrays to defaults and flags (residenceIsHomelessShelter). This is called when the section needs to be reset between updates. -
protected override void OnUpdate()
Primary update path: - Schedules and completes CheckVisibilityJob (Burst) to determine visibility and basic counts based on the currently selected entity and its prefab.
- If visible, schedules and completes CollectDataJob to gather household/resident/pet data into the NativeLists/NativeArrays.
- Sorts the lists and updates the RawMapBindings (m_HouseholdMap / m_ResidentMap / m_PetMap) indexes so UI can request items.
This method obtains ComponentLookup/BufferLookup instances from __TypeHandle via InternalCompilerInterface.GetComponentLookup/GetBufferLookup.
protected override void OnProcess()
Converts job outputs into runtime properties used for JSON output:- Reads m_ResidenceResult / m_HouseholdResult / m_Results to set residenceEntity, household and variant.
- Validates that residenceEntity exists in EntityManager.
-
Detects homeless shelter (park/abandoned + renter buffer) and adds tooltip tag TooltipTags.HomelessShelter, marking residenceIsHomelessShelter true.
-
public override void OnWriteProperties(IJsonWriter writer)
Serializes section-level properties and counts used by the UI: -
Writes "variant" (variant.ToString()), "residence" (via WriteItem), "household" (primary household), numeric counts for households/residents/pets.
-
private void WriteItem(IJsonWriter writer, Entity entity, string iconPath, int memberCount = 0)
Helper to write a single Game.UI.InGame.HouseholdSidebarSection+HouseholdSidebarItem type into JSON: -
Writes entity id, name (bound via m_NameSystem), family name (if household), icon path, selected flag (compares to selectedEntity), and optional member count (or null when zero).
-
private void __AssignQueries(ref SystemState state)
Compiler helper (used by OnCreateForCompiler). The method in this class is a stub which creates/ disposes an EntityQueryBuilder. Kept for ECS query assignment consistency. -
protected override void OnCreateForCompiler()
Called in compiler-generated flow to assign queries and component lookup handles: calls __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef).
Nested types (short overview):
-
CheckVisibilityJob (Burst compiled IJob)
Fast, read-only job that checks whether the selected entity/prefab should show the household sidebar. It inspects components (Citizen, HouseholdPet, Building, Renter buffers, BuildingPropertyData, Abandoned, Park) and produces m_Results (visible, residentCount, variant). Uses CitizenUtils.IsCorpsePickedByHearse to exclude dead citizens. -
CollectDataJob (Burst compiled IJob)
Read-only job that collects detailed household data when the section is visible. Produces: - m_ResidenceResult (Entity)
- m_HouseholdResult (primary HouseholdResult)
- m_HouseholdsResult (NativeList of HouseholdResult)
- m_ResidentsResult (NativeList of ResidentResult)
-
m_PetsResult (NativeList of Entity) It computes per-household aggregates (members, average age, average happiness, education, wealth). Uses EconomyUtils.GetHouseholdTotalWealth, buffers for HouseholdCitizen, HouseholdAnimal and Resources, and excludes corpse residents.
-
HouseholdResult (struct)
Result record for a household: Entity, m_Members, m_Age, m_Education, m_Happiness, m_Wealth. Implements IComparable and IEquatable used for sorting and uniqueness. -
ResidentResult (struct)
Per-resident record: Entity, m_Age (CitizenAge), m_Education, m_Happiness. Implements IComparable and IEquatable for sorting. -
HouseholdComparer and ResidentComparer (classes)
Comparer helpers to sort HouseholdResult and ResidentResult by different criteria (members/age/education/happiness/wealth for households; age/education/happiness for residents) with deterministic tie-breaking.
Notes and important implementation details: - Native containers are allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy. Forgetting disposal would leak memory. - Jobs are scheduled and immediately completed (Schedule(..., base.Dependency).Complete()) because the section needs results synchronously for UI update. - The code relies heavily on ECS ComponentLookup/BufferLookup passed into the Burst jobs. __TypeHandle holds read-only handles and is assigned in OnCreateForCompiler. - Variant mapping: m_Results[2] -> HouseholdSidebarVariant (0 = Citizen, 1 = Household, 2 = Building). - WriteItem uses m_NameSystem to bind names and family names into JSON; familyName is null for non-household entities.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// HouseholdSidebarSection.OnCreate allocates native containers and registers bindings:
// m_ResidentsResult, m_PetsResult, m_HouseholdsResult, m_Results, m_ResidenceResult, m_HouseholdResult
// and registers RawMapBindings for householdMap, residentMap and petMap.
// If extending this class, call base.OnCreate() before using those lists.
}
If you plan to extend or patch this class in a mod: - Ensure you don't inadvertently break the lifecycle of native containers (OnCreate/OnDestroy/Reset). - When reading or writing ECS components used by the Burst jobs, add or update __TypeHandle and __AssignHandles accordingly so lookups are available in jobs. - To change UI serialization shape, modify OnWriteProperties and WriteItem while keeping the same item "type" string if the client-side UI expects it.