Skip to content

Game.SecondaryObjectReferencesSystem

Assembly: Assembly-CSharp
Namespace: Game.Objects

Type: class

Base: GameSystemBase

Summary:
SecondaryObjectReferencesSystem maintains and updates references from owner entities to their secondary sub-objects. It queries entities that are marked as Object + Owner + Secondary (and that are newly Created or Deleted), excluding Vehicles and Creatures. For matching entities it schedules a Burst-compiled IJobChunk (UpdateSecondaryObjectReferencesJob) that:

  • Removes sub-object entries from an owner's SubObject buffer when the object is Deleted.
  • Adds a sub-object entry only when the owner has a Temp component if the object itself is Temp.
  • Otherwise adds the sub-object entry uniquely to the owner's SubObject buffer.

The system uses Entity/Component type handles and Buffer/Component lookups and integrates with Unity's job system and Burst for chunked, parallel execution. It also contains an internal TypeHandle struct to cache component and buffer handles used by the job.

Fields

  • private Unity.Entities.EntityQuery m_ObjectQuery
    Used to select entities that have Object, Owner and Secondary components, with an Any filter on Created or Deleted, and None on Vehicle or Creature. This query is required for the system to run (RequireForUpdate).

  • private TypeHandle __TypeHandle
    Internal helper struct instance that aggregates EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentLookup, and BufferLookup. It's populated for the current SystemState and used to build the job data before scheduling.

Properties

  • (none)
    This system does not expose public properties.

Constructors

  • public SecondaryObjectReferencesSystem()
    [Preserve] default constructor. Initializes the system instance; actual query and handles are set up in OnCreate / OnCreateForCompiler.

Methods

  • protected override void OnCreate() : System.Void
    Sets up the m_ObjectQuery with:
  • All: Object (RO), Owner (RO), Secondary (RO)
  • Any: Created (RO), Deleted (RO)
  • None: Vehicle (RO), Creature (RO)
    Calls RequireForUpdate(m_ObjectQuery) so the system only runs when matching entities exist.

  • protected override void OnUpdate() : System.Void
    Builds an UpdateSecondaryObjectReferencesJob instance by obtaining the required Entity/Component type handles and buffer/component lookups through InternalCompilerInterface and the cached __TypeHandle. Schedules the job over m_ObjectQuery using JobChunkExtensions.Schedule and assigns the resulting handle to base.Dependency.

  • protected override void OnCreateForCompiler() : System.Void
    Called by the compiler-generated flow; calls __AssignQueries to satisfy generated code paths and invokes __TypeHandle.__AssignHandles to initialize the stored type handles for the system state.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Compiler helper; currently constructs and disposes an EntityQueryBuilder(Allocator.Temp). Present for generated code flow; does not create persistent queries here.

Nested types (summary)

  • private struct UpdateSecondaryObjectReferencesJob : IJobChunk
    Burst-compiled job that runs per-chunk. Fields:
  • ReadOnly EntityTypeHandle m_EntityType
  • ReadOnly ComponentTypeHandle m_OwnerType
  • ReadOnly ComponentTypeHandle m_DeletedType
  • ReadOnly ComponentTypeHandle m_TempType
  • ReadOnly ComponentLookup m_TempData
  • BufferLookup m_SubObjects

Execute(in ArchetypeChunk chunk, ...) behavior: - Retrieves native arrays for Entity and Owner components for the chunk. - If chunk.Has(Deleted): iterate and remove the sub-object Entity from the owner's SubObject buffer using CollectionUtils.RemoveValue. - Else if chunk.Has(Temp): for each entry, check if owner has Temp (via m_TempData.HasComponent) and if so add unique SubObject to owner's buffer (CollectionUtils.TryAddUniqueValue). - Else: for each entry add unique SubObject to owner's buffer.

The job uses BufferLookup to access owner buffers and ComponentLookup to test owner's temp status.

  • private struct TypeHandle
    Aggregates the component/buffer/type handles used by the job:
  • EntityTypeHandle
  • ComponentTypeHandle (RO)
  • ComponentTypeHandle (RO)
  • ComponentTypeHandle (RO)
  • ComponentLookup (RO)
  • BufferLookup (RW)

Method: __AssignHandles(ref SystemState state) fills these handles from the provided SystemState.

Notes: - The job is marked with [BurstCompile] for performance. - Several methods and the constructor are marked [Preserve] to avoid stripping. - Interaction with CollectionUtils ensures sub-object entries in owner buffers stay unique where intended.

Usage Example

// Example: Create an owner with a SubObject buffer and a secondary object entity.
// The SecondaryObjectReferencesSystem will add the sub-object to the owner's buffer on the next update.

var em = world.EntityManager;

// Create an owner entity and add a SubObject buffer
Entity owner = em.CreateEntity(typeof(Owner));
em.AddBuffer<SubObject>(owner);

// Create a sub-object entity and mark it as an Object, Secondary and Created
Entity subObject = em.CreateEntity(
    typeof(Unity.Entities.Object), // component name in this codebase is 'Object'
    typeof(Owner),
    typeof(Secondary),
    typeof(Created)
);

// Assign the Owner component so the system knows which owner to update
em.SetComponentData(subObject, new Owner { m_Owner = owner });

// After the next world update, SecondaryObjectReferencesSystem will add 'subObject' to the owner's SubObject buffer.

If you want, I can also: - Generate a short diagram of the data flow between Owner, SubObject buffer and Secondary objects, - Produce a modding guide section showing how to create Owner/SubObject components and how to mark objects with Created/Deleted/Temp flags so this system behaves as expected.