CarKeeperSystem
Assembly:
Game (inferred)
Namespace:
Game.Serialization
Type:
class
Base:
GameSystemBase
Summary:
CarKeeperSystem is an ECS system that keeps CarKeeper components synchronized with PersonalCar entities. It schedules two Burst-compiled chunk jobs:
- CarKeeperJob: iterates over entities with PersonalCar and sets the corresponding CarKeeper.m_Car to the PersonalCar entity referenced by PersonalCar.m_Keeper.
- NoCarJob: iterates over CarKeeper components and disables the CarKeeper component (component enabled flag) when the referenced CarKeeper.m_Car no longer has a PersonalCar component.
The system uses EntityQuery to require PersonalCar entities for update and uses ComponentTypeHandle / ComponentLookup to access components in jobs. Jobs are scheduled with Unity's JobChunk API and Burst-compiled for performance.
Fields
-
private Unity.Entities.EntityQuery m_Query
This query selects entities that have the PersonalCar component. It is used to drive the CarKeeperJob and also registered via RequireForUpdate to ensure the system only runs when PersonalCar entities exist. -
private Unity.Entities.EntityQuery m_KeeperQuery
This query selects entities that have the CarKeeper component. It is used to drive the NoCarJob which validates and disables CarKeeper components when their referenced car is gone. -
private CarKeeperSystem.TypeHandle __TypeHandle
Holds precalculated entity/component type handles and ComponentLookup instances that are assigned in OnCreateForCompiler. These handles are used to obtain the EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle , and ComponentLookup instances for use inside scheduled jobs.
Properties
- This system exposes no public properties.
Constructors
public CarKeeperSystem()
Default parameterless constructor. The system relies on the ECS lifecycle (OnCreate, OnUpdate) for initialization.
Methods
[Preserve] protected override void OnCreate()
Initializes the system queries:- Creates m_Query for PersonalCar (read-only).
- Creates m_KeeperQuery for CarKeeper (read-only).
-
Calls RequireForUpdate(m_Query) so the system only runs when PersonalCar entities exist.
-
[Preserve] protected override void OnUpdate()
Schedules the two jobs: - Schedules CarKeeperJob over m_Query with read-only access to PersonalCar and a ComponentLookup
to update CarKeeper.m_Car to the entity that owns the PersonalCar. - CarKeeperJob: For each chunk with PersonalCar, iterates entries and if the referenced CarKeeper component is available/enabled, writes the car Entity into CarKeeper.m_Car.
-
Schedules NoCarJob over m_KeeperQuery after the first job completes. NoCarJob reads CarKeeper components and a ComponentLookup
and disables the CarKeeper component (via chunk.SetComponentEnabled) for entries whose referenced CarKeeper.m_Car no longer has a PersonalCar component. The method assembles the job data using InternalCompilerInterface helpers to get the proper type handles/lookups from __TypeHandle and base.CheckedStateRef, and then sets base.Dependency appropriately. -
private void __AssignQueries(ref SystemState state)
Compiler helper stub that currently creates and disposes an EntityQueryBuilder(Allocator.Temp). Present to satisfy generated-code needs; actual query assignment is handled in OnCreate and OnCreateForCompiler. -
protected override void OnCreateForCompiler()
Called by generated-code flow to initialize query assignments and to call __TypeHandle.__AssignHandles to populate the TypeHandle with EntityTypeHandle, ComponentTypeHandle and ComponentLookup instances derived from the system's SystemState (base.CheckedStateRef). -
Nested IJobChunk structs (BurstCompile):
- CarKeeperJob : IJobChunk
- Fields: EntityTypeHandle m_EntityType (read-only), ComponentTypeHandle
m_PersonalCarType (read-only), ComponentLookup m_CarKeeperData (read-write). - Execute: For each PersonalCar in the chunk, obtains the corresponding CarKeeper component (via m_PersonalCar.m_Keeper). If the CarKeeper is present/enabled, sets CarKeeper.m_Car = entity and writes the component back via the ComponentLookup indexer.
- Fields: EntityTypeHandle m_EntityType (read-only), ComponentTypeHandle
- NoCarJob : IJobChunk
- Fields: EntityTypeHandle m_EntityType (read-only), ComponentTypeHandle
m_CarKeeperType (read-write), ComponentLookup m_PersonalCars (read-only). - Execute: For each CarKeeper in the chunk, if the CarKeeper component is enabled but the referenced CarKeeper.m_Car does not have a PersonalCar component (m_PersonalCars.HasComponent returns false), it disables the CarKeeper component for that chunk element using chunk.SetComponentEnabled.
- Fields: EntityTypeHandle m_EntityType (read-only), ComponentTypeHandle
Notes on behavior and correctness: - The system expects PersonalCar to contain a handle/reference to a CarKeeper (PersonalCar.m_Keeper). CarKeeper components store an Entity reference (m_Car) that should point back to the car entity. - The CarKeeperJob only writes CarKeeper components when TryGetEnabledComponent returns a valid enabled component reference; it then writes back via the ComponentLookup indexer. - The NoCarJob uses chunk.SetComponentEnabled to turn off CarKeeper when the car no longer exists as a PersonalCar; this avoids stale enabled CarKeeper components referencing missing cars. - Jobs are Burst-compiled and scheduled using JobChunkExtensions.Schedule for chunked, cache-friendly processing.
Usage Example
// The system runs automatically as part of the ECS world. Conceptually:
// - When a PersonalCar entity exists and references a CarKeeper (via PersonalCar.m_Keeper),
// CarKeeperSystem will assign the CarKeeper.m_Car to the PersonalCar entity.
// - If a CarKeeper references an Entity that no longer has a PersonalCar component,
// the CarKeeper component will be disabled to avoid a stale reference.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// This system already calls RequireForUpdate for PersonalCar in its OnCreate implementation,
// so no additional setup is required for normal usage.
}
// No manual calls are necessary; the system updates every frame when PersonalCar entities exist.
If you need example code for creating PersonalCar and CarKeeper components/entities to observe the system behavior, tell me which component fields you want included and I can provide a small spawn/authoring snippet.