Game.Prefabs.HealthcareVehicleSelectData
Assembly:
Assembly-CSharp (game assembly)
Namespace:
Game.Prefabs
Type:
struct
Base:
System.ValueType
Summary:
Utility/value-type used by systems to select and spawn healthcare-related vehicles (ambulances, hearses, and helicopter variants) from available vehicle prefabs. It collects prefab archetype chunks from an EntityQuery, applies selection requirements (via VehicleSelectRequirementData), supports jobified async chunk retrieval (PreUpdate/PostUpdate), and provides methods to create vehicle entities (parked or active) with appropriate components (PrefabRef, PseudoRandomSeed, TripSource, Unspawned). Selection supports road-type filtering (car vs helicopter) and probabilistic picking.
Fields
-
private NativeList<ArchetypeChunk> m_PrefabChunks
Holds the list of archetype chunks obtained from an EntityQuery (populated in PreUpdate). This list is disposed in PostUpdate and is used when iterating candidate prefabs for selection. -
private VehicleSelectRequirementData m_RequirementData
Encapsulates requirement checks used to filter which vehicle prefabs are eligible for selection (e.g., game configuration, enabled/disabled prefabs, or other custom constraints). Updated each PreUpdate. -
private EntityTypeHandle m_EntityType
Handle used to read Entity values from archetype chunks. -
private ComponentTypeHandle<AmbulanceData> m_AmbulanceType
Component type handle used to test whether a chunk contains ambulance-capable prefabs. -
private ComponentTypeHandle<HearseData> m_HearseType
Component type handle used to test whether a chunk contains hearse-capable prefabs. -
private ComponentTypeHandle<CarData> m_CarType
Component type handle used to test whether a chunk contains car-capable prefabs (road vehicles). -
private ComponentTypeHandle<HelicopterData> m_HelicopterType
Component type handle used to test whether a chunk contains helicopter-capable prefabs. -
private ComponentLookup<ObjectData> m_ObjectData
Lookup for ObjectData components on prefab entities (used to get the runtime archetype for unparked vehicles). -
private ComponentLookup<MovingObjectData> m_MovingObjectData
Lookup for MovingObjectData components on prefabs (used to retrieve stopped/parked archetype).
Properties
None
This struct does not expose public properties.
Constructors
public HealthcareVehicleSelectData(SystemBase system)
Initializes internal handles and requirement helper using the provided SystemBase. Prepares ComponentTypeHandle/ComponentLookup and EntityTypeHandle with read-only flags where appropriate. Note: this does not populate m_PrefabChunks — call PreUpdate to collect chunks.
Methods
public static EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that identifies vehicle prefab entities relevant to healthcare selection:- All: VehicleData, ObjectData, PrefabData
- Any: AmbulanceData, HearseData
-
None: Locked This query is suitable for passing to Entities.WithStoreEntityQueryInField(...)/system queries used by PreUpdate to collect prefab chunks.
-
public void PreUpdate(SystemBase system, CityConfigurationSystem cityConfigurationSystem, EntityQuery query, Allocator allocator, out JobHandle jobHandle)
Begins async retrieval of the query's archetype chunks into m_PrefabChunks (via ToArchetypeChunkListAsync), returning a JobHandle that must be completed before accessing the list. Also updates m_RequirementData and all ComponentTypeHandle/ComponentLookup/EntityTypeHandle with the current system state. -
public void PostUpdate(JobHandle jobHandle)
Dispose/cleans up m_PrefabChunks using the provided JobHandle (ensures disposal happens after any jobs that used the chunks complete). -
public Entity SelectVehicle(ref Random random, HealthcareRequestType healthcareType, RoadTypes roadType)
Selects and returns a random eligible prefab Entity matching the requested healthcare type (Ambulance or Hearse) and road type (Car or Helicopter). Uses internal GetRandomVehicle which iterates m_PrefabChunks, filters chunks and entries by components and requirement checks, and picks via weighted/probabilistic selection. Returns Entity.Null if no suitable prefab found. -
public Entity CreateVehicle(EntityCommandBuffer commandBuffer, ref Random random, Transform transform, Entity source, Entity prefab, HealthcareRequestType healthcareType, RoadTypes roadType, bool parked)
Create and initialize a vehicle entity using a non-parallel EntityCommandBuffer. If prefab==Entity.Null the method will pick a random suitable prefab. Adds: - the provided Transform component,
- PrefabRef(prefab),
- PseudoRandomSeed(ref random),
-
if not parked: TripSource(source) and Unspawned marker. Returns the created Entity or Entity.Null if no prefab available.
-
public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, Transform transform, Entity source, Entity prefab, HealthcareRequestType healthcareType, RoadTypes roadType, bool parked)
Same as the previous CreateVehicle overload but for use inside a job with an EntityCommandBuffer.ParallelWriter; uses jobIndex for writing commands. -
private EntityArchetype GetArchetype(Entity prefab, bool parked)
Return the appropriate EntityArchetype to use when creating an instance of the prefab: - if parked: returns m_MovingObjectData[prefab].m_StoppedArchetype
-
else: returns m_ObjectData[prefab].m_Archetype
-
private Entity GetRandomVehicle(ref Random random, HealthcareRequestType healthcareType, RoadTypes roadType)
Iterates the cached m_PrefabChunks, filters chunks by the targeted healthcare type and road type using component presence checks, then iterates entities inside each chunk applying requirement checks (via m_RequirementData.GetChunk/CheckRequirements). Selection uses a running probabilistic accumulator (PickVehicle) to randomly pick among eligible prefabs. Returns Entity.Null if no match. -
private bool PickVehicle(ref Random random, int probability, ref int totalProbability)
Helper for performing reservoir-style probabilistic selection: increments totalProbability by probability and returns true if random.NextInt(totalProbability) < probability. Used to choose uniformly among candidates when all candidates have equal weight (probability parameter).
Usage Example
// Example usage outline inside a SystemBase
public partial class MyHealthcareSystem : SystemBase
{
private HealthcareVehicleSelectData m_VehicleSelector;
private EntityQuery m_PrefabQuery;
protected override void OnCreate()
{
m_VehicleSelector = new HealthcareVehicleSelectData(this);
m_PrefabQuery = GetEntityQuery(HealthcareVehicleSelectData.GetEntityQueryDesc());
}
protected override void OnUpdate()
{
// Begin async chunk collection and update requirement handles
JobHandle prefabHandle;
m_VehicleSelector.PreUpdate(this, World.GetExistingSystem<CityConfigurationSystem>(), m_PrefabQuery, Allocator.TempJob, out prefabHandle);
// Ensure the chunks are available before selecting/creating vehicles
prefabHandle.Complete();
// Example: pick a vehicle prefab and create an active ambulance on car roads
Unity.Mathematics.Random rnd = new Unity.Mathematics.Random(12345);
Entity prefab = m_VehicleSelector.SelectVehicle(ref rnd, HealthcareRequestType.Ambulance, RoadTypes.Car);
if (prefab != Entity.Null)
{
Transform spawnTransform = new Transform { /* set position/rotation/scale */ };
Entity source = /* some source entity */;
m_VehicleSelector.CreateVehicle(CommandBuffer, ref rnd, spawnTransform, source, prefab, HealthcareRequestType.Ambulance, RoadTypes.Car, parked: false);
}
// Dispose chunk list once done
m_VehicleSelector.PostUpdate(prefabHandle);
}
}
Notes and tips: - Always call PreUpdate before accessing selection methods and call PostUpdate afterwards to dispose the internal NativeList. - PreUpdate returns an async JobHandle; you may either complete it immediately (as shown) or schedule follow-up jobs that depend on it. Do not access m_PrefabChunks until the returned JobHandle is complete. - When creating vehicles, pass prefab==Entity.Null to let the selector pick a suitable prefab automatically. - The selector accounts for parked vs active creation by choosing the appropriate archetype from ObjectData or MovingObjectData.