Skip to content

Game.PostVanSelectData

Assembly:
Game

Namespace:
Game.Prefabs

Type:
struct

Base:
System.ValueType

Summary:
A helper/utility struct used by city systems to enumerate, filter and instantiate post-van prefabs (postal delivery vehicles). It collects prefab archetype chunks that contain PostVanData and related components, evaluates selection requirements via VehicleSelectRequirementData, picks an appropriate vehicle prefab based on mail capacity and a simple probability mechanic, and provides methods to create vehicle entities (both from main thread and from jobs via ParallelWriter). It is meant to be used from a SystemBase and participates in the Unity ECS job workflow (PreUpdate -> job handle -> PostUpdate).


Fields

  • private Unity.Collections.NativeList<Unity.Entities.ArchetypeChunk> m_PrefabChunks
    Holds the list of archetype chunks that match the entity query for post van prefabs. Allocated in PreUpdate and must be disposed in PostUpdate (disposing is done via m_PrefabChunks.Dispose(jobHandle)).

  • private VehicleSelectRequirementData m_RequirementData
    Encapsulates requirement-checking logic used to filter prefabs (e.g., city configuration, unlocked features, other requirements). Updated in PreUpdate.

  • private Unity.Entities.EntityTypeHandle m_EntityType
    EntityTypeHandle used to read Entity arrays from archetype chunks. Updated in PreUpdate.

  • private Unity.Entities.ComponentTypeHandle<PostVanData> m_PostVanType
    ComponentTypeHandle for PostVanData (read-only) used to read per-prefab PostVanData from chunks.

  • private Unity.Entities.ComponentLookup<ObjectData> m_ObjectData
    ComponentLookup for ObjectData (read-only) used to retrieve object archetypes for creating entities.

  • private Unity.Entities.ComponentLookup<MovingObjectData> m_MovingObjectData
    ComponentLookup for MovingObjectData (read-only) used to select stopped/parked archetypes when spawning parked vehicles.

Properties

  • None (this struct exposes no public properties).
    The struct exposes methods for lifecycle (PreUpdate/PostUpdate), selection and creation, but no public CLR properties.

Constructors

  • public PostVanSelectData(SystemBase system)
    Initializes internal handles and the VehicleSelectRequirementData using the provided SystemBase. Does not allocate the prefab chunk list — m_PrefabChunks remains default until PreUpdate is called.

Methods

  • public static Unity.Entities.EntityQueryDesc GetEntityQueryDesc()
    Returns an EntityQueryDesc describing which entity prefabs are considered for post vans. The query requires PostVanData, CarData, ObjectData and PrefabData and excludes Locked components.

  • public void PreUpdate(SystemBase system, CityConfigurationSystem cityConfigurationSystem, Unity.Entities.EntityQuery query, Unity.Collections.Allocator allocator, out Unity.Jobs.JobHandle jobHandle)
    Asynchronously gathers archetype chunks matching the given query into m_PrefabChunks (ToArchetypeChunkListAsync) and updates internal component handles and the requirement data. Returns the jobHandle representing the async chunk gathering operation. Must be called before SelectVehicle / CreateVehicle usage that relies on m_PrefabChunks.

  • public void PostUpdate(Unity.Jobs.JobHandle jobHandle)
    Disposes the m_PrefabChunks list, scheduling disposal with the provided jobHandle. Must be called after the job(s) that used the prefab chunks have completed (paired with PreUpdate).

  • public Unity.Entities.Entity CreateVehicle(Unity.Entities.EntityCommandBuffer commandBuffer, ref Unity.Mathematics.Random random, Transform transform, Unity.Entities.Entity source, Unity.Entities.Entity vehiclePrefab, bool parked)
    Creates and configures an entity instance of the provided vehiclePrefab using an EntityCommandBuffer (main-thread writer). Sets Transform, PrefabRef, PseudoRandomSeed and, if not parked, TripSource and Unspawned components. Returns Entity.Null if vehiclePrefab is Entity.Null.

  • public Unity.Entities.Entity CreateVehicle(Unity.Entities.EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Unity.Mathematics.Random random, Transform transform, Unity.Entities.Entity source, Unity.Entities.Entity vehiclePrefab, bool parked)
    Same as above but uses an EntityCommandBuffer.ParallelWriter for use inside jobs. Accepts jobIndex for parallel writer calls.

  • public Unity.Entities.Entity SelectVehicle(ref Unity.Mathematics.Random random, ref Unity.Mathematics.int2 mailCapacity)
    Iterates the cached prefab chunks and selects a vehicle prefab Entity that satisfies the requirements and best matches the requested mail capacity. Uses VehicleSelectRequirementData to filter prefabs and a simple weighted/random pick (via PickVehicle) to choose between matching candidates. Updates mailCapacity to the chosen vehicle's mail capacity and returns Entity.Null if none matched.

  • private Unity.Entities.EntityArchetype GetArchetype(Unity.Entities.Entity prefab, bool parked)
    Returns the archetype to use when creating the instance of the given prefab. If parked is true, uses m_MovingObjectData[prefab].m_StoppedArchetype; otherwise uses m_ObjectData[prefab].m_Archetype.

  • private bool PickVehicle(ref Unity.Mathematics.Random random, int probability, ref int totalProbability)
    A helper that accumulates total probability and uses NextInt(totalProbability) to decide whether the current candidate is selected. Implements reservoir-like selection with weights (here passed as uniform "probability" per candidate in caller).

Usage Example

// Example usage inside a SystemBase

private PostVanSelectData m_PostVanSelect;
private EntityQuery m_PostVanQuery;

protected override void OnCreate()
{
    base.OnCreate();
    m_PostVanSelect = new PostVanSelectData(this);

    // Create an EntityQuery using the descriptor from the helper
    var desc = PostVanSelectData.GetEntityQueryDesc();
    m_PostVanQuery = GetEntityQuery(desc);
}

protected override void OnUpdate()
{
    // Example: prepare selection data before running logic that uses prefab chunks
    JobHandle gatherHandle;
    m_PostVanSelect.PreUpdate(this, World.GetExistingSystem<CityConfigurationSystem>(), m_PostVanQuery, Allocator.TempJob, out gatherHandle);

    // Ensure any jobs that use m_PrefabChunks are scheduled after gatherHandle.
    // For simple single-threaded usage we can complete it immediately, then call SelectVehicle/create methods.
    gatherHandle.Complete();

    // Example selection
    Unity.Mathematics.Random rnd = new Unity.Mathematics.Random(12345);
    Unity.Mathematics.int2 requestedMailCapacity = new Unity.Mathematics.int2(0, 0);
    Entity chosenPrefab = m_PostVanSelect.SelectVehicle(ref rnd, ref requestedMailCapacity);
    if (chosenPrefab != Entity.Null)
    {
        var ecb = new EntityCommandBuffer(Unity.Collections.Allocator.Temp);
        Transform spawnTransform = new Transform { /* ... */ };
        m_PostVanSelect.CreateVehicle(ecb, ref rnd, spawnTransform, sourceEntity, chosenPrefab, parked: false);
        ecb.Playback(EntityManager);
        ecb.Dispose();
    }

    // Dispose collected chunk list (safely schedule disposal if other jobs were used)
    m_PostVanSelect.PostUpdate(JobHandle.Completed);
}

Notes and tips: - Always call PreUpdate before using SelectVehicle or creating vehicles that depend on the cached m_PrefabChunks, and always pair it with PostUpdate to dispose resources. - PreUpdate updates component handles (EntityTypeHandle, ComponentTypeHandle, ComponentLookup) — if called from a different system tick you must ensure these are updated for the correct SystemBase. - SelectVehicle mutates the provided mailCapacity (sets it to the chosen vehicle's capacity). The selection logic uses requirement checks (VehicleSelectRequirementData) and a simple probability-based pick; be mindful of how probabilities are accumulated if modifying the selection logic.