Game.NetObjectInitializeSystem
Assembly: Assembly-CSharp
Namespace: Game.Objects
Type: class
Base: GameSystemBase
Summary:
NetObjectInitializeSystem is an ECS system that initializes NetObject components when those entities are marked as Updated. It schedules a Burst-compiled IJobChunk (InitializeJob) that iterates matching ArchetypeChunks and updates NetObject flags (such as IsClear, TrackPassThrough and Backward) based on prefab data, nearby lanes, node parents and owner subnets. The system uses ComponentTypeHandle, ComponentLookup and BufferLookup to read prefab and geometry data and to determine lane directions and track passthrough behavior. This system is intended to run only for entities that have been flagged as Updated and that contain a NetObject component.
Fields
-
private Unity.Entities.EntityQuery m_UpdateQuery
Used to match entities to process. Created in OnCreate to require entities with ComponentType.ReadOnly() and ComponentType.ReadOnly (). The system requires this query for update scheduling. -
private TypeHandle __TypeHandle
Container struct used to cache ComponentTypeHandle/ComponentLookup/BufferLookup instances required by InitializeJob. Populated in __AssignHandles (called during OnCreateForCompiler) so that OnUpdate can acquire the runtime handles via InternalCompilerInterface.
Properties
- This system exposes no public properties.
Constructors
public NetObjectInitializeSystem()
Default constructor. The system is preserved and instantiated by the game's ECS bootstrap. No custom arguments.
Methods
-
protected override void OnCreate()
Creates the m_UpdateQuery: a query that requires Updated and NetObject. Calls RequireForUpdate(m_UpdateQuery) so the system only runs when matching entities exist. -
protected override void OnUpdate()
Builds and schedules the Burst IJobChunk (InitializeJob) using JobChunkExtensions.ScheduleParallel. Populates the InitializeJob fields by retrieving component type handles and lookups from the cached __TypeHandle via InternalCompilerInterface. The job runs in parallel over m_UpdateQuery and updates the NetObject components accordingly. The scheduled JobHandle is stored back to base.Dependency. -
protected override void OnCreateForCompiler()
Invoked for compiler-time setup. Calls __AssignQueries and __TypeHandle.__AssignHandles to prepare handles and queries used by the system. -
private void __AssignQueries(ref SystemState state)
(Internal helper) Currently creates an EntityQueryBuilder(Allocator.Temp).Dispose(); in decompiled code — intended to assign or validate queries during compile-time setup. -
private struct TypeHandle.__AssignHandles(ref SystemState state)
Populates all required ComponentTypeHandle, ComponentLookup and BufferLookup fields used by InitializeJob. Most handles are read-only except the NetObject component type handle which is writable. -
Nested job:
private struct InitializeJob : IJobChunk
(Burst compiled)
The job contains numerous fields: - ReadOnly ComponentTypeHandle: Attached, Owner, Secondary, Transform, Temp, PrefabRef
- Read/Write ComponentTypeHandle: NetObject
- ReadOnly ComponentLookup/BufferLookup: Node, TrackLane, CarLane, Curve, Owner, EdgeGeometry, NodeGeometry, PrefabRef, NetObjectData, TrackLaneData, LaneDirectionData, BufferLookup
, BufferLookup
Main job methods:
- public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates entities in the chunk and:
- Clears/sets basic NetObject flags (clears TrackPassThrough and Backward; sets IsClear).
- If the prefab has NetObjectData and the entity has an Attached parent that is a Node, calls CheckNodeParent to check sub-lanes under that node and adjust TrackPassThrough/IsClear based on track types and net object track passthrough mask.
- If no Secondary component is present but the prefab has LaneDirectionData, attempts to determine owner and calls CheckOwnerLanes to compute nearest car lane tangent and possibly set Backward based on lane orientation relative to the object's transform.
- Writes modified NetObject back to the chunk's NetObject array.
-
private void CheckNodeParent(ref NetObject netObject, NetObjectData netObjectData, Entity parent)
Looks up SubLane buffer on the parent entity. For each SubLane that is a TrackLane, it reads the SubLane's PrefabRef and uses TrackLaneData to compare allowed track types against netObjectData.m_TrackPassThrough:- If none of the track types match the net object's track passthrough mask, clears IsClear and TrackPassThrough.
- If matching, clears IsClear and sets TrackPassThrough.
-
private void CheckOwnerLanes(ref NetObject netObject, Transform transform, Entity owner)
Attempts to find the nearest car lane curve (among subnets of the owner) and compute the tangent direction at the closest point to the object's position. If the computed tangent's forward direction has negative dot product with the object's forward direction (math.forward(transform.m_Rotation)), sets the NetObjectFlags.Backward flag. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Explicit interface implementation that forwards to the above Execute method.
Remarks about behavior: the job uses MathUtils.DistanceSquared and MathUtils.Bounds helpers to find nearest geometry, iterates SubNets/SubLanes buffers, and filters lanes by PathMethod.Road and whether the lane entity has CarLane component. All lookups are read-only except for the NetObject component which is written back.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// This system creates a query that runs when entities with Updated + NetObject exist:
m_UpdateQuery = GetEntityQuery(ComponentType.ReadOnly<Updated>(), ComponentType.ReadOnly<NetObject>());
RequireForUpdate(m_UpdateQuery);
}
Notes for modders: - If you need to change how NetObject flags are initialized, consider creating a derivative system that runs after NetObjectInitializeSystem or patching the InitializeJob behavior via Harmony or a replacement system. Be careful with Burst and IJobChunk interactions — modifications need to be compatible with Unity's ECS job scheduling. - The system depends on several prefab data components (NetObjectData, TrackLaneData, LaneDirectionData) and geometry lookups; ensure those components/buffers exist and are populated for the relevant prefab types when adding custom prefabs or lanes.