Skip to content

Game.Prefabs.CompanyInitializeSystem

Assembly: Game
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
CompanyInitializeSystem is a compiler-generated ECS system that prepares company-related prefab data. Its responsibilities are: - When Brand prefabs are created, add Brand entries (CompanyBrandElement) to the corresponding Company prefabs so companies know which brand Entities belong to them. - Compute affiliated brands for companies (AffiliatedBrandElement) by analyzing each company's IndustrialProcessData (inputs/outputs) and matching those resources with other companies' outputs/inputs. This computation is performed inside a Burst-compiled IJob (InitializeAffiliatedBrandsJob) using NativeParallelMultiHashMap for efficient cross-referencing. - Remove references to deleted brand entities and deduplicate/sort affiliated-brand buffers. The system uses PrefabSystem to access prefab instances and relies on EntityQuery to find relevant prefabs. It schedules a job to compute affiliated brands and manages component/buffer type handles via an internal TypeHandle struct.


Fields

  • private EntityQuery m_PrefabQuery
    Query used to find Brand prefabs (BrandData + PrefabData) so the system can attach brands to companies when brand prefabs are created.

  • private EntityQuery m_CompanyQuery
    Query used to find company prefabs (company prefabs with CompanyBrandElement buffer and PrefabData, excluding Deleted) used as input for the affiliated-brand initialization job.

  • private PrefabSystem m_PrefabSystem
    Cached reference to PrefabSystem (retrieved from the World) used to read specific prefab instances and to get/set prefab buffers such as CompanyBrandElement and AffiliatedBrandElement.

  • private TypeHandle __TypeHandle
    Compiler-generated struct holding EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle instances (and a Deleted ComponentLookup) used to access component/buffer data efficiently in OnUpdate and in scheduled jobs.

Properties

  • This type exposes no public properties.

Constructors

  • public CompanyInitializeSystem()
    Default (parameterless) constructor. The constructor is preserved for engine/IL2CPP usages ([Preserve] attribute is applied on lifecycle methods). No special initialization logic occurs in the constructor — initialization is done in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes the system: retrieves PrefabSystem from the World, builds the m_PrefabQuery (Brand prefabs + PrefabData and optionally Created/Deleted), builds the m_CompanyQuery (company prefabs that have CompanyBrandElement buffers), and calls RequireForUpdate(m_PrefabQuery) so the system runs only when relevant brand prefabs exist or change.

  • protected override void OnUpdate()
    Primary runtime logic:

  • Collects archetype chunks matching m_PrefabQuery and iterates them to attach brand Entities to company prefabs (using PrefabSystem.GetBuffer(companyPrefab).Add(...)).
  • Schedules InitializeAffiliatedBrandsJob to compute affiliated brands for each company prefab. The job uses the m_CompanyQuery chunk list (asynchronously obtained) and multiple component/buffer handles obtained from __TypeHandle.
  • Ensures the chunk list is disposed after scheduling and wires the job handle into the system's base.Dependency.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper used to assign or validate queries. In this implementation the method is effectively a placeholder (it creates and disposes an EntityQueryBuilder). It's invoked from OnCreateForCompiler to let the incremental compiler set up queries/handles.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization entry: calls __AssignQueries and __TypeHandle.__AssignHandles(ref state) to populate the TypeHandle values from the current SystemState. This is an internal hookup used by the generated code and the ECS compiler.

  • Nested type: private struct InitializeAffiliatedBrandsJob : IJob (BurstCompile)
    Purpose: Build mappings between resource indexes and brands and then populate per-company AffiliatedBrandElement buffers. High-level behavior:

  • Allocates two NativeParallelMultiHashMap for mapping resourceIndex -> brand Entity (one for outputs, one for inputs).
  • First pass: iterate chunks and their IndustrialProcessData + CompanyBrandElement buffers; remove deleted brands from company brand buffers, and populate the maps:
    • For companies that are not storage companies: add their company brands to the input-to-brand map for each input resource.
    • For non-storage/non-commercial companies: add their company brands to the output-to-brand map for outputs.
  • Second pass: iterate company chunks again and, for each company's IndustrialProcessData, collect matching brands from the maps for each input/output resource into the AffiliatedBrandElement buffer:
    • Clears the affiliated buffer, adds matches, sorts & deduplicates the buffer, trims excess, and removes Entity.Null placeholders.
  • Disposes both maps at the end. Notes:

    • Uses ComponentLookup to remove brand entries that refer to deleted Entities.
    • Uses EconomyUtils.GetResourceIndex to convert Resource to an index to use as map keys.
    • The job is Burst-compiled for performance and uses Native containers allocated with Allocator.Temp, so it must finish within a frame and is scheduled as a short-lived job.
  • Nested type: private struct TypeHandle
    Holds read-only/read-write handles used by the system and job:

    • EntityTypeHandle
    • ComponentTypeHandle (RO)
    • ComponentTypeHandle (RO)
    • ComponentTypeHandle (RW)
    • ComponentLookup (RO)
    • ComponentTypeHandle (RO)
    • ComponentTypeHandle (RO)
    • ComponentTypeHandle (RO)
    • BufferTypeHandle (RW)
    • BufferTypeHandle (RW) Contains method __AssignHandles(ref SystemState state) to fill these handles from a SystemState (used by OnCreateForCompiler).

Usage Example

// Example: read affiliated brands for a company prefab from another system.
// Ensure this system runs before your system if you need the affiliated data updated,
// e.g. use [UpdateAfter(typeof(Game.Prefabs.CompanyInitializeSystem))] on your system.

public partial class MyInspectionSystem : SystemBase
{
    protected override void OnCreate()
    {
        base.OnCreate();
        // Optionally get PrefabSystem if you need to read prefab instances
    }

    protected override void OnUpdate()
    {
        var prefabSystem = World.GetExistingSystemManaged<PrefabSystem>();
        if (prefabSystem == null) return;

        // Suppose you have a company prefab entity (companyPrefabEntity)
        Entity companyPrefabEntity = /* obtain company prefab entity */ Entity.Null;
        if (companyPrefabEntity == Entity.Null) return;

        // Read affiliated brands (do not modify)
        var affiliatedBuffer = prefabSystem.GetBuffer<AffiliatedBrandElement>(companyPrefabEntity, isReadOnly: true);
        foreach (var affiliated in affiliatedBuffer)
        {
            Entity brandEntity = affiliated.m_Brand;
            // Inspect brandEntity (e.g. get BrandData from entity)
        }
    }
}

Notes and modding tips: - Prefer depending on this system via ECS ordering (UpdateAfter) or RequireForUpdate rather than trying to invoke OnUpdate manually. - The affiliated brand computation is scheduled as a job; if you need results immediately on the main thread in the same frame, you must ensure job completion (CompleteDependency()) before reading buffers (but typically you should read them in a later system). - Be careful when manipulating CompanyBrandElement or AffiliatedBrandElement buffers from your own code — keep thread-safety and schedule/order considerations in mind.