Game.Prefabs.NetInitializeSystem
Assembly: Assembly-CSharp (game)
Namespace: Game.Prefabs
Type: class NetInitializeSystem
Base: GameSystemBase
Summary:
NetInitializeSystem is an ECS system used during prefab initialization for all "net" prefabs (roads, tracks, waterways, powerlines, pipelines, fences, pathways, taxiways, bridges, etc.). It reads authoring/prefab data and populates runtime ECS components and buffers with default values, derived flags, costs, lanes, pieces, sub‑objects and connectivity information. It also collects pathfinding heuristic defaults and fixes placeholder buffers for spawnable lanes. The system runs when prefab entities are created and is essential for converting Unity prefab configuration into the game's runtime net representation.
Fields
-
private PrefabSystem m_PrefabSystem
This holds a reference to the PrefabSystem (helper for mapping Unity prefabs to Entities). Used to look up prefab data/entities while initializing net components. -
private EntityQuery m_PrefabQuery
EntityQuery used to find newly created prefabs that require net initialization (Created + PrefabData, and any of several net-related component types). Required for the system update. -
private EntityQuery m_LaneQuery
EntityQuery for all NetLaneData entities (excluding Deleted). Used when scheduling jobs that read lane data (pathfind heuristic collection). -
private EntityQuery m_PlaceholderQuery
EntityQuery for placeholder objects used by spawnable lanes (NetLaneData + PlaceholderObjectElement, excluding Deleted). Used to run FixPlaceholdersJob to remove deleted objects from placeholder buffers. -
private NativeValue<PathfindHeuristicData> m_PathfindHeuristicData
Persistent NativeValue that stores collected pathfind heuristic defaults (car/track/pedestrian/flying/taxi/offroad) built by CollectPathfindDataJob. Allocated on OnCreate and disposed in OnDestroy. -
private JobHandle m_PathfindHeuristicDeps
JobHandle used to track job dependencies for pathfind heuristic collection. Completed before returning heuristic data in GetHeuristicData(). -
private Layer m_InGameLayersOnce
Accumulated layers required by in-game (non-editor) prefabs seen once; used by CanReplace() to decide whether a net prefab can replace another in-game. -
private Layer m_InGameLayersTwice
Accumulated layers required by in-game prefabs seen multiple times; used with m_InGameLayersOnce to detect conflicts for replacement rules. -
private TypeHandle __TypeHandle
Internal generated struct that caches ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup entries used throughout OnUpdate to avoid repeated handle creation. Assigned in OnCreateForCompiler / __AssignHandles. -
private struct TypeHandle
(nested)
Generated container of many ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup fields used by job scheduling and OnUpdate. See code for the full list (handles for NetData, NetPieceData, NetGeometryData, PlaceableNetData, lane data types, many buffer types, and read-only lookups). -
private struct FixPlaceholdersJob
(nested)
IJobChunk that removes entries from PlaceholderObjectElement buffers that reference deleted entities. Scheduled when spawnable placeholders exist. -
private struct InitializeNetDefaultsJob
(nested)
IJobParallelFor that performs most of the heavy lifting: iterates prefab archetype chunks and initializes NetGeometryData, NetData, PlaceableNetData, RoadData, TrackData, WaterwayData, TaxiwayData, and many buffers (NetSectionPiece, NetPieceLane, NetPieceObject, SubObject, SubMesh, DefaultNetLane, etc.). It computes composition-derived defaults, adds costs for placeable objects, updates flag masks and geometry defaults, and fills buffers based on prefab components. -
private struct CollectPathfindDataJob
(nested)
IJobChunk that reads NetLaneData and connection-lane distinctions to compute minimal pathfind costs from referenced pathfind prefabs (PathfindCarData, PathfindTrackData, PathfindPedestrianData, PathfindTransportData, PathfindConnectionData) and stores results into m_PathfindHeuristicData.
Properties
- (none — this system exposes behavior via methods; internal state is stored in fields)
Constructors
public NetInitializeSystem()
Parameterless constructor marked with [Preserve]. Typical generated constructor for managed systems — no custom initialization logic beyond what OnCreate performs.
Methods
-
protected override void OnCreate()
Initializes the system: gets/creates the PrefabSystem, constructs EntityQueries (m_PrefabQuery, m_LaneQuery, m_PlaceholderQuery), calls RequireForUpdate(m_PrefabQuery) to ensure the system runs only when there are relevant prefabs, and allocates m_PathfindHeuristicData (Allocator.Persistent). Also prepares internal type handles indirectly. -
protected override void OnDestroy()
Disposes persistent native m_PathfindHeuristicData and performs base cleanup. -
public PathfindHeuristicData GetHeuristicData()
Completes m_PathfindHeuristicDeps ( waits for scheduled pathfind jobs ) and returns the computed PathfindHeuristicData value. Use this to read the aggregated minimal costs computed from lane pathfind prefabs. -
public bool CanReplace(NetData netData, bool inGame)
Utility used to decide whether a net prefab can replace another in an in-game context. If not inGame returns true. If inGame, checks whether required layers conflict with accumulated m_InGameLayersOnce/m_InGameLayersTwice — returns true if replacement is allowed. -
private void AddSections(PrefabBase prefab, NetSectionInfo[] source, DynamicBuffer<NetGeometrySection> target, NetSectionFlags flags)
Helper that converts NetSectionInfo array (authoring data) into NetGeometrySection entries in the buffer. Handles median/left/right classification, hidden layer flags, inversion/flip halves, half-length, and logs errors for invalid requirement flags. -
protected override void OnUpdate()
Main update: builds an array of archetype chunks from m_PrefabQuery, prepares many ComponentTypeHandle/BufferTypeHandle/Lookup handles (using internal __TypeHandle), completes dependencies, and then iterates chunks to perform several operations: - For geometry-prefabs: setup NetGeometryData defaults, add sections (AddSections), populate edge/node states and node flags.
- For placeable nets: set PlaceableNetData defaults (snap, elevation, XP reward, upgrade flags).
- For pieces/prefabs: create NetPieceData, NetPieceLane, NetPieceArea, NetPieceObject buffers, set flags derived from component types (tiling, divider, move-vertices, etc.).
- Fill SubObject buffers from NetSubObjects components, set SubObject flags and occasionally modify NetGeometryData (e.g., MiddlePillars).
- Setup AuxiliaryNets buffer and DefaultNetLane / placeholder handling for spawnable lanes.
- Configure lane types (car, track, utility, parking, pedestrian, secondary) including widths, flags, special lane data (CarLaneData, TrackLaneData, UtilityLaneData, ParkingLaneData), compute parking slot intervals and widths, secondary-lane cross mappings and flags, auxiliary lanes, submesh data/LOD calculations for lane geometry.
- Set NetData/NetGeometryData modifications for specific high-level net types: roads (RoadPrefab), tracks (TrackPrefab), waterways, pathways, taxiways, powerlines, pipelines, fences, bridges and more — applying flags such as FlattenTerrain, BlockZone, SmoothElevation, StrictNodes, ElevatedIsRaised etc., and computing speed limits, node priorities, edge length ranges, intersection/marge layers, and placement flags.
- When spawnable placeholders are present, schedule FixPlaceholdersJob to remove placeholders that reference deleted entities.
-
Schedules InitializeNetDefaultsJob (IJobParallelFor) with the chunk array and schedules CollectPathfindDataJob (IJobChunk) on m_LaneQuery; combines dependencies and stores them in base.Dependency.
-
private void __AssignQueries(ref SystemState state)
Generated helper used by OnCreateForCompiler; minimal in this class. Present for compiler/IL2CPP correctness. -
protected override void OnCreateForCompiler()
Compiler/IL2CPP helper invoked during codegen; assigns handles via __TypeHandle.__AssignHandles and calls __AssignQueries. (Generated/boilerplate for Burst/Entities integration.) -
private void __AssignHandles(ref SystemState state)
(on TypeHandle)
Generated method that caches all ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup fields in the TypeHandle from the passed SystemState. Called during system initialization. -
Nested job methods:
- FixPlaceholdersJob.Execute(...) — removes placeholder entries that reference deleted entities.
- InitializeNetDefaultsJob.Execute(int index) — performs the per-chunk initialization described above (large method). Also contains helper submethods:
- UpdateFlagMasks(ref NetData, DynamicBuffer
/Entity) — recursively update general/side flag masks from composition requirements in sections/subsections/pieces. - AddObjectCosts(ref PlaceableNetComposition, NativeList
) — sums construction costs for placeable objects referenced by a composition, accounting for spacing.
- UpdateFlagMasks(ref NetData, DynamicBuffer
- CollectPathfindDataJob.Execute(...) — aggregates minimal pathfind costs from pathfind prefabs referenced by lanes/connection lanes.
Usage Example
// Example: read pathfinding heuristic data after prefab initialization
var world = World.DefaultGameObjectInjectionWorld;
var netInitSystem = world.GetOrCreateSystemManaged<Game.Prefabs.NetInitializeSystem>();
// Ensure any pending pathfind jobs are finished and get the heuristic data:
PathfindHeuristicData heur = netInitSystem.GetHeuristicData();
// Example: check if a NetData prefab can replace an in-game net
bool canReplace = netInitSystem.CanReplace(someNetData, inGame: true);
Notes / Tips for modders: - This system runs as part of prefab creation; if you add new Net-related prefab components, you may need to update the initialization logic or add corresponding component handlers so defaults and flags are set correctly. - Many values (speed limits, widths, LODs, flags) are derived from explicit prefab components (CarLane, TrackLane, Bridge, RoadPrefab, etc.). To influence runtime behavior of a new net prefab, author those components on the prefab. - The pathfinding heuristics are aggregated from referenced Pathfind* prefabs — adding new pathfind prefabs or changing costs affects the returned PathfindHeuristicData. - Be careful with the placeholder/spawnable lane system: SpawnableLane components add PlaceholderObjectElement entries; FixPlaceholdersJob will prune entries pointing to deleted objects. If you manipulate placeholders manually, ensure consistency of referenced entities.