TheSingularityWorkshop.FSM_API
1.0.9
dotnet add package TheSingularityWorkshop.FSM_API --version 1.0.9
NuGet\Install-Package TheSingularityWorkshop.FSM_API -Version 1.0.9
<PackageReference Include="TheSingularityWorkshop.FSM_API" Version="1.0.9" />
<PackageVersion Include="TheSingularityWorkshop.FSM_API" Version="1.0.9" />
<PackageReference Include="TheSingularityWorkshop.FSM_API" />
paket add TheSingularityWorkshop.FSM_API --version 1.0.9
#r "nuget: TheSingularityWorkshop.FSM_API, 1.0.9"
#:package TheSingularityWorkshop.FSM_API@1.0.9
#addin nuget:?package=TheSingularityWorkshop.FSM_API&version=1.0.9
#tool nuget:?package=TheSingularityWorkshop.FSM_API&version=1.0.9
FSM_API
Blazing-fast, software-agnostic Finite State Machine system for any C# application.
๐ Overview
FSM_API is a modular, runtime-safe, and fully event-aware Finite State Machine (FSM) system designed to plug directly into any C# application from enterprise software to games, simulations, robotics, or reactive systems. It provides a powerful and decoupled approach to managing complex state-driven logic, ensuring clarity, consistency, and control across diverse domains.
โ
Thread-safe operations (main thread only, deferred mutation handling)
๐ง Decoupled state logic from data (POCO-friendly)
๐๏ธ Define once, instantiate many
๐ ๏ธ Error-tolerant FSM lifecycle management
๐งช Dynamic update ticking with frame/process throttling
No external dependencies. No frameworks required. No boilerplate setup. Pure C# power.
๐ก Why FSM_API?
Traditional FSM systems often suffer from tight coupling to specific environments or force rigid coding patterns. FSM_API liberates your state management:
Feature | FSM_API โ | Traditional FSM โ |
---|---|---|
Framework agnostic | โ | โ |
Runtime-modifiable definitions | โ | โ |
Deferred mutation safety | โ | โ |
Named FSMs & Processing Groups | โ | โ |
Built-in diagnostics & thresholds | โ | โ |
Pure C# with no external deps | โ | โ |
๐ Quickstart
- Define a simple context (your data model):
public class LightSwitch : IStateContext
{
public bool IsOn = false;
public bool IsValid => true; // Essential for FSM validation
public string Name { get; set; } = "KitchenLight";
}
- Define and build your FSM:
FSM_API.CreateProcessingGroup("MainLoop");
// Define the condition for the transition from "Off" to "On".
Func<IStateContext, bool> shouldTurnOn = ctx =>
{
if (ctx is LightSwitch l)
{
return l.IsOn;
}
return false;
};
// Define the condition for the transition from "On" to "Off".
Func<IStateContext, bool> shouldTurnOff = ctx =>
{
if (ctx is LightSwitch l)
{
return !l.IsOn;
}
return false;
};
// Use the fluent API to create and define the FSM.
// The .BuildDefinition() call finalizes the blueprint.
FSM_API.Create.CreateFiniteStateMachine("LightSwitchFSM", processRate: 1, processingGroup: "MainLoop")
// Define the "Off" state and its OnEnter action.
.State("Off",
onEnter: ctx => { if (ctx is LightSwitch l) l.IsOn = false; },
onUpdate: null,
onExit: null)
// Define the "On" state and its OnEnter action.
.State("On",
onEnter: ctx => { if (ctx is LightSwitch l) l.IsOn = true; },
onUpdate: null,
onExit: null)
.WithInitialState("Off") // Set the starting state.
// Define the transitions using the condition functions.
.Transition("Off", "On", shouldTurnOn)
.Transition("On", "Off", shouldTurnOff)
.BuildDefinition();
- Create an instance:
var kitchenLight = new LightSwitch();
var handle = FSM_API.Create.CreateInstance("LightSwitchFSM", kitchenLight, "MainLoop");
- Tick the FSM:
FSM_API.Interaction.Update("MainLoop");
๐ง Core Concepts
- FSMBuilder โ fluently define states, transitions, and actions
- FSMHandle โ runtime instance control (pause, reset, query state)
- IStateContext โ implement for your domain objects (POCOs)
- Processing Groups โ organize FSM ticking (UI, AI, physics, etc.)
- Error Handling โ thresholds & diagnostics built-in
- Thread Safety โ deferred mutation model keeps concurrency safe
๐ฆ Features at a Glance
Capability | Description |
---|---|
๐ Deterministic State Logic | Effortlessly define predictable state changes based on dynamic conditions or explicit triggers, ensuring your application's behavior is consistent and, where applicable, mathematically provable. Ideal for complex workflows and reliable automation. |
๐ญ Context-Driven Behavior | Your FSM logic directly operates on any custom C# object (POCO) that implements IStateContext . This enables clean separation of concerns (logic vs. data) and allows domain experts (e.g., BIM specifiers) to define behavior patterns that developers then implement. |
๐งช Flexible Update Control | Choose how FSMs are processed: event-driven, tick-based (every N frames), or manual. This adaptability means it's perfect for real-time systems, background processes, or even complex user interactions within any application loop. |
๐งฏ Robust Error Escalation | Benefit from per-instance and per-definition error tracking, providing immediate insights to prevent runaway logic or invalid states without crashing your application. Critical for long-running services and mission-critical software. |
๐ Runtime Redefinition | Adapt your application on the fly! FSM definitions can be redefined while actively running, enabling dynamic updates, live patching, and extreme behavioral variation without recompilation or downtime. Perfect for highly configurable systems. |
๐ฏ Lightweight & Performant | Engineered for minimal memory allocations and optimized performance, ensuring your FSMs are efficient even in demanding enterprise or simulation scenarios. No overhead, just pure C# power. |
โ Easy to Unit Test | The inherent decoupling of FSM logic from context data ensures your state machines are highly testable in isolation, leading to more robust and reliable code with simplified unit testing. |
๐ฏ Mathematically Provable | With clearly defined states and transitions, the FSM architecture lends itself to formal verification and rigorous analysis, providing a strong foundation for high-assurance systems where correctness is paramount. |
๐ค Collaborative Design | FSMs provide a visual and structured way to define complex behaviors, fostering better communication between developers, designers, and domain experts, and enabling less code-savvy individuals to contribute to core logic definitions. |
๐ฎ Unity Integration Available | Now preparing for submission to the Unity Asset Store. |
๐ Whatโs Next?
- ๐ Full Documentation & Wiki (TBD)
- ๐งช Unit Tests & Benchmarks (in development)
- ๐ Plugins & Extension Framework (e.g., editors, debugging)
๐ค Contributing
PRs, issues, and extensions welcome! Letโs build smarter state logic together.
๐ License
MIT License โ use it, hack it, build amazing things with it.
๐ง Brought to you by
The Singularity Workshop โ Tools for the curious, the bold, and the systemically inclined.
<img src="FSM_API/Assets/TheSingularityWorkshop.png" alt="The Singularity Workshop" width="200" height="200">
Because state shouldnโt be a mess.
Support the project: Donate via PayPal
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.