Skip to content

Game.Prefabs.GarbageTruck

Assembly:
Namespace: Game.Prefabs

Type: class

Base: ComponentBase

Summary:
Prefab component that defines garbage truck behaviour and data for the ECS prefab system. The class exposes editable fields for garbage capacity and unload rate, registers the required ECS component types for the prefab and archetype, and writes initial component data to an entity when the prefab is instantiated. It is decorated with [ComponentMenu("Vehicles/", new Type[] { typeof(CarPrefab) })], so it appears under the Vehicles menu (paired with CarPrefab) in the prefab tooling.


Fields

  • public int m_GarbageCapacity = 10000
    Maximum amount of garbage the truck can carry. This value is passed into GarbageTruckData during Initialize and therefore determines the runtime capacity of the vehicle.

  • public int m_UnloadRate = 2000
    Rate at which the truck unloads garbage (value passed into GarbageTruckData). This represents how much garbage is removed per unload operation/tick as defined by the GarbageTruckData semantics.

Properties

  • None.
    This prefab class exposes only public fields (m_GarbageCapacity, m_UnloadRate) and overrides methods from ComponentBase; it does not declare properties.

Constructors

  • public GarbageTruck()
    No explicit constructor is declared in the source; the compiler-provided default constructor is used. Field defaults are as declared above (m_GarbageCapacity = 10000, m_UnloadRate = 2000).

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Registers the ECS component types required on the prefab instance. This implementation adds:
  • ComponentType.ReadWrite()
  • ComponentType.ReadWrite() These are the components that will be present on every entity instantiated from this prefab.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Adds components that belong to the archetype for this prefab. This implementation always adds:

  • ComponentType.ReadWrite()
    Additionally, if the archetype already includes Moving (ComponentType.ReadWrite()), the method adds:
  • ComponentType.ReadWrite()
  • ComponentType.ReadWrite() This conditional behavior ensures that moving vehicles get pathfinding and service dispatch components in the same archetype.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Writes the initial component data to the created entity. It sets:

  • GarbageTruckData with the current m_GarbageCapacity and m_UnloadRate
  • UpdateFrameData with a fixed value of 2 Use this method to seed the entity with runtime data derived from the prefab fields.

Usage Example

// Example: create a custom garbage truck prefab that uses different capacity/unload rate
[ComponentMenu("Vehicles/Custom Garbage Truck", new Type[] { typeof(CarPrefab) })]
public class CustomGarbageTruck : GarbageTruck
{
    // Option A: change defaults via field initializer (applies before Initialize runs)
    public CustomGarbageTruck()
    {
        m_GarbageCapacity = 20000;
        m_UnloadRate = 4000;
    }

    // Option B: explicitly set component data during initialization
    public override void Initialize(EntityManager entityManager, Entity entity)
    {
        // Write custom values instead of relying on parent implementation
        entityManager.SetComponentData(entity, new GarbageTruckData(20000, 4000));
        entityManager.SetComponentData(entity, new UpdateFrameData(2));
    }
}

Notes / Modding tips: - Changing m_GarbageCapacity and m_UnloadRate affects runtime GarbageTruckData; choose values that balance performance and gameplay. - If your prefab includes a Moving component in the archetype, PathInformation and ServiceDispatch will be added automatically by GetArchetypeComponents — ensure your systems expect those components. - The UpdateFrameData(2) sets how often update systems run against this entity (see UpdateFrameData semantics in the codebase). Adjust with care.