Game.Citizens.StorageInitializeSystem
Assembly: Assembly-CSharp
Namespace: Game.Citizens
Type: class
Base: GameSystemBase
Summary:
StorageInitializeSystem is an ECS system that initializes newly created storage company entities. It schedules a Burst-compiled IJobChunk (InitializeStorageJob) to run in parallel over entities matching a "Created" storage-company query. For each entity the job initializes CompanyData: it seeds the company's Random using a value derived from the current simulation frame and the entity index, then picks a brand from the prefab's CompanyBrandElement buffer (if any) and writes the CompanyData component via a parallel command buffer scheduled through a ModificationBarrier5. The system is designed to run safely in a multithreaded job context and uses type handles and buffer lookups for efficient access to required components and buffers.
Fields
-
private EntityQuery m_CreatedStorageGroup
This query selects storage company entities that are marked Created and exclude Deleted and Temp. The system requires this query for update so the system only runs when new storage entities exist. -
private ModificationBarrier5 m_EndFrameBarrier
A barrier system (ModificationBarrier5) used to create an EntityCommandBuffer.ParallelWriter for applying component writes from the job and to register the job handle with the barrier (AddJobHandleForProducer). This ensures safe, ordered structural changes after the job completes. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current frame index (frameIndex). The job uses this value to derive a deterministic Random seed per entity based on simulation frame and entity index. -
private TypeHandle __TypeHandle
Holds the cached entity/component/buffer type handles used by the job: EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle , and BufferLookup . __TypeHandle.__AssignHandles is used to populate these handles from a SystemState in OnCreateForCompiler.
Properties
- This type defines no public properties.
Constructors
public StorageInitializeSystem()
Default constructor. The system uses OnCreate to perform runtime initialization (barrier/system lookups and entity queries).
Methods
protected override void OnCreate()
: System.Void
Sets up runtime dependencies:- Acquires/creates the ModificationBarrier5 and SimulationSystem from the World.
- Creates the m_CreatedStorageGroup EntityQuery for StorageCompany entities that are Created and not Deleted/Temp.
-
Calls RequireForUpdate(m_CreatedStorageGroup) so the system only runs when matching entities are present.
-
protected override void OnUpdate()
: System.Void
Constructs and schedules the InitializeStorageJob: - Fills job fields from __TypeHandle via InternalCompilerInterface.Get*Handle calls and reads the current simulation frame.
- Creates a parallel EntityCommandBuffer from m_EndFrameBarrier and schedules the job with JobChunkExtensions.ScheduleParallel.
-
Stores the returned JobHandle into base.Dependency and registers it with the barrier via m_EndFrameBarrier.AddJobHandleForProducer.
-
protected override void OnCreateForCompiler()
: System.Void
Called by generated/compiled code paths: assigns queries and calls __TypeHandle.__AssignHandles to initialize the type handles required by the job. -
private void __AssignQueries(ref SystemState state)
: System.Void
Compiler helper that ensures any query builders used by generated code are created/initialized. In this class it currently builds and disposes an empty EntityQueryBuilder (placeholder used by source generation).
Nested / Job-related methods (key behaviors):
InitializeStorageJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
: System.Void
Core job execution method:- Iterates entities in the chunk using the EntityTypeHandle and PrefabRef component array.
- For each entity:
- Reads the PrefabRef to obtain the prefab entity.
- If the chunk contains CompanyData (chunk.Has(ref m_CompanyType)):
- Constructs a CompanyData with a Random seeded as: new Random((uint)(1 + (int)(m_SimulationFrame ^ entity.Index))).
- Uses m_Brands buffer lookup on the prefab to read available CompanyBrandElement elements; if non-empty, selects one at random using the Random to set component.m_Brand.
- Writes the CompanyData back to the entity using the parallel command buffer: m_CommandBuffer.SetComponent(unfilteredChunkIndex, e, component).
-
This method is Burst-compiled and runs in parallel across chunks.
-
TypeHandle.__AssignHandles(ref SystemState state)
: System.Void
Populates the stored EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle , and BufferLookup from the provided SystemState. This is used by OnCreateForCompiler to ensure the job uses correct handles.
Usage Example
// The system itself wires these in OnCreate / OnUpdate; typical usage scenario:
// 1) Ensure a storage prefab has CompanyBrandElement buffer entries (brands).
// 2) Instantiate the prefab and add a Created tag to trigger initialization.
// 3) StorageInitializeSystem will run and set CompanyData (random seed + brand).
// Example: instantiate a storage prefab so the system will initialize it:
Entity storageInstance = EntityManager.Instantiate(storagePrefabEntity);
EntityManager.AddComponentData(storageInstance, new Created()); // mark as newly created
// The StorageInitializeSystem will see the Created tag, schedule the InitializeStorageJob,
// and when the job runs it will assign CompanyData (seed + chosen brand) on the storageInstance.
// No direct calls to the system are required; the system runs automatically when the query matches.
Notes and implementation details: - The initialization uses a deterministic seed derived from the simulation frame and the entity index to produce per-entity randomness that varies across frames in a predictable way. - The job relies on the prefab entity referenced by PrefabRef having a CompanyBrandElement buffer; if that buffer is empty no brand is assigned. - Writes are performed via an EntityCommandBuffer.ParallelWriter created by a ModificationBarrier5; the job registers its JobHandle with the barrier to ensure proper ordering of structural changes.