Quick start · Docs · Unity client · Shardy · Report Bug
This package is an implementation of a finite state machine (FSM) for Shardy, and more. It provides a flexible API for building FSMs using a chain of methods and conditions.
Note
Shardy is a framework for online games and applications for Node.js. It provides the basic functionality for building microservices solutions: mobile, social, web, multiplayer games, realtime applications, chats, middleware services, etc.
- Flexible API: states, triggers, conditions
- Custom state and trigger types
- UML diagram generation (notes support)
- Good reference materials: docs, snippets, samples
- No 3rd party libs
Get it from releases page or add the line to Packages/manifest.json
and module will be installed directly from Git url:
"com.mopsicus.shardy.fsm": "https://github.com/mopsicus/shardy-fsm.git",
For a better experience, you can set up an environment for local development. Since Shardy and all modules (like this) are developed with VS Code, all settings are provided for it.
- Use
Monokai Pro
oreppz!
themes - Use
FiraCode
font - Install extensions:
- C#
- C# Dev Kit
- Unity
- Enable
Inlay Hints
in C# extension - Install
Visual Studio Editor
package in Unity - Put
.editorconfig
in root project directory - Be cool
- Install package
- Add
Shardy.FSM
to uses section - Define your states and triggers
- Call
Builder
method - Add states, transtions, conditions
- Call
Build
method to finish initialization - Call trigger(s)
- Profit
using Shardy.FSM;
/// <summary>
/// Test FSM
/// </summary>
void Test() {
_fsm = FSM<State, Action>.Builder(State.Standing)
.State(State.Standing)
.OnEnter((data) => {
Debug.Log("on enter standing");
})
.To(State.Sitting).On(Action.Down)
.To(State.Jumping).On(Action.Space).If(s => _isConditionActive)
.State(State.Sitting)
.To(State.Lying).On(Action.Down)
.To(State.Standing).On(Action.Up)
.State(State.Lying)
.To(State.Sitting).On(Action.Up)
.State(State.Jumping)
.Note("some help message here")
.OnExit((data) => {
Debug.Log("on exit jumping");
})
.To(State.Standing).On(Action.Down)
.Build();
_fsm.Trigger(Action.Down);
}
You can also add condition(s) to transitions. When the trigger is called, it checks if all conditions are met, and if so, the transition is allowed.
State(State.Standing).To(State.Jumping).On(Action.Space).If(YOUR_CONDITION)
FSM allows you to generate code with UML structure:
@startuml
skin rose
title TestFSM
left to right direction
agent Standing
agent Sitting
agent Lying
agent Jumping
note left of Jumping
some help message here
end note
Start --> Standing
Standing --> Sitting : Down
Standing ~~> Jumping : Space
Sitting --> Lying : Down
Sitting --> Standing : Up
Lying --> Sitting : Up
Jumping --> Standing : Down
@enduml
And you can render it on https://www.planttext.com or http://www.plantuml.com/plantuml/uml/ and check your FSM for correct transitions and triggers:
Important
Dotted lines are transitions configured with conditions.
Note
You can also pass a file with UML content and render the diagram at url: http://www.plantuml.com/plantuml/proxy?src=XXX, where XXX is the url to your *.puml file.
See the sample section to get a demo app. This demo contains a simple FSM example with a small number of states and triggers.
Tested in Unity 2022.3.x.
We invite you to contribute and help improve FSM for Shardy. Please see contributing document. 🤗
You also can contribute to the Shardy project by:
- Helping other users
- Monitoring the issue queue
- Sharing it to your socials
- Referring it in your projects
You can support Shardy by using any of the ways below:
- Bitcoin (BTC): 1VccPXdHeiUofzEj4hPfvVbdnzoKkX8TJ
- USDT (TRC20): TMHacMp461jHH2SHJQn8VkzCPNEMrFno7m
- TON: UQDVp346KxR6XxFeYc3ksZ_jOuYjztg7b4lEs6ulEWYmJb0f
- Visa, Mastercard via Boosty
- MIR via CloudTips
Before you ask a question, it is best to search for existing issues that might help you. Anyway, you can ask any questions and send suggestions by email or Telegram.
FSM for Shardy is licensed under the MIT License. Use it for free and be happy. 🎉