Game.FireEngineSelectData
Assembly: Game (Assembly-CSharp)
Namespace: Game.Prefabs
Type: struct
Base: System.ValueType
Summary:
Helper struct used by systems to select and create fire-engine vehicle prefabs (cars or helicopters) that satisfy configured requirements. It queries prefab chunks, evaluates VehicleSelectRequirementData, performs weighted/random selection based on extinguishing capacity, and creates entities with the correct archetype and initial components. Manages a NativeList of archetype chunks retrieved asynchronously and must be PreUpdate/PostUpdate'd to be job-safe.
Fields
-
private NativeList<ArchetypeChunk> m_PrefabChunks
Holds the matching prefab archetype chunks produced by the entity query in PreUpdate. Allocated with a specified Allocator and disposed in PostUpdate. -
private VehicleSelectRequirementData m_RequirementData
Helper used to check per-prefab requirements (e.g. tech, game configuration). Updated each PreUpdate. -
private EntityTypeHandle m_EntityType
Chunk handle used to extract Entity arrays from each chunk. -
private ComponentTypeHandle<FireEngineData> m_FireEngineType
Read-only component type handle for FireEngineData used during selection. -
private ComponentTypeHandle<CarData> m_CarType
Read-only component type handle used to identify car prefabs. -
private ComponentTypeHandle<HelicopterData> m_HelicopterType
Read-only component type handle used to identify helicopter prefabs. -
private ComponentLookup<ObjectData> m_ObjectData
Lookup to retrieve ObjectData for a prefab entity (used to get the runtime archetype when spawning non-parked objects). -
private ComponentLookup<MovingObjectData> m_MovingObjectData
Lookup to retrieve MovingObjectData for a prefab entity (used to get the stopped archetype when spawning parked vehicles).
Properties
- None (this struct exposes no public properties)
Constructors
public FireEngineSelectData(SystemBase system)
Initializes internal handles and lookups with the provided SystemBase. Initializes m_PrefabChunks to default and constructs VehicleSelectRequirementData. After construction, the component/lookup handles must be kept up-to-date by calling PreUpdate before use.
Methods
-
public static EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that targets prefab entities with FireEngineData, ObjectData and PrefabData, requires either CarData or HelicopterData, and excludes Locked. Use this query to obtain the prefabs to pass into PreUpdate. -
public void PreUpdate(SystemBase system, CityConfigurationSystem cityConfigurationSystem, EntityQuery query, Allocator allocator, out JobHandle jobHandle)
Asynchronously builds m_PrefabChunks from the provided query (ToArchetypeChunkListAsync) and outputs the associated JobHandle. Updates the VehicleSelectRequirementData and all component handles/lookups for the current SystemBase context. Must be called before any selection/creation calls for the frame. -
public void PostUpdate(JobHandle jobHandle)
Disposes the m_PrefabChunks NativeList once the given jobHandle has completed. Must be called after PreUpdate (and after job dependencies are satisfied) to avoid leaks. -
public Entity SelectVehicle(ref Random random, ref float2 extinguishingCapacity, RoadTypes roadType)
Selects and returns an Entity prefab matching the requested RoadTypes and extinguishing capacity. Uses internal GetRandomVehicle logic and updates the extinguishingCapacity to the selected prefab's extinguishing capacity (or leaves unchanged if none found). Returns Entity.Null if no suitable prefab exists. -
public Entity CreateVehicle(EntityCommandBuffer commandBuffer, ref Random random, Transform transform, Entity source, Entity prefab, ref float2 extinguishingCapacity, RoadTypes roadType, bool parked)
Creates an instance entity from the given prefab (or selects one if prefab == Entity.Null) using the provided EntityCommandBuffer. Sets Transform, PrefabRef, and PseudoRandomSeed. If not parked, adds TripSource and Unspawned. Returns the created entity or Entity.Null if no prefab could be selected. -
public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, Transform transform, Entity source, Entity prefab, ref float2 extinguishingCapacity, RoadTypes roadType, bool parked)
Parallel/writer variant of CreateVehicle for use inside Jobs. Same behavior as the non-parallel CreateVehicle but uses jobIndex for parallel command buffer operations. -
private EntityArchetype GetArchetype(Entity prefab, bool parked)
Returns the correct EntityArchetype for the spawned instance: when parked, returns the prefab's stopped archetype from MovingObjectData; otherwise returns the regular archetype from ObjectData. -
private Entity GetRandomVehicle(ref Random random, ref float2 extinguishingCapacity, RoadTypes roadType)
Core selection algorithm. Iterates all prefab chunks, filters by roadType (car vs helicopter), checks per-item requirements via VehicleSelectRequirementData, evaluates difference between prefab extinguishing capacity and requested extinguishingCapacity, and uses an incremental weighted/random pick (via PickVehicle) to choose one prefab. Updates extinguishingCapacity to the selected prefab's capacity. Returns Entity.Null if none match. -
private bool PickVehicle(ref Random random, int probability, ref int totalProbability)
Small helper implementing reservoir-like incremental weighted pick: increments totalProbability by probability and returns true if random.NextInt(totalProbability) < probability. This yields a uniform selection across entries given equal probability values.
Usage Example
// In your SystemBase:
private FireEngineSelectData m_FireEngineSelect;
// OnCreate / Init:
protected override void OnCreate()
{
base.OnCreate();
m_FireEngineSelect = new FireEngineSelectData(this);
}
// In OnUpdate:
protected override void OnUpdate()
{
// Prepare the prefab query (use GetEntityQueryDesc)
var queryDesc = FireEngineSelectData.GetEntityQueryDesc();
EntityQuery prefabQuery = GetEntityQuery(queryDesc);
// PreUpdate: build chunk list asynchronously and update lookups
JobHandle prefabJob;
m_FireEngineSelect.PreUpdate(this, CityConfigurationSystem.Instance, prefabQuery, Allocator.TempJob, out prefabJob);
// Ensure dependency (if you run jobs that depend on prefabJob, chain them).
// Use selection/creation (example: select and spawn a vehicle immediately):
Unity.Mathematics.Random rnd = new Unity.Mathematics.Random(12345);
float2 reqCapacity = new float2(0, 0);
Entity chosenPrefab = m_FireEngineSelect.SelectVehicle(ref rnd, ref reqCapacity, RoadTypes.Car);
if (chosenPrefab != Entity.Null)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
m_FireEngineSelect.CreateVehicle(ecb, ref rnd, new Transform(), sourceEntity, chosenPrefab, ref reqCapacity, RoadTypes.Car, parked: false);
ecb.Playback(EntityManager);
ecb.Dispose();
}
// PostUpdate: dispose chunk list once prefabJob is complete
m_FireEngineSelect.PostUpdate(prefabJob);
}
// OnDestroy: ensure any remaining resources are cleaned up if needed (typically PostUpdate handles disposal).
Notes and caveats: - Always call PreUpdate before using the selection/creation methods each frame and call PostUpdate with the corresponding job handle afterwards to prevent memory leaks. - Random is Unity.Mathematics.Random and should be passed by ref to preserve its state. - Selection respects VehicleSelectRequirementData; configure CityConfigurationSystem/requirements accordingly. - The selection algorithm adjusts and returns extinguishingCapacity based on the chosen prefab.