Skip to content

Game.DeliveryTruckSelectData

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: struct

Base: System.ValueType

Summary:
DeliveryTruckSelectData is a lightweight container and helper used to select delivery truck prefabs and spawn delivery truck entities in the ECS world. It wraps a NativeArray (expected to contain selectable truck variants, typically sorted by capacity) and provides methods to: - determine the min/max capacity available for a given resource set, - probabilistically pick a best-fit truck variant for a requested capacity and resources, and - create one or more vehicle entities (using EntityCommandBuffer.ParallelWriter) while consuming requested amounts and return amounts.

Notes: - The struct stores a NativeArray by value; it does not take ownership or dispose it. The caller is responsible for the array's lifetime. - Designed for use inside jobs/systems where EntityCommandBuffer.ParallelWriter and ComponentLookup are available. - Selection logic uses Unity.Mathematics and Unity.Collections primitives and a custom weighting heuristic based on cost, capacity and supported resources.


Fields

  • private Unity.Collections.NativeArray<DeliveryTruckSelectItem> m_Items
    Holds the list of available DeliveryTruckSelectItem entries used for selection and spawning. The selection routines assume the array is ordered in a way that allows binary-search-like behavior by capacity (the code performs a search based on capacity). The NativeArray lifetime is managed by the caller — this struct neither allocates nor disposes it.

Properties

  • None (this struct exposes no public properties)

Constructors

  • public DeliveryTruckSelectData(NativeArray<DeliveryTruckSelectItem> items)
    Initializes the DeliveryTruckSelectData with the provided NativeArray of items. The provided array is stored by value; keep it alive while using this struct.

Methods

  • public void GetCapacityRange(Resource resources, out int min, out int max)
    Scans m_Items to find the first and last entries that support the requested resource(s). Sets out parameters:
  • min: capacity of the first matching item (0 if none)
  • max: capacity of the last matching item (0 if none) Behavior: linear scan from start to find minimum, linear scan from end to find maximum. Resource matching uses bitmask semantics: (item.m_Resources & resources) == resources.

  • public bool TrySelectItem(ref Random random, Resource resources, int capacity, out DeliveryTruckSelectItem item)
    Attempts to choose a DeliveryTruckSelectItem best matching the requested resources and capacity. Algorithm summary:

  • Performs a binary-search-like step to locate items around the requested capacity.
  • Iterates forward from the found position while cost comparisons permit; weighs items that support the requested resources (weight 100) and uses a running accumulator to perform a weighted random choice using random.NextInt.
  • Iterates backward similarly to consider items below the position.
  • The cost comparison uses element-wise multiplication with math.min(capacity, new int2(itemCapacityA, itemCapacityB)) to prefer cheaper options for the relevant transported quantity. Returns true if a valid item was found (item.m_Prefab1 != Entity.Null). The random parameter is modified and must be a Unity.Mathematics.Random instance passed by reference.

  • public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, ref ComponentLookup<DeliveryTruckData> deliveryTruckDatas, ref ComponentLookup<ObjectData> objectDatas, Resource resource, Resource returnResource, ref int amount, ref int returnAmount, Transform transform, Entity source, DeliveryTruckFlags state, uint delay = 0u)
    Top-level convenience overload that:

  • Combines resource and returnResource,
  • Computes required capacity as max(amount, returnAmount),
  • Uses TrySelectItem to pick a DeliveryTruckSelectItem and then delegates to the overload that accepts a selectItem. If selection fails, returns Entity.Null. If an item is selected, amount and returnAmount are reduced by the created vehicle loads.

  • public Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, ref ComponentLookup<DeliveryTruckData> deliveryTruckDatas, ref ComponentLookup<ObjectData> objectDatas, DeliveryTruckSelectItem selectItem, Resource resource, Resource returnResource, ref int amount, ref int returnAmount, Transform transform, Entity source, DeliveryTruckFlags state, uint delay = 0u)
    Creates one main vehicle entity from selectItem.m_Prefab1 and — if additional prefabs are present (m_Prefab2, m_Prefab3, m_Prefab4) — creates additional linked vehicles:

  • Adds a DynamicBuffer on the main entity and appends LayoutElement entries for each created child vehicle.
  • Sets Controller(parent) component on child vehicles so they reference the main vehicle.
  • Child vehicles are created with state masked by DeliveryTruckFlags.Loaded (they represent load-carrying wagons/trailers). This overload updates amount and returnAmount by subtracting what each created vehicle carries.

  • private Entity CreateVehicle(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ref Random random, ref ComponentLookup<DeliveryTruckData> deliveryTruckDatas, ref ComponentLookup<ObjectData> objectDatas, Entity prefab, Resource resource, Resource returnResource, ref int amount, ref int returnAmount, Transform transform, Entity source, DeliveryTruckFlags state, uint delay)
    Lowest-level creation routine that:

  • Looks up DeliveryTruckData and ObjectData for the given prefab via the provided ComponentLookup collections.
  • Constructs a Game.Vehicles.DeliveryTruck component and fills m_State and, if applicable, m_Amount and m_Resource based on deliveryTruckData.m_CargoCapacity and the requested resource/amount (reduces amount by the allocated amount).
  • Creates an Entity using objectData.m_Archetype and sets components: Transform, DeliveryTruck, PrefabRef(prefab), PseudoRandomSeed(ref random).
  • If source != Entity.Null, adds TripSource(source, delay) and an Unspawned tag component.
  • If returnResource is supported by the vehicle, creates a ReturnLoad component with m_Amount = min(returnAmount, cargoCapacity), sets its m_Resource and reduces returnAmount, then adds it. Returns the created Entity.

Notes on parameters and side-effects: - commandBuffer is an EntityCommandBuffer.ParallelWriter and jobIndex is the parallel writer index — this method is safe to call from jobs when provided correctly. - deliveryTruckDatas and objectDatas are ComponentLookup that must be valid for reading; they are indexed by prefab Entity. - ref int amount and ref int returnAmount are modified: the method consumes amounts as it assigns cargo to created vehicles. - Transform is a struct (from Unity.Transforms) that will be copied to the created entity. - The methods assume certain component/struct types exist (DeliveryTruckSelectItem, DeliveryTruckData, ObjectData, PrefabRef, PseudoRandomSeed, TripSource, Unspawned, ReturnLoad, LayoutElement, Controller, Game.Vehicles.DeliveryTruck, etc.). - Thread-safety: creation uses EntityCommandBuffer.ParallelWriter so it is intended for use inside jobs. ComponentLookup access patterns must follow ECS rules (read/write in the job system as configured).

Usage Example

// Example: constructing and using DeliveryTruckSelectData inside a system/job
NativeArray<DeliveryTruckSelectItem> items = /* filled elsewhere, kept alive by caller */;
var selectData = new DeliveryTruckSelectData(items);

// Inside a job or system where you have:
// EntityCommandBuffer.ParallelWriter ecb;
// int jobIndex;
// ref Unity.Mathematics.Random rnd;
// ref ComponentLookup<DeliveryTruckData> deliveryTruckDatas;
// ref ComponentLookup<ObjectData> objectDatas;
// Transform spawnTransform;
// Entity sourceEntity;
// Resource resource = Resource.SomeGood;
// Resource returnResource = Resource.NoResource;
// int amount = 120, returnAmount = 0;
// DeliveryTruckFlags state = DeliveryTruckFlags.None;

Entity created = selectData.CreateVehicle(
    ecb,
    jobIndex,
    ref rnd,
    ref deliveryTruckDatas,
    ref objectDatas,
    resource,
    returnResource,
    ref amount,
    ref returnAmount,
    spawnTransform,
    sourceEntity,
    state,
    delay: 0u
);

// After usage, dispose items when its owner is done:
// items.Dispose();