Game.Simulation.PropertyRenterRemoveSystem
Assembly:
Assembly-CSharp (game assembly)
Namespace:
Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
A simulation system that ensures PropertyRenter components are removed when their associated building no longer exists or when the number of renters attached to a building exceeds the configured property capacity for that building prefab. The system runs on a fixed update interval (see GetUpdateInterval) and uses two Burst-compiled jobs:
- UpdateRentersJob (IJobChunk) scans entities with PropertyRenter + UpdateFrame and enqueues removal requests when a building is missing or when the renter buffer exceeds the prefab's allowed property count.
- RemoveRentersJob (IJob) processes the queued removal requests, updates renter buffers, removes PropertyRenter components, and emits RentersUpdated events via an EndFrameBarrier command buffer.
The system uses a NativeQueue
Fields
-
private EntityQuery m_RenterGroup
Query selecting entities that have PropertyRenter and UpdateFrame components; used as the workset for UpdateRentersJob. -
private EntityArchetype m_RentEventArchetype
Archetype used to create RentersUpdated event entities (contains Event and RentersUpdated components). -
private SimulationSystem m_SimulationSystem
Cached reference to the SimulationSystem to obtain the current frame index for update scheduling decisions. -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create a command buffer to apply component removals and event creation at the end of the frame. -
private TypeHandle __TypeHandle
Struct containing stored type handles / lookups used when scheduling jobs (EntityTypeHandle, ComponentTypeHandles, SharedComponentTypeHandle, BufferLookups, etc.). -
Nested/related types (declared inside the system):
- UpdateRentersJob (Burst-compiled IJobChunk) — job that identifies renters to remove and enqueues RemoveData.
- RemoveData (struct) — simple container with m_Property (Entity) and m_Renter (Entity) describing what to remove.
- RemoveRentersJob (Burst-compiled IJob) — job that dequeues RemoveData and applies removals via an EntityCommandBuffer.
- TypeHandle (struct) — helper used to assign and hold various type handles/lookups for the jobs.
Properties
- This system does not expose public properties.
Constructors
public PropertyRenterRemoveSystem()
Default constructor. The system relies on OnCreate to initialize its queries, archetypes, and cached systems.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 256. The system is configured to run only at intervals derived from the simulation frame with this base interval. -
[Preserve] protected override void OnCreate()
Initializes the system: caches SimulationSystem and EndFrameBarrier references, sets up the EntityQuery (m_RenterGroup) to require PropertyRenter+UpdateFrame, creates the RentersUpdated event archetype, and calls RequireForUpdate(m_RenterGroup) so the system only runs when relevant entities exist. -
[Preserve] protected override void OnUpdate()
Core scheduling logic: - Computes an update frame index with SimulationUtils.GetUpdateFrameWithInterval (uses the system interval and a sub-interval of 16).
- Allocates a NativeQueue
(temp job) to accumulate removals. - Builds and schedules UpdateRentersJob over m_RenterGroup in parallel. That job:
- Skips chunks whose UpdateFrame shared component does not match the computed frame index.
- For each PropertyRenter entity it checks:
- If the referenced building entity no longer has a Building component -> enqueue a RemoveData with only m_Renter set (property == Entity.Null).
- Else, finds the building's prefab, reads BuildingPropertyData and the Renter buffer; if the buffer length exceeds CountProperties() from the prefab data, it enqueues a RemoveData containing both m_Property and m_Renter.
- Constructs RemoveRentersJob to run after the chunk job. That job:
- Dequeues RemoveData entries and either:
- If m_Property != Entity.Null: remove the matching entry from the renters buffer, RemoveComponent
on the renter entity, and emit a RentersUpdated event entity (once per property using a temporary NativeHashSet to avoid duplicate events). - If m_Property == Entity.Null: log (Debug.Log) and remove the PropertyRenter component from the renter entity.
- Uses an EntityCommandBuffer obtained from m_EndFrameBarrier so structural changes are deferred and safe.
-
Disposes the NativeQueue as a dependency and registers the producer job handle with m_EndFrameBarrier.
-
protected override void OnCreateForCompiler()
Internal hookup for generated code paths: assigns queries and type handles for the compiler/runtime. -
private void __AssignQueries(ref SystemState state)
Internal helper used at compile-time to prepare any EntityQueryBuilders. In this generated code it currently just creates and disposes an empty builder.
Notes on threading and safety:
- UpdateRentersJob is Burst-compiled and scheduled in parallel across chunks. It uses ReadOnly lookups for most data and writes into a NativeQueue
Usage Example
// The system runs automatically if it is present in the world and there are PropertyRenter entities.
// Example: destroy a building to cause associated PropertyRenter components to be removed by this system.
EntityManager.DestroyEntity(buildingEntity); // after this, the system will detect missing Building and remove PropertyRenter components from renters, emitting RentersUpdated events
// Alternatively, from inside a system you can observe RentersUpdated events:
var query = SystemAPI.QueryBuilder().WithAll<Game.Common.Event, RentersUpdated>().Build();
// read event entities in a subsequent system to react to renter list changes.
Additional notes: - The system logs a message when it finds a renter whose referenced building entity no longer exists. - It relies on prefab BuildingPropertyData to determine allowed property slots (CountProperties()). - Because it uses EndFrameBarrier and job scheduling, modifications are deferred and batched for safety and performance.