Game.LocalServicesSection
Assembly:
Game (inferred from file path)
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
LocalServicesSection is an in-game UI info section used to collect and present buildings that provide local services for the currently selected district/area. It queries buildings that belong to service districts, caches the building entities and their associated prefabs, and exposes that data to the UI by writing a JSON representation (name, service icon, entity id). The section uses Unity.Entities APIs (EntityQuery, NativeList, DynamicBuffer) and Colossal UI binding systems (ImageSystem and a name binding system available on the base class).
Fields
-
private ImageSystem m_ImageSystem
This holds a reference to the game's ImageSystem used to resolve group/service icons for prefab entities. It is populated in OnCreate via World.GetOrCreateSystemManaged(). -
private EntityQuery m_ServiceDistrictBuildingQuery
An EntityQuery that matches building entities with PrefabRef and ServiceDistrict components and excludes temporary/deleted entities. Used in OnProcess to find all candidate local service buildings. -
private NativeList<Entity> localServiceBuildings { get; set; }
A NativeList (Allocator.Persistent) used to store building entities that belong to the currently selected service district. Cleared in Reset() and disposed in OnDestroy(). This list is iterated in OnWriteProperties to output the UI data. -
private NativeList<Entity> prefabs { get; set; }
A NativeList (Allocator.Persistent) used to store prefab entity references corresponding to the entries in localServiceBuildings. Indexes parallel localServiceBuildings. Also cleared in Reset() and disposed in OnDestroy().
Properties
-
protected override string group => "LocalServicesSection"
Override of InfoSectionBase.group used to identify the group for this section in the UI / info system. -
public
(inherited) selectedEntity, m_NameSystem, visible, World, EntityManager, etc.
Note: Several members used by this class (for example selectedEntity and m_NameSystem) are declared in the base class InfoSectionBase — they are referenced here (selectedEntity when filtering ServiceDistrict buffers, and m_NameSystem for binding names in OnWriteProperties).
Constructors
public LocalServicesSection()
Default constructor. Marked with [Preserve] on lifecycle methods, but the ctor itself is parameterless and does not perform initialization — initialization happens in OnCreate.
Methods
-
protected override void Reset()
Clears the internal NativeList caches: localServiceBuildings.Clear() and prefabs.Clear(). Called by the base info section lifecycle when appropriate to reset state between frames/updates. -
[Preserve] protected override void OnCreate()
Initializes runtime data: - Calls base.OnCreate().
- Retrieves ImageSystem via base.World.GetOrCreateSystemManaged
() and stores it to m_ImageSystem. - Builds m_ServiceDistrictBuildingQuery to find Building + PrefabRef + ServiceDistrict and exclude Temp and Deleted.
-
Allocates localServiceBuildings and prefabs as NativeList
with Allocator.Persistent. This method sets up the persistent lists and the query needed for OnProcess. -
[Preserve] protected override void OnDestroy()
Disposes persistent NativeLists allocated in OnCreate. Calls base.OnDestroy() then localServiceBuildings.Dispose() and prefabs.Dispose(). Ensures no NativeContainer memory leaks. -
[Preserve] protected override void OnUpdate()
Updates visibility for the section: sets base.visible to true only when the selectedEntity has both District and Area components in the EntityManager. Keeps the section hidden when a district/area is not selected. -
protected override void OnProcess()
Per-frame processing to populate the cached lists: - Uses m_ServiceDistrictBuildingQuery.ToEntityArray(Allocator.TempJob) to get all candidate building entities.
- Uses m_ServiceDistrictBuildingQuery.ToComponentDataArray
(Allocator.TempJob) to get their PrefabRef data in parallel. - For each entity, reads the ServiceDistrict DynamicBuffer and inspects entries. If any ServiceDistrict buffer entry references selectedEntity, that building is added to localServiceBuildings and its prefab entity (PrefabRef.m_Prefab) is added to prefabs.
- Ensures both native arrays are disposed in a finally block. Notes:
- Uses temporary native arrays allocated with Allocator.TempJob — these must be disposed promptly (and are).
-
The two NativeLists (localServiceBuildings and prefabs) grow during processing; Reset() clears them between uses.
-
public override void OnWriteProperties(IJsonWriter writer)
Writes the section's data to a JSON writer for the UI: - Writes a property "localServiceBuildings" containing an array with one element per cached building.
- For each cached building, writes a typed object "selectedInfo.LocalServiceBuilding" and includes:
- "name": binds the building name with m_NameSystem.BindName(writer, localServiceBuildings[i])
- "serviceIcon": writes the icon id/string via m_ImageSystem.GetGroupIcon(prefabs[i])
- "entity": writes the raw Entity value (entity id)
- This is the output consumed by the in-game UI to show local service building entries for the selected district.
Implementation notes: - The order of entries matches the order they were discovered in the query and buffer scanning. - The UI relies on m_NameSystem and m_ImageSystem to provide localized names and icon identifiers.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// initialize systems and persistent lists — same as the mod's class does
m_ImageSystem = base.World.GetOrCreateSystemManaged<ImageSystem>();
m_ServiceDistrictBuildingQuery = GetEntityQuery(
ComponentType.ReadOnly<Building>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<ServiceDistrict>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>()
);
localServiceBuildings = new NativeList<Entity>(Allocator.Persistent);
prefabs = new NativeList<Entity>(Allocator.Persistent);
}
Additional notes for modders - Memory management: localServiceBuildings and prefabs are Allocator.Persistent and must be disposed (OnDestroy) to avoid leaks. Reset() just clears contents and does not free memory. - Threading: This class uses main-thread ECS APIs and temp job native arrays; do not hold references to TempJob arrays beyond the method scope. - Query correctness: m_ServiceDistrictBuildingQuery excludes Temp and Deleted entities so the UI won't show transient or removed buildings. - JSON format: OnWriteProperties produces a JSON array named "localServiceBuildings" with typed entries "selectedInfo.LocalServiceBuilding" containing fields "name", "serviceIcon", and "entity". Use these keys on the UI side to render the list.