Game.Simulation.CompanyProfitabilitySystem
Assembly:
Assembly-CSharp (typical Unity game assembly)
Namespace:
Game.Simulation
Type:
public class
Base:
GameSystemBase
Summary:
Updates company profitability each simulation update frame by scheduling a parallel IJobChunk (CompanyProfitabilityJob) which reads company resources, owned vehicles, layout elements and delivery trucks and writes to the Profitability component. The system runs infrequently (controlled by kUpdatesPerDay) using an UpdateFrame shared component to pick the correct chunks for each update. It uses ResourceSystem for resource prefabs and reports back its job handle to an EndFrameBarrier so the end-of-frame system can depend on it.
Fields
-
public static readonly int kUpdatesPerDay
Number of profitability updates per in-game day. Value is 1 in this implementation and is used to compute the system update interval. -
private EndFrameBarrier m_EndFrameBarrier
Reference to the EndFrameBarrier system used to register the scheduled job handle (producer). -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain frameIndex for computing the current UpdateFrame. -
private ResourceSystem m_ResourceSystem
Reference to ResourceSystem, used to obtain ResourcePrefabs passed into the job. -
private EntityQuery m_ProfitabilityCompanyQuery
EntityQuery selecting companies that have a Profitability component and are not marked Created or Deleted. The query is required for the system to run. -
private TypeHandle __TypeHandle
Container for cached component/buffer/shared-component handles (SharedComponentTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, BufferLookup). Populated in OnCreateForCompiler via __AssignHandles to be converted to runtime handles in OnUpdate. -
private struct CompanyProfitabilityJob
(nested)
IJobChunk that performs the per-chunk processing of Profitability components. See Methods for details. -
private struct TypeHandle
(nested)
Helper struct used at compile-time to obtain component/buffer type handles and lookups from SystemState.
Properties
- None (the system exposes no public properties)
Constructors
public CompanyProfitabilitySystem()
Default constructor. (Annotated with [Preserve] on OnCreate/OnUpdate methods; constructor itself has the default behavior.)
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval in frames for the system. Computed as 262144 / (kUpdatesPerDay * 16). For kUpdatesPerDay = 1 this evaluates to 16384. -
[Preserve] protected override void OnCreate()
Initializes subsystem references and the EntityQuery: - Gets or creates EndFrameBarrier, SimulationSystem and ResourceSystem from the World.
- Builds the m_ProfitabilityCompanyQuery to select entities with Profitability and exclude Created/Deleted.
-
Calls RequireForUpdate(m_ProfitabilityCompanyQuery) so the system only runs when such entities exist.
-
[Preserve] protected override void OnUpdate()
Main update logic: - Computes the current update frame index via SimulationUtils.GetUpdateFrame using m_SimulationSystem.frameIndex and kUpdatesPerDay.
- Instantiates CompanyProfitabilityJob and fills in:
- component/buffer/shared-component handles (converted from the compile-time __TypeHandle via InternalCompilerInterface.Get* calls)
- ComponentLookup/BufferLookup references
- ResourcePrefabs from m_ResourceSystem
- m_UpdateFrameIndex
-
Schedules the job with JobChunkExtensions.ScheduleParallel using m_ProfitabilityCompanyQuery and adds the returned dependency to m_EndFrameBarrier via AddJobHandleForProducer so later systems can depend on it.
-
private void __AssignQueries(ref SystemState state)
Compiler helper used to set up queries at compile-time. In this file it only creates and disposes a temporary EntityQueryBuilder (placeholder—actual handles are assigned elsewhere). -
protected override void OnCreateForCompiler()
Compiler-time initialization method: - Calls __AssignQueries to ensure queries are prepared for the generated code path.
-
Calls __TypeHandle.__AssignHandles(ref state) to fill the compile-time TypeHandle entries.
-
private struct TypeHandle.__AssignHandles(ref SystemState state)
Assigns the various SharedComponentTypeHandle, ComponentTypeHandle, BufferTypeHandle and ComponentLookup/BufferLookup fields from the supplied SystemState. This is used so the OnUpdate method can convert them into runtime handles (via InternalCompilerInterface) before scheduling the job. -
private struct CompanyProfitabilityJob : IJobChunk
Job fields: - ComponentTypeHandle
m_ProfitabilityType - BufferTypeHandle
m_ResourcesBufType (read-only) - SharedComponentTypeHandle
m_UpdateFrameType (read-only) - BufferTypeHandle
m_OwnedVehicleType (read-only) - ComponentLookup
m_ResourceDatas (read-only) - ComponentLookup
m_DeliveryTrucks (read-only) - BufferLookup
m_LayoutElementBufs (read-only) - ResourcePrefabs m_ResourcePrefabs (read-only)
- uint m_UpdateFrameIndex (the index this job run should process)
Job behavior (Execute): - Skips the chunk unless its UpdateFrame shared component index matches m_UpdateFrameIndex. - Reads Profitability native array and resource/vehicle buffers for each entity in the chunk. - Calls EconomyUtils.GetCompanyTotalWorth(...) passing: - the Resources buffer for the company - the OwnedVehicle buffer for the company (if present) - references to the BufferLookup of LayoutElement, the DeliveryTruck ComponentLookup, ResourcePrefabs and ResourceData lookup - Computes profitability as: - diff = (companyTotalWorth - value.m_LastTotalWorth) / 100 - clamped = math.clamp(diff, -127, 128) - stored byte = (byte)(clamped + 127) - Updates Profitability.m_LastTotalWorth to companyTotalWorth - Writes updated Profitability back to the native array. - The job implements IJobChunk.Execute by forwarding to its own Execute method.
Notes on the profitability encoding: - Profitability.m_Profitability is stored as a byte using an offset encoding with +127 to make signed deltas fit in a byte. - The per-update delta is scaled by dividing by 100 and clamped to the range [-127, 128] before encoding.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
m_ProfitabilityCompanyQuery = GetEntityQuery(
ComponentType.ReadOnly<Profitability>(),
ComponentType.Exclude<Created>(),
ComponentType.Exclude<Deleted>()
);
RequireForUpdate(m_ProfitabilityCompanyQuery);
}
Additional notes for modders: - If you change how company worth is computed (EconomyUtils.GetCompanyTotalWorth), ensure the job reads/writes remain consistent and consider the clamping/encoding used for Profitability. - The system schedules a parallel JobChunk; if adding side effects or non-thread-safe reads, you must adapt scheduling or use suitable lookups/handles. - Update frequency is controlled by kUpdatesPerDay and the divisor in GetUpdateInterval — adjust carefully to avoid high per-frame load.