Game.CargoTransportStationInitializeSystem
Assembly: Assembly-CSharp
Namespace: Game.Buildings
Type: class
Base: GameSystemBase
Summary:
CargoTransportStationInitializeSystem is an ECS system that initializes newly created cargo transport station entities. It queries for entities that have a CargoTransportStation tag, a Resources dynamic buffer, and a Created tag, and that do not have the Temp tag. For each matched entity it schedules a Burst-compiled IJobChunk (InitializeCargoTransportStationJob) that adds a PropertyRenter component to the entity (setting PropertyRenter.m_Property to the entity). The system uses a ModificationBarrier5 (an end-frame modification barrier) to obtain an EntityCommandBuffer.ParallelWriter so component additions are performed safely from the job. The system also uses a small TypeHandle helper struct to cache EntityTypeHandle and BufferTypeHandle
Fields
-
private EntityQuery m_Additions
This query selects entities which match: ReadOnly, ReadWrite , ReadOnly , and excludes Temp. The query is required for update via RequireForUpdate(m_Additions), so the system runs only when there are matching entities. -
private ModificationBarrier5 m_EndFrameBarrier
Holds a reference to the modification barrier system (an end-of-frame barrier) retrieved during OnCreate via World.GetOrCreateSystemManaged(). The barrier is used to create an EntityCommandBuffer (parallel writer) to record structural changes (adding PropertyRenter) from the job, and to register the job handle as a producer via AddJobHandleForProducer. -
private TypeHandle __TypeHandle
A small nested struct instance used to cache type handles (EntityTypeHandle and BufferTypeHandle). These handles are assigned during OnCreateForCompiler via __AssignHandles(ref base.CheckedStateRef) and then turned into job-ready handles with InternalCompilerInterface.Get... calls in OnUpdate.
Properties
- This system does not expose any public properties.
Constructors
public CargoTransportStationInitializeSystem()
Default parameterless constructor. Marked with [Preserve] attribute in the source (redundant here since constructor itself is empty), used by the engine/IL2CPP linking to avoid stripping.
Methods
protected override void OnCreate()
Called when the system is created. It:- Calls base.OnCreate().
- Retrieves the ModificationBarrier5 system from the world and stores it in m_EndFrameBarrier.
- Creates the EntityQuery m_Additions to select CargoTransportStation entities with a Resources buffer and Created tag and excluding Temp.
-
Calls RequireForUpdate(m_Additions) so the system only updates when the query has matching entities.
-
protected override void OnUpdate()
Prepares and schedules the InitializeCargoTransportStationJob: - Obtains EntityTypeHandle and BufferTypeHandle
suitable for the current job/Dependency using InternalCompilerInterface.Get... and the cached __TypeHandle fields. - Creates an EntityCommandBuffer.ParallelWriter from m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter().
- Schedules the job in parallel over m_Additions via JobChunkExtensions.ScheduleParallel and assigns the resulting dependency to base.Dependency.
-
Calls m_EndFrameBarrier.AddJobHandleForProducer(base.Dependency) to let the modification barrier know about the scheduled producer job so it can play back the recorded commands at the correct time.
-
protected override void OnCreateForCompiler()
Method used by the generated/compiled code path: - Calls base.OnCreateForCompiler().
- Calls __AssignQueries(ref base.CheckedStateRef) — this method is present but in this compiled form does not create queries (it constructs and disposes an EntityQueryBuilder(Allocator.Temp) placeholder).
-
Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to cache type handles.
-
private void __AssignQueries(ref SystemState state)
Compiler-generated helper. In this compiled form it creates and immediately disposes a temporary EntityQueryBuilder; no runtime query construction is required because queries are set up in OnCreate. -
private struct TypeHandle { ... public void __AssignHandles(ref SystemState state) }
Nested helper struct that caches: - EntityTypeHandle (__Unity_Entities_Entity_TypeHandle)
-
BufferTypeHandle
(__Game_Economy_Resources_RW_BufferTypeHandle)
The __AssignHandles method fills those handles from the SystemState. The OnUpdate method then converts these cached handles to job handles with InternalCompilerInterface.GetEntityTypeHandle / GetBufferTypeHandle. -
InitializeCargoTransportStationJob (private nested Burst-compiled struct implementing IJobChunk)
- Fields:
EntityTypeHandle m_EntityType
(ReadOnly)BufferTypeHandle<Resources> m_ResourcesType
(read/write handle; the job obtains the buffer accessor but does not modify buffers)EntityCommandBuffer.ParallelWriter m_CommandBuffer
(used to record AddComponent calls)
- Execute behavior:
- Iterates all entities in the chunk, obtains the Resources buffer accessor (unused except to ensure chunks match), and for each entity constructs a PropertyRenter component with m_Property set to the entity. It then records an AddComponent call on the parallel command buffer using the provided unfilteredChunkIndex.
- The job is marked [BurstCompile] in the source to enable Burst optimizations.
Notes: - The job uses the chunk's unfiltered index when writing into the parallel ECB. This is correct for parallel ECB writers and the chunk index is provided by the job scheduling API. - The Resources buffer is read via GetBufferAccessor but not otherwise used; its presence in the query ensures the system targets only stations that have that buffer.
Usage Example
// The system runs automatically in the World when there are entities that match the query.
// Example: create an entity that will be initialized by CargoTransportStationInitializeSystem.
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
// Create an archetype with the required components/tags
var archetype = em.CreateArchetype(
typeof(CargoTransportStation), // marker
typeof(Resources), // dynamic buffer
typeof(Created) // Created tag
// Note: do NOT add Temp tag, the query excludes entities with Temp
);
// Create an entity and add an initial Resources buffer element (if desired)
Entity station = em.CreateEntity(archetype);
var buffer = em.GetBuffer<Resources>(station);
buffer.Add(new Resources { /* ... initialize resource fields ... */ });
// After the next system update, CargoTransportStationInitializeSystem will schedule a job
// that will add the PropertyRenter component to `station`.
// The PropertyRenter will have m_Property == station.
If you want the system to run immediately in tests, ensure the world updates (or call World.Step/Update depending on your test harness) so OnUpdate executes and the modification barrier plays back the command buffer after the job completes.