Chickensoft.LogicBlocks 6.0.0

dotnet add package Chickensoft.LogicBlocks --version 6.0.0
                    
NuGet\Install-Package Chickensoft.LogicBlocks -Version 6.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chickensoft.LogicBlocks" Version="6.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chickensoft.LogicBlocks" Version="6.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Chickensoft.LogicBlocks" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Chickensoft.LogicBlocks --version 6.0.0
                    
#r "nuget: Chickensoft.LogicBlocks, 6.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Chickensoft.LogicBlocks@6.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Chickensoft.LogicBlocks&version=6.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Chickensoft.LogicBlocks&version=6.0.0
                    
Install as a Cake Tool

๐Ÿ’ก LogicBlocks

Chickensoft Badge Discord Read the docs line coverage branch coverage

LogicBlocks is a serializable, hierarchical state machine package for C# that works well when targeting ahead-of-time (AOT) environments. LogicBlocks draws inspiration from statecharts, state machines, and blocs.


<p align="center"> <img alt="Chickensoft.LogicBlocks" src="Chickensoft.LogicBlocks/icon.png" width="200"> </p>


Instead of elaborate transition tables, states are simply defined as self-contained class records that read like ordinary code using the state pattern. Logic blocks are designed with performance, adaptability, and error tolerance in mind, making them refactor-friendly and suitable for high performance scenarios (such as games).

Logic blocks grow with your code: you can start with a simple state machine and easily scale it into a nested, hierarchical statechart that represents a more complex system โ€” even while you're working out what the system should be.

Logic blocks are based on statecharts. You may also know them as hierarchical state machines (HSM's).

๐Ÿ’ก Example

A logic block is a class that receives inputs, maintains a single state instance, and produces outputs.

Logic blocks enable you to efficiently model complex behaviors[^1].

In v6, the logic block has no generic type parameter, states live outside the logic block class, and input handlers return Type instead of a Transition struct.

// LightSwitchLogic.cs
public partial class LightSwitchLogic : LogicBlock
{
  public LightSwitchLogic()
  {
    // Preallocate states
    Set(new LightSwitchState.PoweredOn());
    Set(new LightSwitchState.PoweredOff());
  }
}
public abstract partial record LightSwitchState : LogicBlockState
{
  public static class Input
  {
    public readonly record struct Toggle;
  }

  public static class Output
  {
    public readonly record struct StatusChanged(bool IsOn);
  }
}
// LightSwitchStates.cs
public partial record LightSwitchState
{
  public record PoweredOn : LightSwitchState, IGet<Input.Toggle>
  {
    public PoweredOn()
    {
      this.OnEnter(() => Output(new Output.StatusChanged(IsOn: true)));
    }

    public Type On(in Input.Toggle input) => To<PoweredOff>();
  }

  public record PoweredOff : LightSwitchState, IGet<Input.Toggle>
  {
    public PoweredOff()
    {
      this.OnEnter(() => Output(new Output.StatusChanged(IsOn: false)));
    }

    public Type On(in Input.Toggle input) => To<PoweredOn>();
  }
}
// Usage
using var logic = new LightSwitchLogic();
logic.Start<LightSwitchState.PoweredOff>();

logic.Input(new LightSwitchState.Input.Toggle());
// logic.State is now LightSwitchState.PoweredOn

๐Ÿ”— Bindings

Observe a logic block by creating a binding. Bindings are IDisposable โ€” dispose them when done.

using var binding = logic.Bind();

binding
  .OnState<LightSwitchState.PoweredOn>(_ => Console.WriteLine("Light is on"))
  .OnState<LightSwitchState.PoweredOff>(_ => Console.WriteLine("Light is off"))
  .OnOutput<LightSwitchState.Output.StatusChanged>(
    output => Console.WriteLine($"Status changed: {output.IsOn}")
  )
  .OnStart(() => Console.WriteLine("Started"))
  .OnStop(() => Console.WriteLine("Stopped"));

๐Ÿ–ผ๏ธ Visualizing Logic Blocks

LogicBlocks provides a source generator that can generate UML state diagrams of your code.

stateDiagram-v2

state "LightSwitch State" as Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State {
  state "PoweredOn" as Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOn
  state "PoweredOff" as Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOff
}

Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOff --> Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOn : Toggle
Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOn --> Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOff : Toggle

Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOff : OnEnter โ†’ StatusChanged
Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOn : OnEnter โ†’ StatusChanged

[*] --> Chickensoft_LogicBlocks_DiagramGenerator_Tests_TestCases_LightSwitch_State_PoweredOff

Add [StateDiagram] to your base state class to enable diagram generation. Generated *.g.puml files are placed alongside your code. You can use PlantUML (and/or the PlantUML VSCode Extension) to visualize them.

[StateDiagram]
public abstract partial record LightSwitchState : LogicBlockState { ... }

A diagram explains all of the high-level behavior of a state machine in a single picture. Without a diagram, you would have to read through every relevant code file to understand the machine.

๐Ÿ“œ History (Pushdown Automaton)

Logic blocks support a state history stack, enabling pushdown automaton behavior โ€” push the current state type and pop it later to return to a previous state.

public Type On(in Input.Pause input)
{
  Push();               // save current state type on the history stack
  return To<Paused>();
}

public Type On(in Input.Resume input)
{
  return Pop() ?? To<Playing>();  // restore previous state, or fall back
}

The history stack has a configurable maximum capacity (default: 8).

โšก Async Inputs

Bridge async tasks safely back into the synchronous input pipeline using Async():

public Type On(in Input.Load input)
{
  Async(FetchDataAsync())
    .Input(data => new Input.Loaded(data))
    .ErrorInput(ex => new Input.LoadFailed(ex.Message))
    .CanceledInput(() => new Input.LoadCanceled());

  return ToSelf();
}

๐Ÿ’พ Serialization (AutoBlock)

For logic blocks that need serialization, extend AutoBlock instead of LogicBlock. AutoBlock integrates with [Chickensoft.Introspection] and [Chickensoft.Serialization] to automatically discover and preallocate all concrete states, and to save/load state.

[Meta, Id("my_logic")]
public partial class MyLogic : AutoBlock
{
  public MyLogic()
  {
    Preallocate<MyState>(); // discover and preallocate all concrete states
  }

  public override ILogicBlockSaveData GetSaveData(LogicBlockData data) =>
    new MySaveData { Data = data };
}
// Save
var saveData = myLogic.Save();

// Load (resume from saved state)
myLogic.Start(saveData.Data);

๐Ÿงช Testing

Testing States

Test individual states in isolation using state.Test():

var state = new LightSwitchState.PoweredOff();
var tester = state.Test();

tester.Set(new SomeDependency());
state.Enter();

tester.Outputs.ShouldContain(new LightSwitchState.Output.StatusChanged(IsOn: false));

Testing Bindings

Use LogicBlock.CreateFakeBinding() to test binding callbacks without a real logic block:

using var binding = LogicBlock.CreateFakeBinding();

binding.OnState<LightSwitchState.PoweredOn>(_ => ranCallback = true);

binding.SetState(new LightSwitchState.PoweredOn());
ranCallback.ShouldBeTrue();

๐Ÿคซ Differences from Statecharts

In the interest of convenience, logic blocks have a few subtle differences from statecharts:

  • ๐Ÿ’‚โ€โ™€๏ธ No explicit guards

    Use conditional logic in an input handler

  • ๐Ÿชข Attach/Detach callbacks

    These are an implementation specific detail that are called whenever the state instance changes, as opposed to only being called when the state type hierarchy (i.e., state configuration) changes.

  • ๐Ÿ•ฐ๏ธ No event deferral

    Non-handled inputs are simply discarded. There's nothing to stop you from implementing input buffering on your own, though: you may even use the boxless queue collection that LogicBlocks uses internally.

LogicBlocks also uses different terms for some of the statechart concepts to make them more intuitive or disambiguate them from other C# terminology.

statecharts logic blocks
internal transition self transition
event input
action output

[^1]: Simple behaviors, like the light switch example, are considerably more verbose than they need to be. Logic blocks shine brightest when they're used for things that actually require hierarchical state machines.


Looking for more? Read the โœจ docs! โœจ


๐Ÿฃ Package generated from a ๐Ÿค Chickensoft Template โ€” https://chickensoft.games

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Chickensoft.LogicBlocks:

Package Downloads
Chickensoft.LogicBlocks.Auto

Automatic serialization and introspection support for LogicBlocks hierarchical state machines.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Chickensoft.LogicBlocks:

Repository Stars
chickensoft-games/GameDemo
The Chickensoft Game Demo โ€” a fully tested, third-person 3D game built with Godot and C#. Now with saving and loading!
Version Downloads Last Updated
6.0.0 130 6/2/2026
5.20.0 6,406 9/7/2025
5.19.1 1,229 6/30/2025
5.19.0 1,144 5/22/2025
5.18.0 661 4/12/2025
5.17.1 435 4/7/2025
5.17.0 351 3/31/2025
5.16.0 1,979 1/23/2025
5.15.0 471 1/10/2025
5.14.0 413 12/17/2024
5.13.0 562 11/26/2024
5.12.0 706 10/23/2024
5.11.1 265 10/23/2024
5.11.0 387 10/9/2024
5.10.0 295 10/4/2024
5.9.0 338 9/27/2024
5.8.0 335 9/17/2024
5.7.0 198 9/17/2024
5.6.0 433 9/4/2024
5.5.0 405 8/24/2024
Loading failed

LogicBlocks release.