Game.NetUpkeepSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
NetUpkeepSystem is a simulation system responsible for computing upkeep costs for "net" entities (roads/rails/edges) on specific update frames. It creates an EntityQuery that selects composition components which are alive and not owned/temporary/deleted, filters them by the current UpdateFrame slice, collects their archetype chunks, and schedules a Burst-compiled IJob (NetUpkeepJob) to iterate those chunks. The job reads Composition and Curve components and looks up PlaceableNetComposition prefab data to compute upkeep via NetUtils.GetUpkeepCost. The system throttles update frequency using kUpdatesPerDay and GetUpdateInterval to spread work across frames.
Fields
-
public static readonly int kUpdatesPerDay
Holds the number of update slices per in-game day (value: 32). Used to compute which subset of net compositions are processed each simulation frame. -
private CitySystem m_CitySystem
Reference to the CitySystem retrieved from the world in OnCreate. Likely used to access city-level data if needed by future logic (not used directly in this file beyond initialization). -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem retrieved from the world in OnCreate. Used in OnUpdate to compute the current UpdateFrame from the simulation frame index. -
private EntityQuery m_UpkeepQuery
EntityQuery configured to select entities that have a Composition and UpdateFrame component and exclude Owner, Deleted, Destroyed, Native, and Temp. This query is filtered by a shared UpdateFrame component each OnUpdate. -
private TypeHandle __TypeHandle
Compiler-generated structure holding ComponentTypeHandle and ComponentLookup handles for Composition, Curve, and PlaceableNetComposition. Populated in OnCreateForCompiler / __AssignHandles. -
private struct NetUpkeepJob
(nested)
Burst-compiled job that iterates a NativeListand computes upkeep costs. (See methods section for Execute details.) -
private struct TypeHandle
(nested)
Holds read-only ComponentTypeHandle/ComponentLookup fields and provides __AssignHandles to obtain handles from a SystemState.
Properties
- None (no public properties are declared on this system).
Constructors
public NetUpkeepSystem()
Default constructor. At runtime the system is created by the ECS world; the constructor itself is empty but the system is initialized in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval as an integer. Implementation returns: 262144 / (kUpdatesPerDay * 16) This value is used by the engine to schedule how often the system runs relative to the overall simulation schedule. -
protected override void OnCreate()
Initializes system state: - Calls base.OnCreate().
- Retrieves/creates CitySystem and SimulationSystem instances from the world.
- Constructs m_UpkeepQuery to select entities with Composition and UpdateFrame and exclude Owner, Deleted, Destroyed, Native, Temp.
- Calls RequireForUpdate(m_UpkeepQuery) so the system only updates when the query has matching entities.
-
Asserts that kUpdatesPerDay == 32.
-
protected override void OnUpdate()
Main runtime logic executed when the system updates: - Builds an UpdateFrame shared component using SimulationUtils.GetUpdateFrame(m_SimulationSystem.frameIndex, kUpdatesPerDay, 16) to compute the slice index for this frame.
- Resets and sets the shared component filter on m_UpkeepQuery.
- Converts the query to a NativeList
asynchronously (ToArchetypeChunkListAsync), receiving an out JobHandle. - Constructs a NetUpkeepJob with:
- ComponentTypeHandle
(read-only) - ComponentTypeHandle
(read-only) - ComponentLookup
(read-only) - the chunks list
- ComponentTypeHandle
- Schedules the job with IJobExtensions.Schedule, combining dependencies with the query-chunks creation JobHandle, and attaches it to base.Dependency.
-
Schedules disposal of the chunks list dependent on the scheduled job.
-
private void __AssignQueries(ref SystemState state)
Small compiler helper invoked by OnCreateForCompiler. Here it simply constructs and disposes a new EntityQueryBuilder (no persistent queries created here — the runtime query is created in OnCreate). -
protected override void OnCreateForCompiler()
Compiler helper that calls __AssignQueries and __TypeHandle.__AssignHandles(ref state) to populate required handles for the generated code path. -
NetUpkeepJob.Execute() (method of nested struct)
Burst-compiled job entry point. For each archetype chunk in m_Chunks: - Obtains NativeArray
and NativeArray from the chunk via the provided ComponentTypeHandles. -
Iterates each element in the chunk:
- For the composition, checks if m_PlaceableNetCompositionData.HasComponent(composition.m_Edge).
- If present, gets the PlaceableNetComposition for the edge and calls NetUtils.GetUpkeepCost(curve, placeableNetData).
- Accumulates the returned upkeep into a local int variable
num
. Note: the job accumulates upkeep into a local variable but does not write the result back to any component or shared storage in this code snippet. It may be intended for side effects or this snippet may be trimmed.
-
TypeHandle.__AssignHandles(ref SystemState state) (method of nested struct)
Assigns the ComponentTypeHandle, ComponentTypeHandle , and ComponentLookup from the provided SystemState with read-only access. This is used to prepare handles used by the job and OnUpdate.
Usage Example
[Preserve]
protected override void OnUpdate()
{
// Compute which UpdateFrame slice this simulation frame should process.
UpdateFrame sharedComponentFilter = new UpdateFrame {
m_Index = SimulationUtils.GetUpdateFrame(m_SimulationSystem.frameIndex, NetUpkeepSystem.kUpdatesPerDay, 16)
};
// Apply the shared component filter, gather chunks, and schedule the Burst job
m_UpkeepQuery.ResetFilter();
m_UpkeepQuery.SetSharedComponentFilter(sharedComponentFilter);
JobHandle chunksHandle;
var chunks = m_UpkeepQuery.ToArchetypeChunkListAsync(Allocator.TempJob, out chunksHandle);
var job = new NetUpkeepJob {
m_CompositionType = /* component handle from TypeHandle */,
m_CurveType = /* component handle from TypeHandle */,
m_PlaceableNetCompositionData = /* component lookup from TypeHandle */,
m_Chunks = chunks
};
base.Dependency = IJobExtensions.Schedule(job, JobHandle.CombineDependencies(base.Dependency, chunksHandle));
chunks.Dispose(base.Dependency);
}
Notes and Implementation Details:
- The job is annotated with [BurstCompile] for performance.
- Component handles are obtained via InternalCompilerInterface.GetComponentTypeHandle / GetComponentLookup in the generated OnUpdate code path; in user code these are typically created via state.GetComponentTypeHandle