Skip to content

Game.FindOwnersSystem2

Assembly: Assembly-CSharp
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
FindOwnersSystem2 is an ECS system used to locate and assign owners for "sub-entities" (subnets, subareas, subobjects) in the game. It builds two EntityQuery sets (one for owner candidates and one for the sub-entities that should receive owners), collects owner chunks asynchronously, and schedules a parallel job (SetSubEntityOwnerJob) to set Owner components on matching sub-entities using a ModificationBarrier2B command buffer. The system uses a generated TypeHandle struct to cache ComponentTypeHandle/EntityTypeHandle instances for efficient job access and is annotated with [Preserve] for AOT/linker safety.


Fields

  • private ModificationBarrier2B m_ModificationBarrier
    Used to create an EntityCommandBuffer (as a parallel writer) to safely modify components (such as writing Owner) from the scheduled job. The system obtains this from the World in OnCreate.

  • private EntityQuery m_OwnersQuery
    An EntityQuery that matches entities that have Updated plus any of Game.Net.SubNet, Game.Areas.SubArea, or Game.Objects.SubObject, and do not have Deleted. This query is used to collect "owner" archetype chunks into a NativeList which the job iterates to find owner data.

  • private EntityQuery m_SubEntityQuery
    An EntityQuery that matches entities that have OwnerDefinition and Owner and are any of Game.Net.Node, Edge, Area, or Object. This query is the query passed to ScheduleParallel for the SetSubEntityOwnerJob; RequireForUpdate(m_SubEntityQuery) is called in OnCreate to ensure the system only runs when there are matching sub-entities.

  • private TypeHandle __TypeHandle
    A generated nested struct instance that holds cached EntityTypeHandle and ComponentTypeHandle instances for all components the job reads/writes (PrefabRef, Transform, Curve, OwnerDefinition, Temp, Owner, Entity). The struct has an __AssignHandles method to initialize the handles from a SystemState.

Properties

  • (no public properties)
    This system exposes no public properties. All state is held in private fields and handled through the scheduled job and queries.

Constructors

  • public FindOwnersSystem2()
    Default parameterless constructor. Marked with [Preserve] on lifecycle methods to avoid stripping; the constructor itself is empty in the generated code.

Methods

  • protected override void OnCreate()
    Initializes the system: obtains a ModificationBarrier2B from the world, constructs m_OwnersQuery to find potential owners (Updated + SubNet/SubArea/SubObject; not Deleted), constructs m_SubEntityQuery to find sub-entities that should have owners (OwnerDefinition + Owner and are Node/Edge/Area/Object), and calls RequireForUpdate(m_SubEntityQuery) so the system only runs when those sub-entities exist.

  • protected override void OnUpdate()
    Main runtime logic:

  • Converts m_OwnersQuery to a NativeList asynchronously, obtaining an outJobHandle for the query enumeration.
  • Builds and schedules a parallel job of type SetSubEntityOwnerJob (a job struct referenced in the file) using JobChunkExtensions.ScheduleParallel with m_SubEntityQuery and combined dependencies.
  • Passes into the job: the owner chunk list, EntityTypeHandle and ComponentTypeHandles resolved from __TypeHandle via InternalCompilerInterface.Get*Handle calls, and a parallel EntityCommandBuffer from m_ModificationBarrier.
  • Disposes the NativeList after scheduling (Dispose scheduled with the returned jobHandle).
  • Adds the produced job handle to the m_ModificationBarrier and stores it as base.Dependency.

The scheduled job is responsible for iterating sub-entity chunks, finding the appropriate owner data from the ownerChunks list, and writing the Owner component via the command buffer.

  • private void __AssignQueries(ref SystemState state)
    A generated helper that currently only creates and disposes an EntityQueryBuilder with Allocator.Temp (likely a placeholder generated by the compiler). It may exist to satisfy compiler-generation patterns; the real query initialization occurs in OnCreate.

  • protected override void OnCreateForCompiler()
    Compiler-targeted initialization: calls __AssignQueries to ensure any query-related generated code executes, and invokes __TypeHandle.__AssignHandles to populate the cached type handles from the SystemState.

  • private struct TypeHandle.__AssignHandles(ref SystemState state)
    (in nested TypeHandle) Initializes all EntityTypeHandle and ComponentTypeHandle fields from the provided SystemState. Handles are cached for use inside jobs to avoid repeated reflection or lookup costs at runtime. The handles initialized include Entity, PrefabRef (read-only), Transform (read-only), Curve (read-only), OwnerDefinition (read-only), Temp (read-only), and Owner (read/write).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier2B>();

    m_OwnersQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[1] { ComponentType.ReadOnly<Updated>() },
        Any = new ComponentType[3]
        {
            ComponentType.ReadOnly<Game.Net.SubNet>(),
            ComponentType.ReadOnly<Game.Areas.SubArea>(),
            ComponentType.ReadOnly<Game.Objects.SubObject>()
        },
        None = new ComponentType[1] { ComponentType.ReadOnly<Deleted>() }
    });

    m_SubEntityQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[2]
        {
            ComponentType.ReadOnly<OwnerDefinition>(),
            ComponentType.ReadOnly<Owner>()
        },
        Any = new ComponentType[4]
        {
            ComponentType.ReadOnly<Game.Net.Node>(),
            ComponentType.ReadOnly<Edge>(),
            ComponentType.ReadOnly<Area>(),
            ComponentType.ReadOnly<Object>()
        }
    });

    RequireForUpdate(m_SubEntityQuery);
}

Notes and tips for modders: - The system expects Owner and OwnerDefinition to already be present on sub-entities; OwnerDefinition provides the linking criteria and Owner is the component that will be set/updated. - The heavy work is done inside a scheduled job (SetSubEntityOwnerJob). If you need to modify or extend logic, look for or implement that job struct; ensure you preserve the same ComponentTypeHandle usage pattern for performance and safety. - The system uses a ModificationBarrier2B command buffer for safe parallel writes; do not try to write Owner directly from the job without using the provided command buffer. - The [Preserve] attributes indicate the methods are important for AOT/linking and should not be stripped by the Unity managed code stripper.