Game.UI.InGame.VehicleCountSection
Assembly:
Assembly-CSharp (Game module) — (inferred; part of the game's codebase)
Namespace:
Game.UI.InGame
Type:
class VehicleCountSection
Base:
InfoSectionBase
Summary:
VehicleCountSection is a UI info section used by the in-game transport line inspector. It computes and exposes vehicle counts for a selected transport line: current vehicle count, active vehicles, minimum/maximum allowed vehicle counts based on policy sliders, and a "stable" route duration. The heavy computation is performed in a Burst-compiled IJob (CalculateVehicleCountJob) using component and buffer lookups; results are passed back to managed code via NativeArray/NativeReference. The section also binds a UI trigger ("setVehicleCount") to allow the user to set the desired vehicle count, which is converted to a policy adjustment and applied through the PoliciesUISystem.
Fields
-
private PoliciesUISystem m_PoliciesUISystem
Reference to the PoliciesUISystem used to apply policy adjustments (SetPolicy). Initialized in OnCreate via World.GetOrCreateSystemManaged(). -
private Entity m_VehicleCountPolicy
Entity that represents the vehicle count policy prefab (set in OnGameLoaded by reading UITransportConfigurationPrefab). Used when applying a vehicle-count adjustment. -
private EntityQuery m_ConfigQuery
Query used to find UITransportConfigurationData so the vehicle count policy prefab can be located on game load. -
private NativeArray<int> m_IntResults
A 4-element NativeArray (Allocator.Persistent) used to receive integer results from CalculateVehicleCountJob. Values correspond to: [0]=vehicleCount, [1]=activeVehicles, [2]=vehicleCountMin, [3]=vehicleCountMax. Disposed in OnDestroy. -
private NativeReference<float> m_DurationResult
A NativeReference(Allocator.Persistent) holding the computed stable route duration produced by the job. Disposed in OnDestroy. -
private TypeHandle __TypeHandle
Holds ComponentLookup/BufferLookup handles (TransportLineData, PolicySliderData, VehicleTiming, PathInformation, Route buffers, etc.). __TypeHandle.__AssignHandles(ref SystemState) initializes those lookups for safe job scheduling. -
private enum Result
(nested enum)
Enum describing result indices used by the job: VehicleCount, ActiveVehicles, VehicleCountMin, VehicleCountMax, Count. -
private struct CalculateVehicleCountJob
(nested Burst IJob)
Burst-compiled job performing the main calculations: reads transport line/policy/route data, computes stable duration, current vehicle count, active vehicles, min/max counts given policy slider range, and contains helper methods for conversions between vehicle count and policy adjustment. -
private struct TypeHandle
(nested struct)
Container for ComponentLookup and BufferLookup fields and an __AssignHandles method used to populate them from a SystemState.
Properties
-
protected override string group => "VehicleCountSection"
UI grouping name used for binding and identifying this InfoSection. -
private int vehicleCountMin { get; set; }
Minimum vehicle count calculated from policy slider minimum. -
private int vehicleCountMax { get; set; }
Maximum vehicle count calculated from policy slider maximum. -
private int vehicleCount { get; set; }
Calculated vehicle count (based on current vehicle interval and stable duration). -
private int activeVehicles { get; set; }
Number of active vehicles (i.e., RouteVehicle buffer length for the selected entity). -
private float stableDuration { get; set; }
Stable route duration computed by the job (sum of segment durations + stop durations where appropriate).
Constructors
public VehicleCountSection()
Default constructor. Most initialization occurs in OnCreate; native containers are allocated in OnCreate.
Methods
-
protected override void Reset()
Resets managed properties to defaults (zeros) — called to clear state. -
[Preserve] protected override void OnCreate()
Initializes the section: retrieves PoliciesUISystem; builds the config EntityQuery; registers the "setVehicleCount" TriggerBindingwhich calls OnSetVehicleCount; allocates m_IntResults and m_DurationResult (persistent native containers). -
[Preserve] protected override void OnDestroy()
Disposes the NativeArray and NativeReference (m_IntResults, m_DurationResult) and calls base.OnDestroy(). Important to avoid native memory leaks. -
protected override void OnGameLoaded(Context serializationContext)
When the game loads, if a UITransportConfigurationData exists, retrieves UITransportConfigurationPrefab and resolves the m_VehicleCountPolicy entity used for policy adjustments. -
private void OnSetVehicleCount(float newVehicleCount)
Handler invoked by the UI binding. Converts desired vehicle count into a policy adjustment (via CalculateVehicleCountJob.CalculateAdjustmentFromVehicleCount) and calls m_PoliciesUISystem.SetPolicy(selectedEntity, m_VehicleCountPolicy, active: true, adjustment). -
private bool Visible()
Determines whether this section should be visible: returns true when the selected entity has Route, TransportLine, RouteWaypoint and Policy components. -
[Preserve] protected override void OnUpdate()
If visible, schedules and immediately completes the CalculateVehicleCountJob. The job is constructed with component/buffer lookups provided by __TypeHandle and the base.CheckedStateRef (for safe access). Results are written into m_IntResults and m_DurationResult. -
protected override void OnProcess()
Reads results from native containers and assigns them to managed properties (vehicleCountMin/Max, vehicleCount, activeVehicles, stableDuration). Also adds tooltip tags "TransportLine" and "CargoRoute". -
public override void OnWriteProperties(IJsonWriter writer)
Serializes the managed properties (vehicleCountMin, vehicleCountMax, vehicleCount, activeVehicles) into JSON (used by UI/serialization). -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler helper: sets up any entity queries necessary for the compiled codepath (in this implementation it creates an empty builder). -
protected override void OnCreateForCompiler()
Compiler-time helper that calls __AssignQueries and __TypeHandle.__AssignHandles to ensure lookups are initialized for compiled contexts. -
Nested Job:
private struct CalculateVehicleCountJob : IJob
- Execute(): main logic that:
- Reads TransportLineData for the selected prefab.
- Reads route buffers (RouteVehicle, RouteWaypoint, RouteSegment, RouteModifier) for the selected entity and policy data.
- Applies route modifiers to default interval and computes stable duration via CalculateStableDuration.
- Writes vehicle count results into m_IntResults and duration into m_Duration.
- CalculateVehicleCountFromAdjustment(float policyAdjustment, float interval, float duration): builds a RouteModifier from policy slider data and modifier data and returns the vehicle count for the adjusted interval/duration.
- static CalculateAdjustmentFromVehicleCount(int vehicleCount, float originalInterval, float duration, DynamicBuffer
modifierDatas, PolicySliderData sliderData): reverse-calculates a policy slider adjustment needed to reach a target vehicleCount for a given duration. Used by OnSetVehicleCount to produce the policy adjustment float. - CalculateStableDuration(TransportLineData transportLineData): iterates waypoints and segments to compute total expected duration for a single vehicle lap, taking stops into account using VehicleTiming and segment PathInformation durations.
Notes on CalculateVehicleCountJob: - Uses BurstCompile and read-only ComponentLookup/BufferLookup fields for high performance. - The job uses RouteUtils.ApplyModifier and TransportLineSystem.CalculateVehicleCount/CalculateVehicleInterval utilities to convert between intervals and counts.
Usage Example
// Example: The class itself binds a UI trigger and applies policy adjustments.
// This is the same approach used in the source: when the UI calls "setVehicleCount",
// OnSetVehicleCount converts the desired vehicle count to a policy adjustment and applies it.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_PoliciesUISystem = base.World.GetOrCreateSystemManaged<PoliciesUISystem>();
AddBinding(new TriggerBinding<float>(group, "setVehicleCount", OnSetVehicleCount));
m_IntResults = new NativeArray<int>(4, Allocator.Persistent);
m_DurationResult = new NativeReference<float>(0f, Allocator.Persistent);
}
private void OnSetVehicleCount(float newVehicleCount)
{
// Read current policy slider and modifier data from the policy prefab entity,
// compute an adjustment that will produce the requested number of vehicles,
// then apply the policy to the selected entity.
DynamicBuffer<RouteModifierData> buffer = base.EntityManager.GetBuffer<RouteModifierData>(m_VehicleCountPolicy, isReadOnly: true);
PolicySliderData componentData = base.EntityManager.GetComponentData<PolicySliderData>(m_VehicleCountPolicy);
float adjustment = CalculateVehicleCountJob.CalculateAdjustmentFromVehicleCount(
(int)newVehicleCount,
base.EntityManager.GetComponentData<TransportLineData>(selectedPrefab).m_DefaultVehicleInterval,
stableDuration,
buffer,
componentData);
m_PoliciesUISystem.SetPolicy(selectedEntity, m_VehicleCountPolicy, active: true, adjustment);
}
Notes and Modding Tips: - The heavy computation is run in a Burst IJob; when modifying behavior, be careful to update both the job and its managed usage. - NativeArray and NativeReference are created with Allocator.Persistent; ensure you dispose of them in OnDestroy to avoid memory leaks. - The TypeHandle contains ComponentLookup/BufferLookup fields. When scheduling the job, use InternalCompilerInterface.GetComponentLookup / GetBufferLookup with the CheckedStateRef as shown in OnUpdate. - To change how vehicle counts are calculated, consider overriding or altering the logic inside CalculateVehicleCountJob (CalculateStableDuration, interval modifications, or the transport line utilities used). - The Show/Hide logic depends on the selected entity containing Route, TransportLine, RouteWaypoint and Policy components; ensure your entities include those components if you want this section visible.