Skip to content
Agentmart
Submit a tool

MonoFSM

MonoFSM 有限狀態機框架的使用指南。當需要:(1) 了解 MonoFSM 架構與設計理念 (2) 在 Unity Scene 中新增/修改 State、Transition、Condition、Action (3) 撰寫新的 Action、Condition C# 腳本 (4) 使用 Auto 系列…

Skill by RED-CANDLE-GAMES-CO-LTD

IndexedMIT★ 25
Write a review

Chalk Labs

Need help deploying MonoFSM?

Chalk Labs sets up agents like this inside real teams: connected to your tools, tested on your data, with a human in the loop where it matters.

Book a call

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/red-candle-games-co-ltd-monofsm

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

MonoFSM 有限狀態機框架的使用指南。當需要:(1) 了解 MonoFSM 架構與設計理念 (2) 在 Unity Scene 中新增/修改 State、Transition、Condition、Action (3) 撰寫新的 Action、Condition C# 腳本 (4) 使用 Auto 系列 Attribute 自動引用組件 (5) 理解狀態優先級系統 (6) 設定 VarFloat 計時器 (7) 使用 EffectDealer/EffectReceiver 互動系統 (8) 解析、匯出、或讀懂既有 FSM prefab/scene 物件的結構(用 FsmTextExporter 匯出 markdown 文字)時使用此 skill。

Use cases: documents

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md and code. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/monofsm

What's inside

Skill name
MonoFSM
Description
MonoFSM 有限狀態機框架的使用指南。當需要:(1) 了解 MonoFSM 架構與設計理念 (2) 在 Unity Scene 中新增/修改 State、Transition、Condition、Action (3) 撰寫新的 Action、Condition C# 腳本 (4) 使用 Auto 系列 Attribute 自動引用組件 (5) 理解狀態優先級系統 (6) 設定 VarFloat 計時器 (7) 使用 EffectDealer/EffectReceiver 互動系統 (8) 解析、匯出、或讀懂既有 FSM prefab/scene 物件的結構(用 FsmTextExporter 匯出 markdown 文字)時使用此 skill。
License
MIT
Instructions
206 lines in SKILL.md · view on GitHub

Agent Skills spec issues

  • name: only lowercase letters, digits and single hyphens allowed
  • files: referenced but missing: references/probe.md

Files (23)

  • SKILL.md
  • SKILL.md.meta
  • references.meta
  • components.md · references/
  • components.md.meta · references/
  • csharp-patterns.md · references/
  • csharp-patterns.md.meta · references/
  • design-patterns.md · references/
  • design-patterns.md.meta · references/
  • effect-system.md · references/
  • effect-system.md.meta · references/
  • fsm-traversal.md · references/
  • fsm-traversal.md.meta · references/
  • scene-editing.md · references/
  • scene-editing.md.meta · references/
  • serialization-migration.md · references/
  • serialization-migration.md.meta · references/
  • value-source.md · references/
  • value-source.md.meta · references/
  • var-wrapper.md · references/
  • var-wrapper.md.meta · references/
  • writing-actions.md · references/
  • writing-actions.md.meta · references/

SKILL.md (first lines)

# MonoFSM

以 GameObject 層級為核心的有限狀態機框架。

## 核心設計

**GameObject 層級表達式**:狀態、轉換、動作和條件都是場景中的 GameObject。

```
[FSM Root]                           # MonoFSMOwner
├── [VarFolder] VariableFolder       # 變數區(VariableFolder + StateMachineLogic)
│   └── f_varName                    # VarFloat / VarEntity 等
│       └── BoundModifier            # VariableFloatBoundModifier(可選)
├── [SchemaFolder] SchemaFolder
├── [StateFolder] StateFolder        # StateMachineLogic 主體
│   └── [State] StateName            # GeneralState
│       ├── OnStateEnterHandler      # 進入時觸發 → 子層放 Action
│       │   └── [Action] XxxAction   # AbstractStateAction 實作
│       ├── [Timer] TimerName        # VarFloatCountDownTimer(可選)
│       └── [Transition] => Target   # TransitionBehaviour
│           └── [Condition] Name     # AbstractConditionBehaviour 實作
└── Context                          # MonoContext
```

## 場景編輯(Unity MCP)

直接用 MCP 工具在 Scene 中編輯 FSM,見 [references/scene-editing.md](references/scene-editing.md)。

## 程式化讀取 FSM 結構

實作 Editor 工具(匯出、視覺化、批次修改)需要 traverse MonoFSM 階層時,見 [references/fsm-traversal.md](references/fsm-traversal.md)。涵蓋 StateFolder 偵測、變數/狀態/轉換/條件/動作的走訪規則,以及 `AnimatorPlayAction` 不繼承 `AbstractStateAction` 等 gotcha。實作範本:`MonoFSM/1_MonoFSM_Core/Editor/PrefabExporter/FsmTextExporter.cs`。

## Auto Attributes

```csharp
[Auto]                               // GetComponent<T>()
[AutoParent]                         // GetComponentInParent<T>()
[AutoChildren]                       // GetComponentsInChildren<T>()
[AutoChildren(DepthOneOnly = true)]  // 僅直接子物件
```

欄位型別可以是 interface(`[AutoParent] private ICurrentEntityOwner _owner;`),底層走 `GetComponentInParent(Type, true)`,取到的是**最近的一顆** parent,可用來當「多種容器共用同一個 child 元件」的自動接線。

### Editor 下 Auto 欄位還沒解析:用 AutoReferenceFieldEditor

Auto 系列在 **editor 下要等 Inspector 被點開才會解析**,所以 `Description`、`IsValid` 這類「畫 hierarchy 就會被呼叫」的成員裡,Auto 欄位常常還是 null → NRE 或一直噴 error。**不要**自己寫 `GetComponentInParent` 補,也不要用 `if (Application.isPlaying)` 迴避,要當場補解析:

```csharp
[ShowInInspector] [AutoParent] private ICurrentEntityOwner _owner;

private ICurrentEntityOwner Owner
{
    get
    {
        if (_owner == null)
            AutoAttributeManager.AutoReferenceFieldEditor(this, nameof(_owner));
        return _owner;
    }
}
```

- `AutoAttributeManager` 在 global namespace,不用 using;方法標了 `[Conditional("UNITY_EDITOR")]`,build 時整個 call site 被移除,內部又自己 `if (Application.isPlaying) return`,**runtime 零成本**
- 走的是同一顆 attribute 的 `Execute`,`LimitedType` / includeSelf 等設定都會被尊重,不會把語意寫死
- 反射結果進 `FieldCache`,比每次 `GetComponentInParent` 便宜
- 即使補了解析仍可能是 null(真的沒接),呼叫端還是要 null guard;**error log 只在 `Application.isPlaying` 時才印**,否則 editor 會刷滿 console
- 既有範例:`AbstractMonoVariable.HasParentVarEntity`、`MonoEntity._fsmLogic`、`MonoBlackboard` 的各 folder、`ValueProvider._parentEntity`、`VarEntityCurrentItem.Owner`

## 其他常用 Attributes

```csharp
[Required]       // 必填欄位(Inspector 警告)
[CompRef]        // 標記為組件引用
[DropDownRef]    // 下拉選擇(需手動在 Inspector 設定,MCP 無法設定此類型)
[SOConfig("子資料夾名")] // ScriptableObject 欄位用,提供 Create 按鈕與路徑選擇器
```

### SOConfig 注意事項

`[SOConfig]` 的 Drawer(`SOConfigAttributeDrawer`)使用 `IList.Add()` 新增資產,因此:
- **集合欄位必須用 `List<T>`,不可用 `T[]`**(原生陣列大小固定,`Add()` 會拋 `NotSupportedException`)
- 範例:`[SerializeField] [SOConfig("StateTags")] private List<StateTag> _stateTags = new();`

## 狀態優先級

狀態有 `Priority` 屬性,高優先級狀態不會被低優先級狀態打斷。

## 狀態進入條件

**優先把「能否進入此 State」的條件放在目標 State 的 `CanEnterState`。**
不要把相同條件分散複製到各個來源 State 的 Transition;這樣多個 State 要轉入同一目標時,只需各自建立轉向該 State 的 Transition,進入資格仍由目標 State 統一維護。

只有條件確實取決於「從哪個來源 State 離開」時,才放在該來源的 Transition。

## Transition 評估順序(AbstractStateBehaviour.IMonoState.OnFixedUpdate)

1. 目前 state 底下的 transition 照 **sibling 順序**跑,第一條成立就 `TryActivateState` 並 return。
2. 自己的都沒過,才問 `StateFolder.AllAnyStates` 的 transition;AnyState 指向目前 state 的那條會跳過。
3. 一條 transition 要過:condition 全部 AND(`IsAllValid`)+ source(自己或 AnyState)的 CanExit + target 的 CanEnter;
   接著 `TryActivateState` 再問一次**目前 state** 的 CanExit(含 priority)+ target CanEnter,target 也不能是目前 state。
4. 條件過了但 TryActivateState 被擋,**不會記任何東西**:看 `FsmTrace.CaptureSnapshot` 的 `canExit` / `canEnter` 欄。
5. 因為 2、3 兩道擋,AnyState 目標 state 的 CanEnter **不用自己加「!Is 自己」**。要擋的是「終點 state → 又回到這裡」,例如 Destroying 的 CanEnter 加 `!Is [State] Destroyed`。

### 網路同步會繞過 transition 直接改 state

- `StateMachine.Network.cs` 的 `ReadNetworkData`(IBeforeAllTicks)、`RestoreState`(resim / restore)、proxy 的 `Interpolate()`,
  都會**直接覆寫 `_activeStateId` / `_stateChangeTick`**,不走 transition、CanEnter,也不會跑 OnExit。
- `StateMachine.Render()` 只要看到 state id **或** changeTick 變了,就重跑 `OnEnterStateRender`。所以 client 上看到 enter 的視覺重播,
  不一定是 sim 重進,可能只是 changeTick 被同步蓋過去。
- 懷疑是這條路時,先開 FsmTrace 看 `Previous` 和 `IsResim` / `ProxyRender`。`IsResimFalse` 而且 Previous 是終點 state,就是 authority sim 自己轉的,
  不是網路蓋的,回去查 CanEnter 的 condition。

### 陷阱:module pack 的 state 靠 `_bindingRoot` 找 Owner

- 靠 `StateFolder._bindingRoot` 併進宿主 FSM 的 module pack(例如 Hittable_Destroyable ModulePack),parent 鏈上沒有 MonoFSMOwner。
  `AbstractStateBehaviour.Owner` 找不到的時候,會改抓 binding root 上的 owner(2026-10-01 補的)。
- 補之前 Owner 是 null,`IsStateCondition` 永遠 false,勾 Inverted 就變成永遠 true。實際發生過:`!Is [State] Destroyed` 沒擋到,
  造成 AnyState → Destroying → Destroyed 一直循環。現在 Owner 找不到的 IsStateCondition 會在 hierarchy 標紅。

## ModulePack:併進宿主 entity 的模組

From RED-CANDLE-GAMES-CO-LTD/MonoFSM, MIT.

More from RED-CANDLE-GAMES-CO-LTD/MonoFSM

Signals

Stars
25
Forks
2
Open issues
49
Last commit (this folder)
Oct 5, 2026 (29 days ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from RED-CANDLE-GAMES-CO-LTD

Done for you

Rather not set it up yourself?

Chalk Labs scopes, builds and runs agents for your team: the task audit, the eval harness, the integrations and the weekly ops report. Fixed scope, priced before we start.

See how we build AI agents →Read the case studies

Chalk Labs is a two-founder AI and growth studio. We build AI agents, CRMs and MVPs for Web3 and AI companies, and have shipped 50+ product and token launches.

launches shipped
50+
launches shipped
media placements
500+
media placements
founders on every account
2
founders on every account

Press we've placed: Forbes · CoinDesk · Decrypt · Cointelegraph · The Block

Rahil Jain, Co-founder, Chalk Labs. Ex-Goldman Sachs engineer. You talk to the person who builds it.

Get MonoFSM running in your business

Tell us what you want to automate. A founder replies within one working day with a plan, not a pitch deck.

Goes to the Chalk Labs founders, not the newsletter. Privacy

Need help deploying MonoFSM?

CallGet help
Cite this page

MonoFSM skill. Agent Mart. https://agentmart.dev/skills/red-candle-games-co-ltd-monofsm (last updated 2026-10-24)