Game.Citizens.CompanyInitializeSystem
Assembly:
(assembly not present in source — likely the main game assembly for Cities: Skylines 2 runtime)
Namespace: Game.Citizens
Type: class
Base: GameSystemBase
Summary:
Initializes newly created company entities (processing companies, extractors, service companies, etc.) when they enter the simulation. This system runs on entities that have just been Created and are not Temp or Deleted and sets up runtime CompanyData, Profitability, starting Resources, ServiceAvailable, LodgingProvider defaults, and enqueues rent actions for property renters. It schedules a Burst-compiled IJobChunk (InitializeCompanyJob) to perform per-chunk initialization in parallel and coordinates with ResourceSystem and PropertyProcessingSystem for resource prefabs and rent action queue access.
Fields
-
private ResourceSystem m_ResourceSystem
Initializes and holds a reference to the ResourceSystem (readers for resource prefabs). Used to obtain ResourcePrefabs for price lookups and resource-prefab data when adding starting resources. -
private PropertyProcessingSystem m_PropertyProcessingSystem
Holds a reference to the PropertyProcessingSystem. The system is used to obtain the RentAction queue (as a parallel writer) so newly created renter entities can enqueue rent actions. -
private EntityQuery m_CreatedGroup
EntityQuery that matches created company entities to be initialized. Built in OnCreate and used to schedule the InitializeCompanyJob. Query includes CompanyData, Profitability, PrefabRef, Created and excludes Deleted and Temp. -
private TypeHandle __TypeHandle
Compiler-generated container holding many ComponentTypeHandle/BufferTypeHandle/BufferLookup/ComponentLookup instances used by the job. Populated via __AssignHandles(ref SystemState) so job scheduling can access component handles efficiently. -
private EntityQuery __query_1030701297_0
Compiler-generated EntityQuery used to fetch a singleton EconomyParameterData (includes EntityQueryOptions.IncludeSystems). Assigned by __AssignQueries and used by the job to read economy parameters.
Properties
- No public properties on this system.
(Internally the nested job uses many ComponentTypeHandle and lookups via __TypeHandle, but the system itself exposes no public properties.)
Constructors
public CompanyInitializeSystem()
Default constructor. Marked with [Preserve] in source; standard managed-system construction, no custom runtime initialization beyond what OnCreate performs.
Methods
protected override void OnCreate()
: System.Void
Sets up system dependencies and queries:- Retrieves ResourceSystem and PropertyProcessingSystem from the World.
- Builds m_CreatedGroup to match newly created company entities (CompanyData, Profitability, PrefabRef, Created; excludes Deleted and Temp).
- Calls RequireForUpdate on the created-group and also requires EconomyParameterData singleton be present.
-
Effect: system will run only when there are newly created companies and economy parameters exist.
-
protected override void OnUpdate()
: System.Void
Prepares and schedules the InitializeCompanyJob (Burst compiled, runs as IJobChunk) in parallel: - Fills job fields including EntityTypeHandle, component type handles (PrefabRef, CompanyData, Profitability, Resources buffer, ServiceAvailable, LodgingProvider), buffer/component lookups (CompanyBrandElement, IndustrialProcessData, ServiceCompanyData, ResourceData, PropertyRenter), ResourcePrefabs from ResourceSystem, a RandomSeed, EconomyParameterData singleton, and RentActionQueue (parallel writer) from PropertyProcessingSystem.
- Schedules the job across the m_CreatedGroup using ScheduleParallel and combines dependencies with the rent queue writer dependency.
- Registers the resulting JobHandle with PropertyProcessingSystem (AddWriter) and as a prefabs reader with ResourceSystem (AddPrefabsReader).
-
Effect: company initialization runs concurrently and updates dependent systems' read/write registrations.
-
private void __AssignQueries(ref SystemState state)
: System.Void
Compiler-generated method that builds the internal __query_1030701297_0 (an EntityQuery that includes the EconomyParameterData singleton with IncludeSystems option). Called from OnCreateForCompiler. -
protected override void OnCreateForCompiler()
: System.Void
Compiler-generated helper used at startup to assign queries and component handles into __TypeHandle. Calls __AssignQueries and __TypeHandle.__AssignHandles to initialize internal handles used by the job. -
Nested type: InitializeCompanyJob (private struct, [BurstCompile], implements IJobChunk)
- Purpose: chunk-based initialization of created company entities.
- Main per-chunk Execute behavior:
- Obtains arrays/accessors for Entity, PrefabRef, CompanyData, Profitability, Resources buffer, ServiceAvailable, LodgingProvider.
- Detects whether the chunk has ProcessingCompany or ExtractorCompany markers and whether the ServiceAvailable component array is present.
- For each entity in the chunk:
- Selects a Brand entity from the prefab's CompanyBrandElement buffer using a Random seeded per-entity.
- Writes CompanyData (including Random seed and selected brand).
- Sets Profitability.m_Profitability = 127 (initial default).
- If ServiceAvailable exists, reads ServiceCompanyData from the prefab and sets initial m_ServiceAvailable to half of the prefab m_MaxService and mean priority 0f.
- If ProcessingCompany exists, reads IndustrialProcessData from the prefab and adds starting input resources to the company's Resources buffer. Amounts differ depending on whether the prefab is service-capable or not and whether the company is an extractor:
- If Services are present (flag), adds 3,000 units of each input resource.
- Otherwise for processing companies: adds 15,000 of each input and potentially some output (1000 units) if the output is a material (checked by EconomyUtils.IsMaterial) and the company is not an extractor.
- If entity has a PropertyRenter component with m_Property != Entity.Null, enqueues a RentAction into the RentActionQueue (m_Property and m_Renter).
- After per-entity loop, initializes all LodgingProvider components in the chunk to m_FreeRooms = 0 and m_Price = -1.
-
Helper method AddStartingResources(DynamicBuffer
buffer, Resource resource, int amount): - If resource != Resource.NoResource:
- Computes money cost: num = round(amount * EconomyUtils.GetIndustrialPrice(resource, m_ResourcePrefabs, ref m_ResourceDatas)).
- Adds "amount" of the specified resource to the buffer and subtracts the computed money by adding a negative Money resource entry.
- This ensures starting inputs are funded at appropriate cost according to current resource prices.
-
InitializeCompanyJob.Execute is implemented explicitly for IJobChunk to call the struct's Execute implementation. The job is Burst-compiled and uses various ReadOnly and read/write handles to perform initialization safely in parallel.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// This system creates a query that matches newly created companies and requires EconomyParameterData.
m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
m_PropertyProcessingSystem = base.World.GetOrCreateSystemManaged<PropertyProcessingSystem>();
m_CreatedGroup = GetEntityQuery(
ComponentType.ReadWrite<CompanyData>(),
ComponentType.ReadWrite<Profitability>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<Created>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>());
RequireForUpdate(m_CreatedGroup);
RequireForUpdate<EconomyParameterData>();
}
Additional notes / tips for modders: - If you add new company-related prefabs with custom Resource or IndustrialProcessData, ensure their data components are present on the prefab so this system will initialize resources correctly. - Changes to EconomyUtils (price lookup, IsMaterial) can affect the calculated starting money subtraction and added resource amounts. - The job enqueues RentAction for entities that have PropertyRenter with a non-null m_Property; hooking into PropertyProcessingSystem requires correct writer registration (this system already adds the writer). - Because initialization is chunked and scheduled as a parallel job, any code that depends on fully-initialized companies should respect system/job dependencies (use the system's JobHandle if interacting with ResourceSystem/PropertyProcessingSystem).