Game.SubNetSystem
Assembly: Assembly-CSharp
Namespace: Game.Serialization
Type: class
Base: GameSystemBase
Summary:
SubNetSystem collects subnet entities (nodes or edges) and associates them with their owner entities by appending SubNet buffer elements to the owner's dynamic buffer. It uses a Burst-compiled IJobChunk (SubNetJob) to iterate matching chunks, read each entity's Owner component, and add a SubNet entry for that entity to the Owner's SubNet buffer. The system only runs when there are entities matching the query (Owner + Node/Edge) and is designed for efficient chunked processing in DOTS/Entities context. It also contains generated type handles and compiler-friendly setup methods used by the Unity ECS codegen/runtime.
Fields
-
private EntityQuery m_Query
Holds the query used to find entities that have an Owner component and are either Node or Edge. The system calls RequireForUpdate(m_Query) in OnCreate so the system only updates when matching entities exist. -
private TypeHandle __TypeHandle
A generated container struct that caches EntityTypeHandle, ComponentTypeHandle(read-only) and BufferLookup (read/write). It's assigned during OnCreateForCompiler via __TypeHandle.__AssignHandles(ref state) and used in OnUpdate to build the job's handles.
Properties
- This system exposes no public properties.
All interaction is performed via the EntityQuery and the scheduled job. The system uses internal Generated TypeHandle fields and SystemState handles to create job-safe handles each frame.
Constructors
public SubNetSystem()
Default parameterless constructor. Marked with [Preserve] on lifecycle methods to avoid stripping; the constructor itself is present but contains no custom initialization logic beyond base class construction.
Methods
-
protected override void OnCreate()
Initializes m_Query with an EntityQuery that requires Owner and selects entities that are Node OR Edge. Calls RequireForUpdate(m_Query) so the system will only run when matching entities exist. This is the place to customize the query if modding to include additional entity types. -
protected override void OnUpdate()
Creates and populates a SubNetJob instance with: - an EntityTypeHandle for reading Entity,
- a ComponentTypeHandle
(read-only), -
a BufferLookup
for writing/appending to owner buffers. The job is scheduled via JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency) and the resulting JobHandle is stored back into base.Dependency. -
protected override void OnCreateForCompiler()
Called to perform compiler/codegen-time initializations: calls __AssignQueries to ensure any queries are set up, and __TypeHandle.__AssignHandles(ref state) to initialize type/buffer handles. This method is part of the generated/compiled pattern used by Unity's Entities/DOTS codegen. -
private void __AssignQueries(ref SystemState state)
A generated helper that (in this implementation) creates an EntityQueryBuilder temporarily and disposes it. Present to satisfy codegen patterns; real query initialization happens in OnCreate. -
(Nested)
SubNetJob : IJobChunk
- Execute(...)
Burst-compiled job which: - obtains the Entity array for the chunk,
- obtains the Owner component array for the chunk (read-only),
- iterates elements and for each element attempts to get the owner's SubNet buffer via BufferLookup.TryGetBuffer(owner.m_Owner, out buffer),
- appends a new SubNet entry constructed from the entity to the owner's buffer.
This job writes to buffers (BufferLookup
) and reads Owner components; it is scheduled with the m_Query that filters Node/Edge entities with Owner.
Notes and considerations:
- The job is annotated with [BurstCompile] for performance.
- Owner is read-only; SubNet buffer is updated (RW) through BufferLookup.
- Uses InternalCompilerInterface.GetEntityTypeHandle/GetComponentTypeHandle/GetBufferLookup to materialize handles from __TypeHandle for the current SystemState.
- The system depends on SubNet being a valid buffer element type and that owners have a DynamicBuffer
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// This system configures a query that requires Owner and filters for Node or Edge.
m_Query = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[1] { ComponentType.ReadOnly<Owner>() },
Any = new ComponentType[2]
{
ComponentType.ReadOnly<Node>(),
ComponentType.ReadOnly<Edge>()
}
});
RequireForUpdate(m_Query);
}
[Preserve]
protected override void OnUpdate()
{
// Build and schedule the Burst-compiled SubNetJob that appends SubNet entries to owner buffers.
var job = new SubNetJob
{
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_OwnerType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Common_Owner_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_SubNets = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Net_SubNet_RW_BufferLookup, ref base.CheckedStateRef)
};
base.Dependency = JobChunkExtensions.Schedule(job, m_Query, base.Dependency);
}
Additional tips for modders:
- If owners may not have a SubNet buffer by default, ensure a system or archetype adds DynamicBuffer