Skip to content

Commit b50ac8a

Browse files
committed
Adding some guide for Buff & Debuff
1 parent 64aad51 commit b50ac8a

File tree

7 files changed

+579
-1
lines changed

7 files changed

+579
-1
lines changed

BuffDebuff/img/buffdebuffarch.png

28.6 KB
Loading

BuffDebuff/img/neg-buff.jpg

43.6 KB
Loading

BuffDebuff/img/pos-buff.jpg

73 KB
Loading

BuffDebuff/img/random-buff.jpg

38.2 KB
Loading

BuffDebuff/index.MD

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,21 @@ With this mod, you can effortlessly apply buffs or debuffs to any in-game entity
99

1010
![Buff & Debuff System](../img/buffdemo.jpg)
1111

12-
# Entities
12+
You can check out a complete [Demo project](https://github.com/datvm/TimberbornMods/tree/master/BuffDebuffDemo) or the mod [Global Wellbeing](https://github.com/datvm/TimberbornMods/tree/master/GlobalWellbeing) for usage. This guide will walk you through making a mod like `BuffDebuffDemo`.
13+
14+
# Guides
15+
16+
![Buff & Debuff Flow](./img/buffdebuffarch.png)
17+
18+
- [Quick Read](./quickstart)
19+
- [Step-by-step Guide](./stepbystep.MD)
20+
21+
# Architecture
22+
23+
The Buff & Debuff System consists of the following components:
24+
25+
- `IBuffEntity`: everything is a buff entity and they have a unique `long Id` assigned when registered to the `IBuffEntityService`.
26+
- `IBuffEntityService`: the singleton service that manages all buff entities. You can get all or one by Id with this service. `Register` and `Unregister` most likely won't need to be called by you.
27+
28+
- `BuffableComponent`: Attached to all entities in the game. They have a unique ID to identify them. You can get them all from the `IBuffableService` singleton.
1329

BuffDebuff/quickstart.MD

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
![Buff & Debuff Flow](./img/buffdebuffarch.png)
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

Comments
 (0)