Game.Prefabs.PersonalCarSelectData
Assembly: Game
Namespace: Game.Prefabs
Type: struct
Base: System.ValueType
Summary:
Utility/selector used by vehicle-spawning systems to pick and instantiate personal car (and trailer/tractor) prefabs based on passenger/baggage requirements, probabilistic weights, and selection rules (e.g., avoid trailers, exclude slow vehicles). Maintains a list of prefab archetype chunks and component type handles required to evaluate prefab data and create runtime entities. Designed to be used from a SystemBase: call PreUpdate to gather chunks and update handles, use CreateVehicle/CreateTrailer to spawn entities, and call PostUpdate to dispose temporary native lists.
Fields
-
private NativeList<ArchetypeChunk> m_PrefabChunks
Holds the list of prefab archetype chunks returned by an EntityQuery. Populated by PreUpdate (via ToArchetypeChunkListAsync) and disposed in PostUpdate. -
private VehicleSelectRequirementData m_RequirementData
Helper that contains requirement checks/filters per chunk (e.g., DLC/availability/other mod/game restrictions). Updated in PreUpdate and consulted when iterating prefabs. -
private EntityTypeHandle m_EntityType
Entity type handle used to read Entity values from prefab chunks. Updated in PreUpdate. -
private ComponentTypeHandle<Game.Prefabs.CarData> m_CarDataType
Component type handle for the CarData prefab component (read-only). Updated in PreUpdate and used when evaluating max speed and other car-level fields. -
private ComponentTypeHandle<PersonalCarData> m_PersonalCarDataType
Component type handle for PersonalCarData (read-only). Contains passenger/baggage capacity and probability weight used for selection. -
private ComponentTypeHandle<CarTrailerData> m_CarTrailerDataType
Component type handle for trailer-specific data (read-only). Used when checking trailer compatibility and when selecting trailer prefabs. -
private ComponentTypeHandle<CarTractorData> m_CarTractorDataType
Component type handle for tractor-specific data (read-only). Used when selecting tractors or matching fixed trailers. -
private ComponentTypeHandle<ObjectData> m_ObjectDataType
Component type handle for ObjectData (read-only). Used to get archetype for creating runtime entity. -
private ComponentTypeHandle<MovingObjectData> m_MovingObjectDataType
Component type handle for MovingObjectData (read-only). Used to get stopped archetype when creating stopped variants.
Properties
- (none)
Constructors
public PersonalCarSelectData(SystemBase system)
Creates and initializes the selector for use in the provided system. Initializes the VehicleSelectRequirementData and obtains component/entity type handles (set as read-only). After construction, call PreUpdate before iterating prefabs.
Methods
-
public static EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc configured to match personal car prefabs: requires PersonalCarData, Game.Prefabs.CarData, MovingObjectData, PrefabData, and excludes Locked. Use this query to fetch the prefabs to pass into PreUpdate. -
public void PreUpdate(SystemBase system, CityConfigurationSystem cityConfigurationSystem, EntityQuery query, Allocator allocator, out JobHandle jobHandle)
Asynchronously converts the provided query to an ArchetypeChunk list (m_PrefabChunks) with the given allocator and produces a JobHandle for that async operation. Also updates all stored type handles and the requirement helper with the current system state. Must be called before any Create or Check methods that iterate m_PrefabChunks. -
public void PostUpdate(JobHandle jobHandle)
Disposes the m_PrefabChunks NativeList, taking the JobHandle produced by PreUpdate to ensure safe disposal after any pending jobs complete. -
public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, int passengerAmount, int baggageAmount, bool avoidTrailers, bool noSlowVehicles, Transform transform, Entity source, Entity keeper, PersonalCarFlags state, bool stopped, uint delay = 0u)
Overload that spawns a personal vehicle (and optional trailer) using a parallel ECB writer. Internally calls the overload which returns trailer/vehicle prefab information. Returns the main vehicle entity or Entity.Null on failure. -
public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, int passengerAmount, int baggageAmount, bool avoidTrailers, bool noSlowVehicles, Transform transform, Entity source, Entity keeper, PersonalCarFlags state, bool stopped, uint delay, out Entity trailer, out Entity vehiclePrefab, out Entity trailerPrefab)
Primary parallel spawn method: selects a matching prefab combination via GetVehicleData, creates the main vehicle entity, and if a trailer is selected, creates it, sets up Controller relationships and adds LayoutElement buffer entries. Outputs the created trailer entity and the prefab entities selected. -
public Entity CreateVehicle(EntityCommandBuffer commandBuffer, ref Random random, int passengerAmount, int baggageAmount, bool avoidTrailers, bool noSlowVehicles, Transform transform, Entity source, Entity keeper, PersonalCarFlags state, bool stopped, uint delay = 0u)
Non-parallel ECB overload. Same selection and creation semantics as the parallel version, but uses the non-parallel EntityCommandBuffer. -
public Entity CreateTrailer(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, int passengerAmount, int baggageAmount, bool noSlowVehicles, Entity tractorPrefab, Transform tractorTransform, PersonalCarFlags state, bool stopped, uint delay = 0u)
Given a tractor prefab and transform, searches available prefabs for a compatible trailer (matching trailer/tractor types and fixed-pair constraints) and spawns the appropriate trailer entity attached to the tractor. Returns created trailer entity or Entity.Null. -
private bool GetVehicleData(ref Random random, int passengerAmount, int baggageAmount, bool avoidTrailers, bool noSlowVehicles, out CarData bestFirst, out CarData bestSecond)
Iterates all prefab chunks and evaluates each prefab (and trailer/tractor combinations) against requirements, no-slow filter, capacity, and probabilistic weighting to select the best matching main vehicle (bestFirst) and optional secondary (bestSecond, e.g., trailer). Returns true when a main vehicle was selected. -
private Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, CarData data, Transform transform, Entity source, Entity keeper, PersonalCarFlags state, bool stopped, uint delay)
Low-level parallel creation of a single vehicle entity using the provided CarData (prefab reference). Chooses stopped archetype if requested, sets Transform, PersonalCar, PrefabRef, PseudoRandomSeed and TripSource/Unspawned if source is provided. -
private Entity CreateVehicle(EntityCommandBuffer commandBuffer, ref Random random, CarData data, Transform transform, Entity source, Entity keeper, PersonalCarFlags state, bool stopped, uint delay)
Low-level non-parallel creation variant. Same behavior as the parallel version but using the non-parallel command buffer. -
private void CheckTrailers(int passengerAmount, int baggageAmount, int extraOffset, CarData firstData, bool emptyOnly, bool noSlowVehicles, ref Random random, ref CarData bestFirst, ref CarData bestSecond, ref int totalProbability, ref int bestOffset)
Iterates all trailer-capable prefabs and, for trailers compatible with firstData, computes probability and attempts to pick a trailer (and set the selected main/trailer pair) using PickVehicle. Supports constraints like empty-only trailers and noSlowVehicles. -
private void CheckTractors(int passengerAmount, int baggageAmount, int extraOffset, CarData secondData, bool noSlowVehicles, ref Random random, ref CarData bestFirst, ref CarData bestSecond, ref int totalProbability, ref int bestOffset)
Searches tractors compatible with a given trailer (secondData) and uses probability picking to pair tractors with trailers. -
private void CalculateProbability(int passengerAmount, int baggageAmount, CarData firstData, CarData secondData, out int probability, out int offset)
Calculates combined passenger/baggage coverage, derives an "offset" (penalty/bonus) based on under/over capacity and small-vehicle preference, and computes a base probability (from PersonalCarData), scaled further if combining two vehicles. Ensures a minimum effective probability and reduces probability if the combined capacity greatly exceeds requirements. -
private bool PickVehicle(ref Random random, int probability, int offset, ref int totalProbability, ref int bestOffset)
Weighted selection helper: if offset equals current bestOffset, treats as same-priority group and picks using totalProbability accumulation. If offset is better (higher), resets accumulation and selects the candidate. Returns true when the candidate should become the chosen best.
Usage Example
// Example: using PersonalCarSelectData inside a SystemBase
public partial class MyVehicleSpawnSystem : SystemBase
{
private PersonalCarSelectData m_SelectData;
private EntityQuery m_PrefabQuery;
protected override void OnCreate()
{
m_SelectData = new PersonalCarSelectData(this);
m_PrefabQuery = GetEntityQuery(PersonalCarSelectData.GetEntityQueryDesc());
}
protected override void OnUpdate()
{
JobHandle handle;
m_SelectData.PreUpdate(this, World.GetExistingSystem<CityConfigurationSystem>(), m_PrefabQuery, Allocator.TempJob, out handle);
// Use an ECB or ECB.ParallelWriter in a job; here we show a simple immediate use:
var ecb = new EntityCommandBuffer(Allocator.Temp);
var random = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, int.MaxValue));
var created = m_SelectData.CreateVehicle(ecb, ref random, passengerAmount: 2, baggageAmount: 1, avoidTrailers: false, noSlowVehicles: false,
transform: new Transform { m_Position = float3.zero, m_Rotation = quaternion.identity }, source: Entity.Null,
keeper: Entity.Null, state: (PersonalCarFlags)0, stopped: false);
ecb.Playback(EntityManager);
ecb.Dispose();
m_SelectData.PostUpdate(handle);
}
}
Notes: - Always call PreUpdate before any selection or creation methods to ensure chunk list and handles are up-to-date. - Always call PostUpdate with the JobHandle returned from PreUpdate to free temporary native memory. - Selection respects m_RequirementData checks and may skip prefabs based on game configuration, DLCs, or other constraints.