Skip to content

ItemGeneratorAsset — Generator Barricade Definition

Overview

ItemGeneratorAsset extends ItemBarricadeAsset to define fuel-burning power generators. Generators provide electricity to connected electrical items (lights, sentries, traps) within their wire range. They burn fuel over time, have a fuel capacity, and produce a configurable amount of power.

The class is one of the simplest barricade subclasses at 78 lines. It adds three key properties (capacity, wire range, burn rate) and a 3-byte state array for powered status and remaining fuel.

Source code location: Unturned/Bundles/ItemGeneratorAsset.cs (78 lines), inheriting from ItemBarricadeAsset.cs (605 lines), ItemPlaceableAsset.cs (454 lines), and ItemAsset.cs (base).

Inheritance Chain

ItemAsset → IArmorFalloff
  └─ ItemPlaceableAsset
       └─ ItemBarricadeAsset
            └─ ItemGeneratorAsset — fuel, power, wire range

Class Definition

csharp
public class ItemGeneratorAsset : ItemBarricadeAsset
{
    protected ushort _capacity;
    protected float _wirerange;
    protected float _burn;

    public ushort capacity => _capacity;
    public float wirerange => _wirerange;
    public float burn => _burn;

    public override byte[] getState(EItemOrigin origin) { ... }
    public override void BuildDescription(ItemDescriptionBuilder builder, Item itemInstance) { ... }
    public override void PopulateAsset(in PopulateAssetParameters p) { ... }
    internal override void BuildCargoData(CargoBuilder builder) { ... }
}

Core Properties

Fuel Capacity

.dat KeyTypeDefaultRangeDescription
CapacityushortRequired0–65,535Maximum fuel units
csharp
_capacity = p.data.ParseUInt16("Capacity");

Fuel capacity defines how many fuel units the generator can hold. A player fills the generator with fuel items (e.g., gas cans) which each provide a certain number of fuel units.

Burn Rate

.dat KeyTypeDefaultUnitDescription
BurnfloatRequiredSeconds per fuel unitTime between fuel unit consumption
csharp
_burn = p.data.ParseFloat("Burn");

The burn rate determines how quickly the generator consumes fuel. A value of 2.0 means one fuel unit is consumed every 2 seconds. Lower values = faster consumption.

Fuel consumption rate: fuelPerSecond = 1.0 / burn

Burn valueFuel units per secondFuel units per hour
1.01.03,600
2.00.51,800
5.00.2720
10.00.1360

Wire Range

.dat KeyTypeDefaultUnitDescription
WirerangefloatRequiredMetersMaximum wire connection distance
csharp
_wirerange = p.data.ParseFloat("Wirerange");
if (wirerange > PowerTool.MAX_POWER_RANGE + 0.1f)
{
    Assets.ReportError(this, "Wirerange is further than the max supported power range of "
        + PowerTool.MAX_POWER_RANGE);
}

The wire range defines how far the generator can connect to electrical items. The value is validated against PowerTool.MAX_POWER_RANGE — if the wire range exceeds the engine's maximum supported power range, an error is reported.


State Byte Layout

csharp
public override byte[] getState(EItemOrigin origin)
{
    return new byte[3];
}
ByteContent
0Powered state (0 = off, 1 = on)
1–2Fuel amount (ushort, little-endian)

The 3-byte state stores whether the generator is actively producing power and how much fuel remains. The powered state byte allows toggling the generator on/off.


BuildDescription — Inventory Tooltip

csharp
public override void BuildDescription(ItemDescriptionBuilder builder, Item itemInstance)
{
    base.BuildDescription(builder, itemInstance);

    builder.Append(localization.format("ItemDescription_FuelCapacity", capacity), DescSort_Important);

    if (burn > 0.0f)
    {
        const float SECONDS_PER_HOUR = 3600;
        float fuelPerHour = SECONDS_PER_HOUR / burn;
        int roundedFuelPerHour = Mathf.RoundToInt(fuelPerHour);
        builder.Append(localization.format("ItemDescription_FuelBurnRate", roundedFuelPerHour), DescSort_Important);

        float maxRuntimeSeconds = burn * capacity;
        float maxRuntimeHours = maxRuntimeSeconds / SECONDS_PER_HOUR;
        builder.Append(localization.format("ItemDescription_FuelMaxRuntime", maxRuntimeHours.ToString("0.00")), DescSort_Important);
    }
}

The tooltip shows three lines:

LineFormatExample (capacity=500, burn=2.0)
Fuel Capacity500500 units
Fuel Burn Rate1800Uses 1,800 units per hour
Max Runtime0.28Runs for 0.28 hours (16.7 minutes)

Calculation:

  • fuelPerHour = 3600 / burn = 3600 / 2.0 = 1800
  • maxRuntimeHours = (burn * capacity) / 3600 = (2.0 * 500) / 3600 = 0.277...

BuildCargoData — Wiki Export

csharp
CargoDeclaration data = builder.GetOrAddDeclaration("Generator");
data.Append("GUID", GUID);
data.Append("Capacity", capacity);
data.Append("Wirerange", wirerange);
data.Append("Burn", burn);
ColumnSource
GUIDPK, FK to Barricade
Capacity_capacity
Wirerange_wirerange
Burn_burn

.dat File Reference — Generator-Specific

.dat KeyTypeDefaultNotes
CapacityushortRequiredMax fuel units
WirerangefloatRequiredWire connection range (must be ≤ MAX_POWER_RANGE)
BurnfloatRequiredSeconds per fuel unit consumed

All ItemBarricadeAsset, ItemPlaceableAsset, and ItemAsset keys are available.


Modding Example — Basic Generator .dat

ini
GUID a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
Type Barricade
Build Generator
Health 200
Range 4
Radius 0.5
Offset 0.1
Capacity 500
Wirerange 16
Burn 2.0

500 fuel capacity, 16m wire range, burns 0.5 fuel/second (30 fuel/minute). Runs for 16.7 minutes on a full tank.


Modding Example — Industrial Generator .dat

ini
GUID f1e2d3c4b5a69788796a5b4c3d2e1f0a
Type Barricade
Build Generator
Health 500
Range 4
Radius 0.8
Offset 0.1
Capacity 2000
Wirerange 32
Burn 5.0
Armor_Tier High

Industrial generator: 2000 fuel capacity, 32m wire range, burns slowly at 5 sec/unit (0.2 fuel/sec). Full tank lasts 2.78 hours. High armor tier.


Modding Example — Portable Generator .dat

ini
GUID d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9
Type Barricade
Build Generator
Health 100
Range 4
Radius 0.3
Offset 0.05
Capacity 200
Wirerange 8
Burn 1.0
Armor_Tier Low

Portable generator: 200 fuel, 8m wire, fast burn (1.0 sec/unit = 1 fuel/sec). Runs for 3.3 minutes. Low armor.


Common Issues

  1. Wire range too large: Wire range must be ≤ PowerTool.MAX_POWER_RANGE. Exceeding this limit creates an asset error. Reduce the wire range or modify the server's MAX_POWER_RANGE constant.

  2. Fuel not burning: The Burn field must be greater than 0.0. A value of 0 would cause division by zero in consumption rate calculations.

  3. Capacity overflow: _capacity is ushort (max 65,535). Setting higher values wraps around. Use a spawn table for better fuel items instead of increasing capacity beyond this limit.

  4. Runtime calculation confusion: burn is seconds per unit, not units per second. A larger Burn value = slower consumption = longer runtime. This is the inverse of what you might expect.

  5. State size: The 3-byte state is fixed — generators always use 3 bytes regardless of capacity or configuration.

Document history

VersionDateAuthorNotes
1.02026-07-2857 StudiosInitial publication. Full generator asset documentation including fuel system, wire range, and modding examples.