Game.Serialization.ConnectedBuildingSystem
Assembly: Game
Namespace: Game.Serialization
Type: public class
Base: GameSystemBase
Summary:
ConnectedBuildingSystem is a Unity ECS GameSystem that collects building entities and registers them with the ConnectedBuilding buffer on their associated road edge entity. The system schedules a Burst-compiled IJobChunk (ConnectedBuildingJob) that iterates Building components; for each building that has a non-null m_RoadEdge Entity, it adds a ConnectedBuilding entry into the ConnectedBuilding dynamic buffer of that road edge. This system uses ECS type handles and buffer lookups obtained via an internal TypeHandle helper and is intended to run whenever Building components exist (it requires a query for Building).
Fields
-
private EntityQuery m_Query
Holds the EntityQuery used to find entities that have a Building component. This query is created in OnCreate and is required for updating the system. -
private TypeHandle __TypeHandle
A generated helper struct that stores EntityTypeHandle, ComponentTypeHandle(read-only), and BufferLookup (read-write) used by the job. __TypeHandle has an __AssignHandles method to populate these handles from the SystemState.
Properties
- (none)
This system does not expose public properties.
Constructors
public ConnectedBuildingSystem()
Default parameterless constructor. Marked with [Preserve] attribute on the type-level methods; nothing special occurs in the constructor itself — initialization happens in OnCreate/OnCreateForCompiler.
Methods
protected override void OnCreate()
Called when the system is created. It sets up m_Query to select entities that have a Building component:- m_Query = GetEntityQuery(ComponentType.ReadOnly
()) -
Calls RequireForUpdate(m_Query) so the system only updates when matching entities exist.
-
protected override void OnUpdate()
Creates and schedules the ConnectedBuildingJob: - Fills jobData fields using InternalCompilerInterface to get the EntityTypeHandle, ComponentTypeHandle
(read-only), and BufferLookup (read-write) from __TypeHandle and the system's CheckedStateRef. -
Schedules the job with JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency) and assigns the returned JobHandle to base.Dependency.
-
protected override void OnCreateForCompiler()
Called by generated/compiled code paths to prepare handles for the compiler/runtime: -
Calls __AssignQueries(ref base.CheckedStateRef) and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to set up query and type handles.
-
private void __AssignQueries(ref SystemState state)
A generated stub for query assignment. In this file it creates and immediately disposes an EntityQueryBuilder(Allocator.Temp) — present to satisfy codegen patterns. -
ConnectedBuildingJob (private struct) — Burst compiled, implements IJobChunk
- Fields:
[ReadOnly] public EntityTypeHandle m_EntityType
— used to get entity array for the chunk.[ReadOnly] public ComponentTypeHandle<Building> m_BuildingType
— read-only access to Building component array.public BufferLookup<ConnectedBuilding> m_ConnectedBuildings
— buffer lookup to access ConnectedBuilding buffers on road edge entities.
- Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void
- Iterates over the chunk's Building array and the corresponding Entities.
- For each Building, checks if building.m_RoadEdge != Entity.Null.
- If true, uses m_ConnectedBuildings[roadEdgeEntity].Add(new ConnectedBuilding(buildingEntity)) to append a ConnectedBuilding entry referencing the building into the road edge's buffer.
- Explicit IJobChunk.Execute forwards to the typed Execute method.
- The job is annotated with [BurstCompile] for performance.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Create a query that selects all entities with a Building component.
m_Query = GetEntityQuery(ComponentType.ReadOnly<Building>());
RequireForUpdate(m_Query);
}
[Preserve]
protected override void OnUpdate()
{
var jobData = new ConnectedBuildingJob
{
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_BuildingType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Buildings_Building_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_ConnectedBuildings = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Buildings_ConnectedBuilding_RW_BufferLookup, ref base.CheckedStateRef)
};
base.Dependency = JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency);
}
Notes: - This system relies on the existence of a ConnectedBuilding dynamic buffer type on the road edge entities. Ensure road edge entities have that buffer component added elsewhere. - The job uses read-only access to Building data and writes to buffers on other entities (road edges) via BufferLookup; race conditions are avoided because each connected building is added to the buffer corresponding to a specific road edge entity. - The job is Burst-compiled, so ensure types used (ConnectedBuilding, Building) are Burst-compatible (blittable) for performance.