# LuaPatch cheat sheet

Every method in the patch chain, grouped by what it does. All of them return the patch, so they chain. For the full explanation of any group, follow the link.

## Starting a patch

These come from a module and return a patch to chain on. See [Targeting assets](/doc/targeting-assets-8eefHSmg4w).

| Call | What it does |
|------|--------------|
| `PM.JSON:Patch(label, name)` | Patch any asset under `label` by raw JSON. |
| `PM.Parts:Patch(name)` | Patch parts. |
| `PM.Planets:Patch(name)` | Patch celestial bodies. |
| `PM.Resources:Patch(name)` | Patch resources and recipes. |
| `PM.Science:PatchExperiments(name)` etc. | Patch a science asset kind. |
| `PM.Missions:Patch(name)` | Patch missions. |
| `PM.CampaignPacks:Patch(name)` | Patch campaign packs. |
| `PM.Planets:PatchGalaxy(key, name)` | Patch one galaxy definition. |
| `PM:Patch(converter, label, name)` | Patch with an explicitly named converter. |

## Copying

These also start a patch, and copy each matched asset before running the chained `:Do` callback on the copy. See [Copying an existing asset](/doc/creating-assets-9bPpsuBU3M).

| Call | What it does |
|------|--------------|
| `PM.JSON:Duplicate(label, source, newName)` | Copy any asset under `label`. |
| `PM.Parts:Duplicate(source, newName)` | Copy parts. `PM.Planets`, `PM.Resources`, `PM.Missions` and `PM.CampaignPacks` have the same method. |
| `PM.Planets:DuplicateGalaxy(key, newKey)` | Copy a galaxy definition. |
| `PM.Planets:DuplicateAtmosphereOverride(...)`, `:DuplicateCloudOverride(...)` | Copy an atmosphere or cloud override. |
| `PM.Science:DuplicateRegions(...)` etc. | Copy a science asset kind. |
| `PM:Duplicate(converter, label, source, newName)` | Copy with an explicitly named converter. |

`source` takes `*` and `?` wildcards, and `newName` takes `{name}` for the source's name. Each also takes the callback as an optional last argument, as shorthand for `:Do`. A copy keeps the internal ids of its source (`partName`, `bodyName`). Give the copy a new id in the callback.

## The change

See [The Do callback](/doc/the-do-callback-Qq6LNtj9BT).

| Call | What it does |
|------|--------------|
| `:Do(function(value) ... end)` | The callback that edits the asset. Return `"remove"` to delete it. |

## Targeting

See [Targeting assets](/doc/targeting-assets-8eefHSmg4w).

| Call | What it does |
|------|--------------|
| `:Named(...)` | Run only against these asset ids. Repeatable. |
| `:NotNamed(...)` | Do not run against these asset ids. |

## Gates

See [Gates](/doc/gates-Zh2sLnyxZX).

| Call | What it does |
|------|--------------|
| `:Requires(predicate, message?)` | Skip unless the predicate (or constant boolean) passes. |
| `:Has(key, predicate?, message?)` | Skip unless the asset exposes `key` (and the value passes the predicate). |
| `:HasNo(key, message?)` | Skip if the asset exposes `key`. |

## Features

See [Features](/doc/features-geboWdIiDN). An id without a mod prefix is a feature of your mod. Each patch also needs the default feature of your mod.

| Call | What it does |
|------|--------------|
| `:Feature(...)` | Run only in feature sets that have all of the listed features on. Also available on created assets and `PM:Prefab` builders. |
| `:NoFeature(...)` | Run only in feature sets that have none of the listed features on. |
| `PM:WithFeature(id, function() ... end)` | Add the feature to each patch, created asset, prefab patch, run callback and config entry that the function registers. |
| `PM:DefineFeature(id, options?)` | Declare a feature, with `requires`, `conflicts`, `provides` and `recommended`. Call it only during registration. |
| `PM:OnBeginRun(function(set) ... end)` | Run before the patch run of each feature set. Accepts `:Feature(id)` and `:NoFeature(id)`. |
| `PM:OnEndRun(function(set) ... end)` | Run after the patch run of each feature set. |

## Pass and bucket

See [The patch lifecycle](/doc/the-patch-lifecycle-bV7YhyoMv8).

| Call | What it does |
|------|--------------|
| `:Early()` | Run in the early pass. |
| `:Late()` | Run in the late pass. |
| `:First()` | Run in the first bucket of its pass. |
| `:Last()` | Run in the last bucket of its pass. |

Default (none of these) is the Default pass, Default bucket.

## Dependencies

See [Dependencies and ordering](/doc/dependencies-and-ordering-werp1yYh7P). These take mod ids or patch ids.

| Call | What it does |
|------|--------------|
| `:Needs(...)` | Run only if these mods are installed and on in the feature set. |
| `:Conflicts(...)` | Skip if one of these mods is installed and on in the feature set. |
| `:NeedsPatch(...)` | Run only if these patches also run. |
| `:ConflictsPatch(...)` | Skip if any of these patches runs. |

## Ordering

Within the same bucket only.

| Call | What it does |
|------|--------------|
| `:After(...)` | Run after every patch from these mods. |
| `:Before(...)` | Run before every patch from these mods. |
| `:AfterPatch(...)` | Run after these patches. |
| `:BeforePatch(...)` | Run before these patches. |

## JSON value methods

Available on the value inside a callback. See [Working with JSON](/doc/working-with-json-fWfY8KrejF).

| Call | What it does |
|------|--------------|
| `:Append(value)` | Add to the end of an array. |
| `:Insert(index, value)` | Insert at a 1-indexed position. |
| `:RemoveAt(index)` | Remove the element at a 1-indexed position. |
| `:RemoveWhere(predicate)` | Remove every element the predicate matches. |
| `:Clear()` | Empty an array. |
| `:Count` | Number of elements. |
| `:HasKey(key)` | Whether an object has a key. |
| `:Keys()` | An object's property names. |
| `:Remove(key)` | Remove an object property. |
| `:Patch(key, callback)` | Edit the value at `key` if present. |

## J helpers

See [Working with JSON](/doc/working-with-json-fWfY8KrejF).

| Call | What it does |
|------|--------------|
| `J.Int(value)` | Force an integer into a new or non-integer JSON slot. |
| `J.Empty()` | Make an empty JSON array. |
| `J(value)` | Convert a Lua value to the JSON-backed form. |