Skip to content

Game.Buildings.InitializeSchoolSystem

Assembly: Assembly-CSharp (inferred)
Namespace: Game.Buildings

Type: class

Base: GameSystemBase

Summary: Initializes newly created school entities and updates student components accordingly. When schools are created (entities matching the "created school" query), the system records which education levels are present among those new schools. It then schedules a parallel IJobChunk (InitializeSchoolsJob) over all Student components and, for students whose associated school entity matches one of the newly-created education levels, removes the Student component from the student entity and adds a StudentsRemoved marker component to the school entity. The system uses a ModificationBarrier5 command buffer to perform structural changes safely from the job.


Fields

  • private ModificationBarrier5 m_ModificationBarrier The barrier system used to create an EntityCommandBuffer.ParallelWriter so structural changes (AddComponent/RemoveComponent) can be performed by the job in a safe, deferred manner.

  • private EntityQuery m_CreatedSchoolQuery EntityQuery that matches newly created school entities. It is configured to match entities that have School, Building, Updated, and PrefabRef components and excludes Temp and Deleted. This query is required for the system to run (RequireForUpdate).

  • private EntityQuery m_StudentQuery EntityQuery that matches all Student components (Game.Citizens.Student) except Deleted. This query is used to schedule the InitializeSchoolsJob over chunks that contain Student components.

  • private TypeHandle __TypeHandle Holds cached ECS type handles and component lookups used by the job: EntityTypeHandle, ComponentTypeHandle (read-only), ComponentLookup (read-only), and ComponentLookup (read-only). It provides a method to assign these handles from the SystemState for efficient access during job scheduling.

  • private struct InitializeSchoolsJob (nested) IJobChunk implementation that runs per-chunk over student entities. It reads entity and student arrays, checks whether the student's school entity has a School component (and does not have a Building component), and whether the school's education level was among those recorded as newly created. If conditions match, it adds a StudentsRemoved component to the school entity and removes the Student component from the student entity via the parallel command buffer. The job also holds a DeallocateOnJobCompletion NativeArray (m_NewLevels) that indicates which education levels were created.

Properties

  • (none) This system exposes no public properties.

Constructors

  • public InitializeSchoolSystem() Default constructor. Marked with [Preserve] attribute in the compiled code to avoid stripping. The main initialization happens in OnCreate.

Methods

  • protected override void OnCreate() : System.Void Sets up the system:
  • Acquires the ModificationBarrier5 system instance from the world.
  • Creates m_CreatedSchoolQuery to detect newly-created schools (School, Building, Updated, PrefabRef; excludes Temp, Deleted).
  • Creates m_StudentQuery for all Student components (excludes Deleted).
  • Calls RequireForUpdate(m_CreatedSchoolQuery) so the system only updates when created school entities exist.

  • protected override void OnUpdate() : System.Void Main update logic:

  • Reads PrefabRef components from entities matched by m_CreatedSchoolQuery into a temporary NativeArray.
  • Builds a NativeArray newLevels of length 4 and sets entries for any education levels (1..4) present among created school prefabs (reads SchoolData from the prefab entity to obtain m_EducationLevel).
  • Constructs and schedules an InitializeSchoolsJob with component/lookup handles, the newLevels array, and a parallel command buffer from the modification barrier.
  • Schedules the job in parallel using JobChunkExtensions.ScheduleParallel and registers the resulting dependency with the modification barrier (AddJobHandleForProducer).
  • Disposes the temporary PrefabRef array.

  • private void __AssignQueries(ref SystemState state) : System.Void Compiler-generated helper used during OnCreateForCompiler. In the decompiled code it creates (and immediately disposes) an EntityQueryBuilder(Allocator.Temp). This is part of the generated boilerplate for systems.

  • protected override void OnCreateForCompiler() : System.Void Compiler helper that calls __AssignQueries and assigns the TypeHandle handles from the SystemState (via __TypeHandle.__AssignHandles). Present to satisfy generated code paths.

  • private struct TypeHandle.__AssignHandles(ref SystemState state) : System.Void Assigns EntityTypeHandle, ComponentTypeHandle (read-only), and ComponentLookup/ComponentLookup (read-only) from the given SystemState for use when scheduling the job.

  • private struct InitializeSchoolsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void Job execution implementation that iterates entities in a chunk:

  • Retrieves native arrays for Entity and Game.Citizens.Student.
  • For each student: checks the student's m_School entity for School component and absence of Building; checks student.m_Level is within 1..4; checks if newLevels[student.m_Level - 1] != 0.
  • If all conditions met, it adds a StudentsRemoved component to the school entity and removes the Student component from the student entity via the parallel ECB.

Usage Example

// This example demonstrates the conditions that cause InitializeSchoolSystem to run:
// When a school entity is created with a PrefabRef pointing to a prefab that has SchoolData.m_EducationLevel
// within 1..4, the system will schedule a job to remove Student components whose student.m_School
// matches such newly-created school levels.

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // System initialization happens automatically in InitializeSchoolSystem.OnCreate:
    // - it will require the created-school query, so the system only runs when schools are created.
}

// Example: creating a school entity (pseudo-code)
// var schoolEntity = entityManager.CreateEntity();
// entityManager.AddComponentData(schoolEntity, new School { /* ... */ });
// entityManager.AddComponentData(schoolEntity, new Building { /* ... */ });
// entityManager.AddComponentData(schoolEntity, new Updated { });
// entityManager.AddComponentData(schoolEntity, new PrefabRef { m_Prefab = schoolPrefabEntity });

// After such a creation, InitializeSchoolSystem.OnUpdate will detect the new school and schedule
// InitializeSchoolsJob which may remove Student components from student entities that reference
// that school and match the recorded education level.

{{ This system is designed to perform deferred structural changes safely using a ModificationBarrier5 command buffer. Pay attention to the fact that education levels are expected to be in the range 1..4; the system uses a fixed-length NativeArray of length 4 to flag newly-created levels. The StudentsRemoved component is added to school entities to mark that students were removed as part of initialization. }}