Listing and switching devices
This is the full list of what the API gives you, one section for each part. Text in brackets at the end of a note is extra detail you can skip on a first read.
PowerDevices list, read and switch power devices
Use it to list the power devices in the scene, read one by its Id, or switch one on or off with SetOn.
public static class PowerDevices
{
public static List<PowerDeviceInfo> All();
public static PowerDeviceInfo Get(string id);
public static bool SetOn(string id, bool on);
public static event Action<PoweredChangedArgs> PoweredChanged;
public static event Action<FuelRanOutArgs> FuelRanOut;
public static bool IsAvailable { get; }
}
| Property | Type | What it means |
|---|---|---|
IsAvailable | bool | false when the power events could not be set up. |
| Method | Returns | What it does |
|---|---|---|
All() | List<PowerDeviceInfo> | Every power device in the scene you are in. |
Get(id) | PowerDeviceInfo | The device with this id. null when none has it. |
SetOn(id, on) | bool | Switches a device on or off. Returns true when it is now in that state. |
| Event | Gives you | When it fires |
|---|---|---|
PoweredChanged | PoweredChangedArgs | A furniture device or a battery has gained or lost power. IsPowered in the event is the new state. |
FuelRanOut | FuelRanOutArgs | A fuel burner has run out of fuel. |
PowerDeviceInfo a snapshot of one device
PowerDevices.All() and PowerDevices.Get(id) give you these, one for each device. Check IsPowered, not IsOn, to see whether a device is really running.
public sealed class PowerDeviceInfo
{
public string Id { get; }
public PowerDeviceKind Kind { get; }
public bool IsOn { get; }
public bool IsPowered { get; }
public float NetPower { get; }
public float FuelLevel { get; }
public float MaxFuel { get; }
public float StoredCharge { get; }
public float MaxCharge { get; }
public List<int> FuelItemIds { get; }
public bool IsBurning { get; }
public float BurnTimeFactor { get; }
}
| Property | Type | What it means |
|---|---|---|
Id | string | The device’s id: its save id, or its place in the scene if it has none. |
Kind | PowerDeviceKind | Whether the device gives power (Source) or uses it (Consumer). |
IsOn | bool | true when the device is switched on. |
IsPowered | bool | true when the device is really getting power and running. |
NetPower | float | Furniture only: positive when it makes power, negative when it uses power. 0 for everything else. |
FuelLevel | float | Seconds of burn time left. 0 for devices that do not burn fuel. |
MaxFuel | float | The most seconds of burn time it can hold. 0 for devices that do not burn fuel. |
StoredCharge | float | Batteries only: the charge stored now, in the game’s own units. 0 for everything else. |
MaxCharge | float | Batteries only: the most charge it can store. 0 for everything else. |
FuelItemIds | List<int> | The item numbers of the fuel it accepts. Empty when it burns no fuel. |
IsBurning | bool | Fuel burners only: true while fuel is burning. |
BurnTimeFactor | float | Fuel burners only: how fast it burns fuel. 0 for everything else. |
PowerDeviceKind whether a device gives or uses power
This is the value of PowerDeviceInfo.Kind. Source devices feed power to others, and Consumer devices use it.
public enum PowerDeviceKind
{
Source,
Consumer
}
| Value | Meaning |
|---|---|
Source | The device gives power to others: a generator, a battery, or furniture that produces power. |
Consumer | The device uses power, including a stove that burns its own fuel. |
Things to know
All()andGet(id)read the device as it is right now, and they are slow, so do not call them every frame. They search the scene each time and look up the fuel items. Call them when something happens, or on a timer. APowerDeviceInfois a snapshot, so ask again for fresh values. (The id is the object’s GUID (FurnitureStats.GUID,CraftingBase.GUIDorBatteryPowerSource.GUID, see the glossary), or the scene path when it has none.FuelItemIdswalks the item database for each fuel category.)KindisSourcewhen the device feeds power to others. That is a generator, a battery, or a piece of furniture that makes power. Everything else is aConsumer, even a stove that burns its own fuel. (Sourcemeans aGenerator, aBatteryPowerSource, or furniture withNetPowerabove 0.)IsOnis the device’s switch.IsPoweredis whether it is really running. A device can be switched on and still have no power, for example a generator with no fuel, or a lamp whose power line cannot feed it. (For furniture,IsOnisFurnitureStats.isOnandIsPoweredisFurnitureStats.isActive, which staysfalsewhen the switch is on but the grid cannot feed the device. For a crafting station, they areCraftingBase.OnandCraftingBase.PoweredAndActive. For a battery,IsOnis whether drain is enabled (BatteryPowerSource.drainEnabled) andIsPoweredisBatteryPowerSource.powering; both are read by reflection.)NetPoweronly means something for furniture. Crafting stations and batteries always give0, because the game does not use a power draw for them. (It isFurnitureStats.netPowerOn.PowerSource.powerConsumptionis marked “Not used anywhere yet” in the game.)FuelLevelandMaxFuelare in seconds of burn time. For a crafting station that needs fuel, they are what is left and the most it holds. For a battery, they are how long it would run at its current drain, whether or not drain is on. For a battery that is charging they are0, because no burn time applies. For everything else they are0. (For stations they areFuel.BurnTimeRemainingandFuel.MaxBurnTime. For batteries, they are the stored charge and the capacity divided byBatteryPowerSource.fuelDrainPerHour, times 3600, and0when the drain is zero or negative.)StoredChargeandMaxChargeonly apply to batteries. They add up all the battery’s storage slots, in the game’s own units. They are0for everything else. (The units areItemAmmodata.ammoandmaxAmmo, orItemLiquidData.GetLiquidAmountandGetLiquidMaxAmount. The battery’sfuelTypeflags choose which meta types count, the same asBatteryPowerSource.CheckItems.)FuelItemIdslists the items a fuel burner accepts. It is empty for devices that burn no fuel. It is also empty for batteries, because a battery accepts fuel by what kind of item it is, not by a list of items. (The ids are the item ids in the device’s fuel categories (Fuel.FuelCategories), the same filter the game’s fuel slot uses.)IsBurningandBurnTimeFactoronly apply to crafting stations that burn fuel.IsBurningistruewhile fuel is burning.BurnTimeFactoris how fast the machine burns fuel, which depends on its quality and upgrades. Both givefalseor0for everything else. (They areFuel.activeandFuel.BurnTimeFactor.)SetOn(id, on)switches the device the way the game does. It returnstruewhen the device is in the state you asked for afterwards, so asking for the state it is already in also givestrue. It returnsfalsefor an unknown or empty id, for a device the game keeps always on, and while a save is loading or saving. (For furniture it callsFurnitureStats.turnONorturnOFF. For a crafting station it callsCraftingBase.Toggle(on), and returnsfalsefor anAlwaysOnstation, which the game ignores. For a battery it callsBatteryPowerSource.ToggleDrain(on).)
Limits
- Switching on a station that has no fuel works, but it does not run.
IsOnbecomestruewhileIsPoweredstaysfalse, because the game does not check the fuel when you switch it on. (A crafting station’sToggledoes not check fuel.)