# Typed Modules

The [Generic JSON API](/doc/generic-json-api-fqEwMuvrGb) can patch anything, but it makes you spell out the asset's JSON by hand. The typed modules sit on top of it and add helpers that understand a specific kind of content. `PM.Parts` knows what a module is. `PM.Planets` knows how to add a body to the galaxy. `PM.Science` knows how to unlock a part in a tech node.

You still register patches the same way (a `Patch` call, then the chain from [Core Concepts](/doc/core-concepts-JzW14TdnTL)). The difference is what you get inside the `Do` callback: a value with typed helpers instead of a bare JSON table.

## The shared pattern

Most typed helpers follow the same five methods, so once you learn them for one module they read the same everywhere:

* `Add<Thing>(name, callback)` creates a new child and errors if it already exists.
* `Patch<Thing>(name, callback)` edits an existing child and does nothing if it is missing.
* `Ensure<Thing>(name, callback)` adds it if missing or patches it if present. This is the one you usually want.
* `Remove<Thing>(name)` deletes it.
* `Has<Thing>(name)` tests for it.

For example, on a part: `part:EnsureModule("Module_Engine", function(module) ... end)`. The callback receives the thing you just added or found, and you fill it in there.

Lists you address by name (modules, variants, science regions, and so on) are exposed as indexed collections: you can read `data.cubes["DEPLOYED"]`, iterate them with `pairs`, and call `:Append` or `:RemoveWhere` on them. See [Working with JSON](/doc/working-with-json-fWfY8KrejF).

## The modules

* [Parts](/doc/parts-TLgScEaMqZ) is the largest. Parts hold modules, modules hold data, and there are dedicated helpers for engine modes and resource containers.
* [Planets](/doc/planets-4WfecyFI2a) patches celestial bodies, creates and copies galaxies, adds new bodies to them, and overrides atmosphere and cloud visuals.
* [Resources](/doc/resources-iyrGW6ZIcU) defines fuels and recipes.
* [Science](/doc/science-ddhsZhnZbd) covers experiments, science regions, discoverables, and the tech tree.
* [Missions](/doc/missions-8XD1wFHsqI) patches missions, including the condition and action builders.
* [Campaign packs](/doc/campaign-packs-5HBzAytKyU) creates the packs that players select for a campaign. It also sets the galaxy of each pack and the features that a new campaign starts with.
* [VSwift](/doc/vswift-qleULJnxbJ) adds part variants (the in-editor variant switcher).
* [Audio](/doc/audio-n0GoCslJZ0) attaches stock audio presets to a part.

## When a typed helper does not exist

The typed modules do not cover every field of every asset. When you need something they do not expose, you can always fall through to the raw JSON on the same value (assign a field directly, or use the JSON methods). The typed helpers and raw access work on the same underlying data, so mixing them is fine.

If you find yourself wishing a typed helper existed for something you patch often, say so. Contact the Redux developers, or the author of the mod whose content you are patching, and ask for it. :3

---

**Documents**

- [Campaign packs](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/campaign-packs-5HBzAytKyU)
- [Parts](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/parts-TLgScEaMqZ)
- [Missions](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/missions-8XD1wFHsqI)
- [Planets](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/planets-4WfecyFI2a)
- [Resources](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/resources-iyrGW6ZIcU)
- [Science](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/science-ddhsZhnZbd)
- [VSwift](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/vswift-qleULJnxbJ)
- [Audio](https://docs.ksp2redux.org/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/audio-n0GoCslJZ0)