# Resource containers

A resource container is a part's storage for a resource: a fuel tank's methalox, a pod's monopropellant, a battery's charge. Each container names a resource, a capacity, and how much it starts with. The parts module exposes a part's containers as a typed list with a one-call helper for adding them.

## The containers list

A part's containers are on `part.resourceContainers`. The list is keyed by resource name and has two helpers:

* `part.resourceContainers:Add(type, capacity, initial, nonStageable)` adds a container.
* `part.resourceContainers:Has(type)` tests whether the part already stores that resource.

## Adding storage

The common case is giving a part a resource it did not store, or topping up its capacity:

```lua
PM.Parts:Patch("AddMonopropToPod")
        :Named("pod_1v_command")
        :Do(function(part)
            part.resourceContainers:Add("MonoPropellant", 100, 100)
        end)
```

The arguments are:

* `**type**` is the resource name (`"MonoPropellant"`, `"Methalox"`, `"ElectricCharge"`, and so on).
* `**capacity**` is how many units the container holds.
* `**initial**` is how many units it starts filled with. It is optional and defaults to `0`. Pass the same value as the capacity for a full tank, as above.
* `**nonStageable**` is optional and defaults to `false`. Set it `true` for storage that should be exempt from staging (for example a reserve that is never drained as a stage).

## Guarding against duplicates

Use `:Has` to avoid adding a container the part already has, which keeps a broad patch from doubling up storage on parts that already store the resource:

```lua
:Do(function(part)
    if not part.resourceContainers:Has("ElectricCharge") then
        part.resourceContainers:Add("ElectricCharge", 50, 50)
    end
end)
```

You could also express "only parts without this resource" as a [gate](/doc/gates-Zh2sLnyxZX) with `:Requires`, depending on whether you want the whole patch to skip or just the one container.

## Containers versus the resource module

Adding a container puts the resource storage on the part. Defining the resource itself (its mass per unit, its flow mode, its cost) is a separate job handled by [Resources](/doc/resources-iyrGW6ZIcU). A container references a resource by name. The resource has to exist for that reference to mean anything, whether it is a stock resource or one you defined.