Game.UI.InGame.SelectVehiclesSection
Assembly: Game
Namespace: Game.UI.InGame
Type: class
Base: InfoSectionBase
Summary:
SelectVehiclesSection is an in-game information UI section used by the transport line / vehicle selection UI. It queries available vehicle prefabs and depots, filters and assembles lists of compatible primary and secondary vehicle prefabs for a selected transport line, and exposes those lists and the currently selected primary/secondary vehicle as JSON properties for the UI. It uses ECS EntityQueries and Unity Jobs (including a chunk job) to perform performance-sensitive filtering and supports transport-specific constraints (transport type, size class, energy types, public transport purpose, cargo routes). It also binds a trigger ("selectVehicles") to allow the UI to update the selected vehicle model on the selected entity.
Fields
-
private PrefabUISystem m_PrefabUISystem
Provides UI helpers related to prefabs (e.g., requirements display). Assigned from the world in OnCreate and used when writing prefab requirements to the JSON output. -
private ImageSystem m_ImageSystem
Used to obtain thumbnails (or placeholder icons) for vehicle prefabs when writing UI JSON. -
private CityConfigurationSystem m_CityConfigurationSystem
Passed into the TransportVehicleSelectData PreUpdate call to take city config into account when building vehicle lists. -
private TransportVehicleSelectData m_TransportVehicleSelectData
Helper that contains the logic and data for selecting and listing vehicles. It is constructed in OnCreate and used with PreUpdate/PostUpdate API calls around job scheduling. -
private EntityQuery m_VehiclePrefabQuery
EntityQuery used to enumerate vehicle prefabs (created from TransportVehicleSelectData.GetEntityQueryDesc()). -
private EntityQuery m_DepotQuery
EntityQuery used to find transport depots in the city (TransportDepot components) — used by the DepotsJob to determine available energy types and whether any depots exist. -
private NativeArray<int> m_Results
A 2-length NativeArray used as an output from the DepotsJob. Index 0 is used as a boolean flag indicating presence of depots; index 1 is used as a bitmask of EnergyTypes available from depots. -
private TypeHandle __TypeHandle
Internal struct to cache Entity/Component/Buffer handles for performing IJobChunk scheduling safely. Populated in OnCreateForCompiler.
Properties
-
protected override string group => "SelectVehiclesSection"
Section group identifier used by the base InfoSectionBase for bindings and grouping. -
private Entity primaryVehicle { get; set; }
Current primary vehicle prefab entity chosen for the selected vehicle model (used for JSON output and UI state). -
private Entity secondaryVehicle { get; set; }
Current secondary vehicle prefab entity chosen for the selected vehicle model. -
private NativeList<Entity> primaryVehicles { get; set; }
List (NativeList, persistent) holding candidate primary vehicle prefabs produced by the VehiclesListJob. -
private NativeList<Entity> secondaryVehicles { get; set; }
List (NativeList, persistent) holding candidate secondary vehicle prefabs produced by the VehiclesListJob.
Constructors
public SelectVehiclesSection()
Default constructor. Initialization of systems and native containers happens in OnCreate.
Methods
-
protected override void Reset()
Clears the candidate lists and resets the m_Results array (depot flags / energy types). Called by the base class when resetting section state. -
[Preserve] protected override void OnCreate()
Initializes required systems (PrefabUISystem, ImageSystem, CityConfigurationSystem), constructs TransportVehicleSelectData, creates entity queries (depots and vehicle prefabs), allocates persistent NativeLists (primary/secondary) and m_Results (NativeArraylength 2), and adds a TriggerBinding "selectVehicles" to invoke SetVehicleModel from UI. This is the main setup method; it must be paired with proper disposal in OnDestroy. -
private void SetVehicleModel(Entity primary, Entity secondary)
Invoked by the UI binding ("selectVehicles"). Creates an EntityCommandBuffer from the end-frame barrier, sets the VehicleModel component on the selected entity to use the provided primary and secondary prefab entities, and requests an Info UI update. Note: uses m_EndFrameBarrier and base.EntityManager — ensure selectedEntity is valid. -
[Preserve] protected override void OnDestroy()
Disposes the persistent NativeLists and NativeArray (primaryVehicles, secondaryVehicles, m_Results) and calls base.OnDestroy. Important to avoid native memory leaks. -
private bool Visible()
Determines whether this section should be displayed for the current selected entity/prefab: returns true when the selected entity has a VehicleModel component and the selectedPrefab has TransportLineData. -
[Preserve] protected override void OnUpdate()
Main update flow: - Sets base.visible based on Visible().
- If visible, reads TransportLineData from selectedPrefab and schedules a DepotsJob (IJobChunk) to scan depot entities for compatible transport type and gather energy type bitmask and depot presence.
- Builds/updates TransportVehicleSelectData (PreUpdate) and schedules a VehiclesListJob (IJob) which calls TransportVehicleSelectData.ListVehicles to fill the primary/secondary NativeLists. Combines job dependencies and calls PostUpdate on m_TransportVehicleSelectData.
-
Completes jobs and sets base.visible to whether there is more than one candidate in primary/secondary lists (i.e., whether selection UI should be shown).
-
protected override void OnProcess()
Called when processing the section: reads the VehicleModel component from selectedEntity to set primaryVehicle and secondaryVehicle for JSON output; adds tooltip tags ("TransportLine", "CargoRoute") to the base tooltipTags set. -
public override void OnWriteProperties(IJsonWriter writer)
Writes JSON properties for the UI: - "primaryVehicle" — either null or an object written by WriteVehicle.
- "secondaryVehicle" — either null or object.
- "primaryVehicles" — array of candidate primary vehicle prefabs.
-
"secondaryVehicles" — for trains: array of candidate secondary vehicle prefabs; otherwise writes null. This method uses m_PrefabUISystem and m_ImageSystem to include requirements and thumbnails.
-
private void WriteVehicle(IJsonWriter writer, Entity entity)
Helper used by OnWriteProperties to write a vehicle prefab entry. Writes: - type name (GetType().FullName + "+VehiclePrefab")
- "entity" (entity id)
- "id" (prefab name via m_PrefabSystem.GetPrefabName)
- "locked" (whether the prefab has an enabled Locked component)
- "requirements" (prefab requirements via m_PrefabUISystem.BindPrefabRequirements)
-
"thumbnail" (image or placeholder via m_ImageSystem)
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler helper for query assignment (generated pattern). The implementation in this class constructs an EntityQueryBuilder (and disposes it) — needed for compiler-generated wiring. -
protected override void OnCreateForCompiler()
Compiler-time wiring: calls __AssignQueries and populates __TypeHandle.__AssignHandles with the CheckedStateRef so chunk/job handles are ready for use. -
Private nested job types:
DepotsJob : IJobChunk
A chunk job scanning depot entities to determine whether any depot of the required TransportType exists and to OR-accumulate energy types available. Writes results into the shared NativeArraym_Results. VehiclesListJob : IJob
Executes on a worker thread, calling TransportVehicleSelectData.ListVehicles with filters (resources, energy types, size class, purpose, transport type) to fill the primary/secondary NativeLists.TypeHandle
Internal helper holding cached EntityTypeHandle, BufferTypeHandle, and ComponentLookup . Has __AssignHandles(ref SystemState) to get handles from the system state.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// standard setup performed by SelectVehiclesSection:
m_PrefabUISystem = base.World.GetOrCreateSystemManaged<PrefabUISystem>();
m_ImageSystem = base.World.GetOrCreateSystemManaged<ImageSystem>();
m_CityConfigurationSystem = base.World.GetOrCreateSystemManaged<CityConfigurationSystem>();
m_TransportVehicleSelectData = new TransportVehicleSelectData(this);
m_DepotQuery = GetEntityQuery(
ComponentType.ReadOnly<Game.Buildings.TransportDepot>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>());
m_VehiclePrefabQuery = GetEntityQuery(TransportVehicleSelectData.GetEntityQueryDesc());
primaryVehicles = new NativeList<Entity>(20, Allocator.Persistent);
secondaryVehicles = new NativeList<Entity>(20, Allocator.Persistent);
m_Results = new NativeArray<int>(2, Allocator.Persistent);
// bind UI trigger so the JS/UI can call "selectVehicles" with (primary, secondary)
AddBinding(new TriggerBinding<Entity, Entity>(group, "selectVehicles", SetVehicleModel));
}
Notes and tips for modders:
- This class relies heavily on Unity.Entities ECS patterns and Jobs. Ensure you call base.OnCreate/OnDestroy when deriving or altering lifecycle behavior.
- Native containers allocated with Allocator.Persistent must be disposed in OnDestroy to avoid leaks (this class does so).
- The DepotsJob writes into a small NativeArray