# Lua prefab patches

Lua authoring is useful for compact prefab changes that do not need a Unity prefab variant. It uses the `PM:Prefab` builder and the same runtime schema as visual and C# patches.

## A minimal component value change

```lua
PM:Prefab("toolbar-tint", "SomeWindow.prefab")
    :SetComponent(
        "set-background-red",
        "KSP2UIWindow/Root/Window-App/Background",
        "UnityEngine.UI.Image",
        "m_Color.r",
        0.25
    )
    :Register()
```

The first argument to `PM:Prefab` is the patch name. The second is the stock prefab's Addressables key.

`SetComponent` is a shortcut for a scalar field on a component. Its arguments are operation ID, hierarchy path, component type, serialized property path, value, and an optional component ordinal.

## Reusable targets

```lua
local patch = PM:Prefab("hide-button", "SomeWindow.prefab")
local button = patch:GameObject("Root/Buttons/OldButton")

patch:Active("disable-old-button", button, false)
patch:Register()
```

Use `:GameObject(path)` and `:Component(path, type, ordinal)` when several operations share a target.

## Available operations

The Lua builder provides `:Set`, `:Reference`, `:Active`, `:Suppress`, `:AddObject`, `:AddComponent`, and `:RemoveComponent`.

Complex added objects and components are Lua tables matching the public prefab-patch model. For large hierarchies, visual authoring is usually easier and gives immediate Inspector feedback.

## Dependencies and order

The builder also provides:

* `:Needs` and `:Conflicts` for mod IDs
* `:NeedsPatch` and `:ConflictsPatch` for patch IDs
* `:BeforePatch` and `:AfterPatch`
* `:Before` and `:After` for mod-wide ordering
* `:Early`, `:Late`, `:First`, and `:Last`
* `:Configuration` for cache inputs
* `:Feature` and `:NoFeature` for [features](/doc/features-geboWdIiDN)

Keep ordinary patches in the default pass and bucket. If you target something added by another patch, declare `:NeedsPatch("other-mod:patch-name")`.

## Features

A prefab patch applies only in campaigns where the default feature of its mod is on. Two builder methods add more conditions:

* `:Feature(...)`: all of the listed features must also be on.
* `:NoFeature(...)`: the patch does not apply while one or more of the listed features is on.

A bare id is a feature of your mod. A `PM:Prefab` call inside `PM:WithFeature` also gets the feature of that scope.

```lua
PM:Prefab("hard-mode-tint", "SomeWindow.prefab")
    :Feature("HardMode")
    :SetComponent(
        "set-background-red",
        "KSP2UIWindow/Root/Window-App/Background",
        "UnityEngine.UI.Image",
        "m_Color.r",
        0.5
    )
    :Register()
```

## Choosing Lua or visual authoring

Use Lua for a few known property changes or generated tables. Use visual authoring when you need to inspect the hierarchy, add several objects/components, manage Unity object references, or target ambiguous stock paths.