Game.Tools.ToolApplySystem
Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Tools
Type: class
Base: GameSystemBase
Summary:
ToolApplySystem is an ECS system that finalizes pending "tool" operations produced by tool code. It finds entities that have temporary tool-state components (Temp, Warning, Override), applies command-buffer operations (marking entities Deleted, Updated, Overridden as appropriate), accumulates the monetary cost of non-cancelled Temp entries and subtracts that total from the city's PlayerMoney. Work is performed on a scheduled, Burst-compiled IJob (ApplyEntitiesJob) and command mutations are sent through a ToolOutputBarrier to run safely with the entity manager.
Fields
-
private ToolOutputBarrier m_ToolOutputBarrier
Handles creation of EntityCommandBuffer instances for use by the scheduled job. Ensures command buffers are played back at the correct point in the frame and coordinates job dependencies. -
private CitySystem m_CitySystem
Reference to the CitySystem used to obtain the current city Entity (m_CitySystem.City) so the system can lookup and modify the PlayerMoney component. -
private EntityQuery m_ApplyQuery
Query used to locate entities that have any of the Temp, Warning or Override components. This query is required for the system to run (RequireForUpdate). -
private TypeHandle __TypeHandle
Generated container that caches Entity/Component type handles and a ComponentLookup for PlayerMoney. It is assigned in OnCreateForCompiler and used in OnUpdate to obtain runtime handles suitable for job scheduling.
Properties
- This system has no public properties.
Constructors
public ToolApplySystem()
Default constructor. Systems are typically created/managed by the world; this constructor is preserved but does not perform initialization beyond default field setup. Initialization happens in OnCreate.
Methods
protected virtual OnCreate() : System.Void
Initializes the system:- Retrieves the ToolOutputBarrier and CitySystem from the world.
- Builds the m_ApplyQuery that matches any entity containing Temp, Warning or Override components.
-
Calls RequireForUpdate(m_ApplyQuery) so the system only runs when matching entities exist.
-
protected virtual OnUpdate() : System.Void
Main update path. Steps: - Creates an asynchronous archetype-chunk list from m_ApplyQuery (ToArchetypeChunkListAsync) producing a JobHandle for the query.
- Schedules a Burst-compiled ApplyEntitiesJob, passing:
- chunk list,
- entity/component type handles (Entity, Temp, Warning, Override),
- the city entity,
- a PlayerMoney ComponentLookup,
- an EntityCommandBuffer from the ToolOutputBarrier.
- Combine job dependencies with base.Dependency, disposes the chunk list via Dispose(jobHandle).
- Adds the job handle to m_ToolOutputBarrier for proper playback ordering and wires base.Dependency to the job handle.
-
The scheduled job:
- Iterates all chunks and their entities,
- If chunk has Warning component, issues a Deleted component on those entities via the command buffer,
- If chunk has Override component, issues Updated and Overridden components via the command buffer,
- Iterates Temp components, sums m_Cost for temps not flagged with TempFlags.Cancel,
- If sum != 0 and the city has PlayerMoney, subtracts the total cost from the PlayerMoney component.
-
private void __AssignQueries(ref SystemState state)
Generated helper used during compiler-time initialization; currently creates (and immediately disposes) an EntityQueryBuilder (placeholder logic for code generation compatibility). -
protected override void OnCreateForCompiler()
Generated initialization hook used by the compiled system: assigns queries and populates __TypeHandle by calling its __AssignHandles method with the checked SystemState reference. -
Nested types and important details:
- ApplyEntitiesJob (BurstCompile, implements IJob):
- Fields: m_Chunks (NativeList
), m_EntityType, m_TempType, m_WarningType, m_OverrideType, m_City, m_PlayerMoney (ComponentLookup ), m_CommandBuffer (EntityCommandBuffer). - Execute(): performs the logic described above (mark Deleted/Updated/Overridden and subtract costs).
- Fields: m_Chunks (NativeList
- TypeHandle:
- Holds EntityTypeHandle, ComponentTypeHandles for Temp/Warning/Override (read-only), and a ComponentLookup
(read/write). - __AssignHandles(ref SystemState) obtains the appropriate runtime handles from the provided state.
- Holds EntityTypeHandle, ComponentTypeHandles for Temp/Warning/Override (read-only), and a ComponentLookup
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by the system:
m_ToolOutputBarrier = base.World.GetOrCreateSystemManaged<ToolOutputBarrier>();
m_CitySystem = base.World.GetOrCreateSystemManaged<CitySystem>();
m_ApplyQuery = GetEntityQuery(new EntityQueryDesc
{
Any = new ComponentType[3]
{
ComponentType.ReadOnly<Temp>(),
ComponentType.ReadOnly<Warning>(),
ComponentType.ReadOnly<Override>()
}
});
RequireForUpdate(m_ApplyQuery);
}
Additional notes: - The system expects tools to write Temp, Warning or Override components to entities to indicate pending tool operations. Temp components include cost and flags (e.g., TempFlags.Cancel). The system only deducts costs for Temp entries that are not cancelled. - All structural changes are queued via an EntityCommandBuffer produced by ToolOutputBarrier; playback timing and job synchronization are managed by that barrier to avoid race conditions with the EntityManager.