Skip to content

Game.Zones.BlockReferencesSystem

Assembly: Assembly-CSharp
Namespace: Game.Zones

Type: class

Base: GameSystemBase

Summary:
BlockReferencesSystem keeps owner entities' SubBlock buffers in sync with Block entities as they are created and deleted. It queries for entities with Block and Owner components and reacts to the presence of Created or Deleted to add or remove SubBlock entries from the Owner's buffer. The system uses an inner Burst-compiled IJobChunk (UpdateBlockReferencesJob) and BufferLookup to perform these updates efficiently on chunks.


Fields

  • private struct UpdateBlockReferencesJob
    Contains the Burst-compiled IJobChunk that performs the per-chunk work. Key fields:
  • m_EntityType (EntityTypeHandle) — used to read the chunk's Entity array.
  • m_OwnerType (ComponentTypeHandle) — used to read the Owner component on each Block entity.
  • m_CreatedType (ComponentTypeHandle) — read-only handle used to test whether a chunk contains Created (presence determines add vs remove).
  • m_Blocks (BufferLookup) — used to access the owner's SubBlock dynamic buffer to add or remove SubBlock entries.

Behavior: for each entity in a chunk, if the chunk has the Created component it calls CollectionUtils.TryAddUniqueValue(m_Blocks[owner.m_Owner], new SubBlock(blockEntity)); otherwise it calls CollectionUtils.RemoveValue(m_Blocks[owner.m_Owner], new SubBlock(blockEntity)). The job implements IJobChunk.Execute and is annotated with [BurstCompile] for performance.

  • private struct TypeHandle
    Holds cached handle instances used by the system:
  • EntityTypeHandle __Unity_Entities_Entity_TypeHandle
  • ComponentTypeHandle<Owner> __Game_Common_Owner_RO_ComponentTypeHandle
  • ComponentTypeHandle<Created> __Game_Common_Created_RO_ComponentTypeHandle
  • BufferLookup<SubBlock> __Game_Zones_SubBlock_RW_BufferLookup

Provides __AssignHandles(ref SystemState state) which initializes those handles from the SystemState (GetEntityTypeHandle, GetComponentTypeHandle, GetBufferLookup). This struct is used to avoid repeatedly requesting handles every frame.

  • private EntityQuery m_BlockQuery
    The EntityQuery used to find relevant Block entities. It is constructed in OnCreate to match entities that have Block and Owner, and either Created or Deleted. The system calls RequireForUpdate(m_BlockQuery) so the system only runs when matching entities exist.

  • private TypeHandle __TypeHandle
    Instance of the TypeHandle struct above, used in OnUpdate and OnCreateForCompiler to access component/buffer handles.

Properties

  • None.

Constructors

  • public BlockReferencesSystem()
    The system ctor is preserved ([Preserve]) and otherwise uses the GameSystemBase constructor. No special runtime initialization beyond what is done in OnCreate.

Methods

  • protected override void OnCreate()
    Sets up the EntityQuery (m_BlockQuery) to select entities with Block + Owner and with either Created or Deleted. Calls RequireForUpdate(m_BlockQuery) so the system will only run when there are matching entities.

  • protected override void OnUpdate()
    Instantiates and schedules UpdateBlockReferencesJob. It uses InternalCompilerInterface to obtain the runtime EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, and BufferLookup from the cached __TypeHandle and the system's checked state reference. The job is scheduled with JobChunkExtensions.Schedule(...) and the returned JobHandle is stored into base.Dependency.

  • protected override void OnCreateForCompiler()
    Called by the compiler-generated boilerplate; it assigns queries and handles for the compiler path by calling __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef).

  • private void __AssignQueries(ref SystemState state)
    A small helper used during compiler-driven initialization. In this generated implementation it calls into an EntityQueryBuilder (temporary) and disposes it; the concrete query is created in the readable OnCreate method.

  • UpdateBlockReferencesJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Per-chunk worker that:

  • Retrieves the chunk's Entity array and Owner component array.
  • Checks whether the chunk "Has" the Created component type (chunk.Has(ref m_CreatedType)).
  • If Created is present, loops entities and adds a SubBlock (wrapping the block Entity) to the owner's SubBlock buffer via CollectionUtils.TryAddUniqueValue(...).
  • If Created is not present (i.e., Deleted path), removes the SubBlock entry via CollectionUtils.RemoveValue(...).
  • Implements the explicit interface method IJobChunk.Execute which forwards to the above.

Notes: - The job uses BufferLookup to read/write owner buffers (RW). - The job fields are annotated with [ReadOnly] where applicable to enable better job scheduling/parallelism. - The job is [BurstCompile] for performance.

Usage Example

// The system itself constructs and schedules the job in OnUpdate.
// Example of how creating a block entity would cause the system to add a SubBlock
// to the owner's buffer (pseudo-code, showing the relevant components):

// Create or find the owner entity that holds the SubBlock buffer
Entity ownerEntity = ...; // some owner entity with a SubBlock buffer

// Create a block entity and add required components
Entity block = entityManager.CreateEntity();
entityManager.AddComponentData(block, new Block());
entityManager.AddComponentData(block, new Owner { m_Owner = ownerEntity });
entityManager.AddComponentData(block, new Created()); // mark as created to trigger add

// After the framework updates systems, BlockReferencesSystem will:
// - detect the Block+Owner with Created
// - schedule UpdateBlockReferencesJob
// - the job will call CollectionUtils.TryAddUniqueValue on the owner's SubBlock buffer
//   adding new SubBlock(block)

If you need a more concrete example showing how to create the Owner and SubBlock buffer or how to unit-test this system in DOTS (EntityManager/World setup), tell me what context you are using (authoring vs runtime tests) and I can add a focused snippet.