|
| 1 | +--- |
| 2 | +title: "Quick Start for Buff & Debuff System" |
| 3 | +description: Adding customizable buffs and debuffs to various in-game entities. |
| 4 | +date: 2025-02-26 |
| 5 | +tags: mods timberborn buff-debuff |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## The very short list |
| 11 | + |
| 12 | +1. Refer to the mod DLL. |
| 13 | +2. Create and register an `IBuff` singleton instance. |
| 14 | +3. Create one (or more) `BuffInstance`. |
| 15 | +4. Create `IBuffTarget` and `IBuffEffect` for the `BuffInstance`. |
| 16 | +5. Process the applied `BuffInstance` in your mod using Singleton, BaseComponent or Harmony patch. |
| 17 | + |
| 18 | +## A bit more detailed |
| 19 | + |
| 20 | +1. Add the mod reference to your project. The file should be at `"SteamLibrary\steamapps\workshop\content\1062090\3433810580\version-0.7\BuffDebuff.dll"`. |
| 21 | + - Optionally add `global using BuffDebuff` as this will be the namespace for all Buff & Debuff System classes. |
| 22 | + |
| 23 | +2. Create a singleton **Buff** class similar to `BuffDebuffDemo.Buffs.PositiveBuff` that implements `IBuff`. This class will declare general information about the buff, such as its name, description. It will also act as a manager and factory for the **BuffInstance**. |
| 24 | + - You can create a class from scratch or inherit from `SimpleBuff`, `SimpleValueBuff` or `SimpleFloatBuff`. The latter two are for buffs that create instances with a simple value. |
| 25 | + - If you use `SimpleValueBuff` or similar, you do not have enough class (`BuffInstance`) to provide for the generic parameters yet. Just ignore and go ahead to the next step. |
| 26 | + - You are not limited to one type of `BuffInstance` for a `Buff`. You can create any number of `BuffInstance` of any type(s) you want. |
| 27 | + |
| 28 | +> [!TIP] |
| 29 | +> `BuffInstance`, `IBuffTarget`, and `IBuffEffect` all have `Init()`, `Update...()` and `CleanUp()` methods. You can override them to add your custom logic. |
| 30 | +
|
| 31 | +3. Create a **BuffInstance** class similar to `BuffDebuffDemo.Buffs.PositiveBuffInstance` that inherit `BuffInstance`. This class will declare the targets and effects of the **Buff**. |
| 32 | + - You can inherit your class directly from `BuffInstance` or use some provided classes that support common use cases, such as `BuffInstance<TValue, TBuff>` or `TimedBuffInstance`. |
| 33 | + - **BuffInstance** must have a `new()` (parameterless) constructor. You then use `InjectAttribute` to inject necessary services into the instance similar to how you code a `BaseComponent`. |
| 34 | + - Use `Init()` to setup your instance. This method is called after the instance is created **and injected**. You would have your `IBuffInstance<TBuff>.Buff` and `IValuedBuffInstance<TValue>.Value` available at this point as well if your instance is of those types. |
| 35 | + - Use `Update()` if you need to update your instance every tick even when its `Active` is `false`. If you set `Active` to `false`, the `Targets` won't be called right in that tick. |
| 36 | +4. Create `IBuffTarget`(s) for your buff instance to target. This interface will define the target(s) of the buff instance. |
| 37 | + - You can create a class from scratch or inherit from `GlobalBuffTarget` and its subclasses (`GlobalBeaverBuffTarget`, `GlobalAdultBeaverBuffTarget`, `GlobalChildBeaverBuffTarget`, `GlobalBotBuffTarget`), or `IdsBuffTarget`. |
| 38 | + - Each tick, `UpdateTargets()` is called. If the `TargetsChanged` property is `true`, the game will use `Targets` as new targets and remove the buff instance from the old targets. |
| 39 | +5. Create `IBuffEffect`(s) for your buff instance to apply. There is `UpdateEffect()` method that is called every tick if you need to. |
| 40 | + |
| 41 | +6. Create another class (it can be a game's singleton or a Harmony patch) that uses `IBuffService` to scan for your `BuffInstance`. Then decide what to do with the `IBuffEffect`(s) that they have. |
| 42 | + - Another way (recommended in the [Step-by-step guide](./stepbystep.MD)) is to add another `BaseComponent` to the entities that the buff can apply to (like `BeaverSpec`) and listen to the events. |
0 commit comments