{"id":30473890,"url":"https://github.com/corundumgames/stateless-for-unity","last_synced_at":"2025-09-23T11:14:16.901Z","repository":{"id":145199064,"uuid":"470305344","full_name":"CorundumGames/Stateless-For-Unity","owner":"CorundumGames","description":"A Unity port of Stateless that uses UniTask instead of standard TAP classes.","archived":false,"fork":false,"pushed_at":"2022-08-26T18:51:08.000Z","size":725,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-24T14:42:37.270Z","etag":null,"topics":["csharp","state-machine","stateless","unity"],"latest_commit_sha":null,"homepage":"https://corundumgames.github.io/Stateless-For-Unity","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CorundumGames.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-03-15T19:34:25.000Z","updated_at":"2025-08-19T12:42:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"3f5d6787-7bd1-4e0c-834b-3855587996c4","html_url":"https://github.com/CorundumGames/Stateless-For-Unity","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/CorundumGames/Stateless-For-Unity","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorundumGames%2FStateless-For-Unity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorundumGames%2FStateless-For-Unity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorundumGames%2FStateless-For-Unity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorundumGames%2FStateless-For-Unity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CorundumGames","download_url":"https://codeload.github.com/CorundumGames/Stateless-For-Unity/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorundumGames%2FStateless-For-Unity/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276562810,"owners_count":25664473,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-23T02:00:09.130Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","state-machine","stateless","unity"],"created_at":"2025-08-24T09:50:50.563Z","updated_at":"2025-09-23T11:14:16.895Z","avatar_url":"https://github.com/CorundumGames.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stateless For Unity\n\n[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/CorundumGames/Stateless-For-Unity/Test%20and%20Publish/main?logo=github\u0026style=for-the-badge)](https://github.com/CorundumGames/Stateless-For-Unity/actions)\n[![openupm](https://img.shields.io/npm/v/games.corundum.stateless-for-unity?label=openupm\u0026registry_uri=https://package.openupm.com\u0026style=for-the-badge)](https://openupm.com/packages/games.corundum.stateless-for-unity)\n[![Stack Overflow](https://img.shields.io/badge/stackoverflow-tag-orange.svg?logo=stackoverflow\u0026style=for-the-badge)](http://stackoverflow.com/questions/tagged/stateless-state-machine)\n\n**Create *state machines* and lightweight *state machine-based workflows* directly in .NET code:**\n\n```csharp\nvar phoneCall = new StateMachine\u003cState, Trigger\u003e(State.OffHook);\n\nphoneCall.Configure(State.OffHook)\n    .Permit(Trigger.CallDialled, State.Ringing);\n\nphoneCall.Configure(State.Connected)\n    .OnEntry(t =\u003e StartCallTimer())\n    .OnExit(t =\u003e StopCallTimer())\n    .InternalTransition(Trigger.MuteMicrophone, t =\u003e OnMute())\n    .InternalTransition(Trigger.UnmuteMicrophone, t =\u003e OnUnmute())\n    .InternalTransition\u003cint\u003e(_setVolumeTrigger, (volume, t) =\u003e OnSetVolume(volume))\n    .Permit(Trigger.LeftMessage, State.OffHook)\n    .Permit(Trigger.PlacedOnHold, State.OnHold);\n\n// ...\n\nphoneCall.Fire(Trigger.CallDialled);\nAssert.AreEqual(State.Ringing, phoneCall.State);\n```\n\nThis Unity-centric fork is not affiliated with the original [Stateless](https://github.com/dotnet-state-machine/stateless) or with Unity.\n\n## Features\n\nMost standard state machine constructs are supported:\n\n * Generic support for states and triggers of any .NET type (numbers, strings, enums, etc.)\n * Hierarchical states\n * Entry/exit actions for states\n * Guard clauses to support conditional transitions\n * Introspection\n\nSome useful extensions are also provided:\n\n * Ability to store state externally (for example, in a property tracked by an ORM)\n * Parameterised triggers\n * Reentrant states\n * Export to DOT graph\n\n### Unity Support\n\nInstall the package `games.corundum.stateless-for-unity` from OpenUPM through the instructions described [here](https://openupm.com/packages/games.corundum.stateless-for-unity/#modal-manualinstallation).\nIf you also install [`com.cysharp.unitask`](https://openupm.com/packages/com.cysharp.unitask),\nall [`async`](#async-triggers) APIs described below will be enabled.\n\nThis port of Stateless is largely the same as the original Stateless, including the API.\nYou can even continue to use [DOT graph output](#export-to-dot-graph) if you need it.\nThe primary difference is the use of [`UniTask`](https://github.com/Cysharp/UniTask) instead of [standard TAP classes](https://docs.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap)\n(e.g. [`Task`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task)).\nStandard `Task`s will still work in Unity, but UniTask is more optimized for the player loop.\n\n### Hierarchical States\n\n\nIn the example below, the `OnHold` state is a substate of the `Connected` state. This means that an `OnHold` call is still connected.\n\n```csharp\nphoneCall.Configure(State.OnHold)\n    .SubstateOf(State.Connected)\n    .Permit(Trigger.TakenOffHold, State.Connected)\n    .Permit(Trigger.PhoneHurledAgainstWall, State.PhoneDestroyed);\n```\n\nIn addition to the `StateMachine.State` property, which will report the precise current state, an `IsInState(State)` method is provided. `IsInState(State)` will take substates into account, so that if the example above was in the `OnHold` state, `IsInState(State.Connected)` would also evaluate to `true`.\n\n### Entry/Exit actions\n\nIn the example, the `StartCallTimer()` method will be executed when a call is connected. The `StopCallTimer()` will be executed when call completes (by either hanging up or hurling the phone against the wall.)\n\nThe call can move between the `Connected` and `OnHold` states without the `StartCallTimer()` and `StopCallTimer()` methods being called repeatedly because the `OnHold` state is a substate of the `Connected` state.\n\nEntry/Exit action handlers can be supplied with a parameter of type `Transition` that describes the trigger, source and destination states.\n\n### Internal transitions\n\nSometimes a trigger does needs to be handled, but the state shouldn't change. This is an internal transition. Use `InternalTransition` for this.\n\n### Initial state transitions\n\nA substate can be marked as initial state. When the state machine enters the super state it will also automatically enter the substate. This can be configured like this:\n\n```csharp\n    sm.Configure(State.B)\n        .InitialTransition(State.C);\n\n    sm.Configure(State.C)\n        .SubstateOf(State.B);\n```\n\nDue to Stateless' internal structure, it does not know when it is \"started\". This makes it impossible to handle an initial transition in the traditional way. It is possible to work around this limitation by adding a dummy initial state, and then use Activate() to \"start\" the state machine.\n\n```csharp\n    sm.Configure(InitialState)\n        .OnActivate(() =\u003e sm.Fire(LetsGo)))\n        .Permit(LetsGo, StateA)\n```\n\n\n### External State Storage\n\nStateless is designed to be embedded in various application models. For example, some ORMs place requirements upon where mapped data may be stored, and UI frameworks often require state to be stored in special \"bindable\" properties. To this end, the `StateMachine` constructor can accept function arguments that will be used to read and write the state values:\n\n```csharp\nvar stateMachine = new StateMachine\u003cState, Trigger\u003e(\n    () =\u003e myState.Value,\n    s =\u003e myState.Value = s);\n```\n\nIn this example the state machine will use the `myState` object for state storage.\n\nAnother example can be found in the JsonExample solution, located in the example folder. \n\n\n### Activation / Deactivation\n\nIt might be necessary to perform some code before storing the object state, and likewise when restoring the object state. Use `Deactivate` and `Activate` for this. Activation should only be called once before normal operation starts, and once before state storage. \n\n### Introspection\n\nThe state machine can provide a list of the triggers that can be successfully fired within the current state via the `StateMachine.PermittedTriggers` property. Use `StateMachine.GetInfo()` to retreive information about the state configuration.\n\n### Guard Clauses\n\nThe state machine will choose between multiple transitions based on guard clauses, e.g.:\n\n```csharp\nphoneCall.Configure(State.OffHook)\n    .PermitIf(Trigger.CallDialled, State.Ringing, () =\u003e IsValidNumber)\n    .PermitIf(Trigger.CallDialled, State.Beeping, () =\u003e !IsValidNumber);\n```\n\nGuard clauses within a state must be mutually exclusive (multiple guard clauses cannot be valid at the same time.) Substates can override transitions by respecifying them, however substates cannot disallow transitions that are allowed by the superstate.\n\nThe guard clauses will be evaluated whenever a trigger is fired. Guards should therefor be made side effect free.\n\n### Parameterised Triggers\n\nStrongly-typed parameters can be assigned to triggers:\n\n```csharp\nvar assignTrigger = stateMachine.SetTriggerParameters\u003cstring\u003e(Trigger.Assign);\n\nstateMachine.Configure(State.Assigned)\n    .OnEntryFrom(assignTrigger, email =\u003e OnAssigned(email));\n\nstateMachine.Fire(assignTrigger, \"joe@example.com\");\n```\n\nTrigger parameters can be used to dynamically select the destination state using the `PermitDynamic()` configuration method.\n\n### Ignored Transitions and Reentrant States\n\nFiring a trigger that does not have an allowed transition associated with it will cause an exception to be thrown.\n\nTo ignore triggers within certain states, use the `Ignore(TTrigger)` directive:\n\n```csharp\nphoneCall.Configure(State.Connected)\n    .Ignore(Trigger.CallDialled);\n```\n\nAlternatively, a state can be marked reentrant so its entry and exit actions will fire even when transitioning from/to itself:\n\n```csharp\nstateMachine.Configure(State.Assigned)\n    .PermitReentry(Trigger.Assigned)\n    .OnEntry(() =\u003e SendEmailToAssignee());\n```\n\nBy default, triggers must be ignored explicitly. To override Stateless's default behaviour of throwing an exception when an unhandled trigger is fired, configure the state machine using the `OnUnhandledTrigger` method:\n\n```csharp\nstateMachine.OnUnhandledTrigger((state, trigger) =\u003e { });\n```\n\n### State change notifications (events)\n\nStateless supports 3 types of state machine events:\n * Internal state transition \n * State transition\n * State machine transition completed\n\n#### Internal state transition \n```csharp\nstateMachine.OnInternalTransitioned((state, trigger) =\u003e { });\n```\n\nThis event will be invoked every time the state machine handles an internal state transition.\n\n#### State transition\n```csharp\nstateMachine.OnTransitioned((state, trigger) =\u003e { });\n```\nThis event will be invoked every time the state machine changes state.\n\n#### State machine transition completed\n```csharp\nstateMachine.OnTransitionCompleted((state, trigger) =\u003e { });\n```\nThis event will be invoked at the very end of the trigger handling, after the last  entry action have been executed.\n\n### Export to DOT graph\n\nIt can be useful to visualize state machines on runtime. With this approach the code is the authoritative source and state diagrams are by-products which are always up to date.\n \n```csharp\nphoneCall.Configure(State.OffHook)\n    .PermitIf(Trigger.CallDialled, State.Ringing, IsValidNumber);\n    \nstring graph = UmlDotGraph.Format(phoneCall.GetInfo());\n```\n\nThe `UmlDotGraph.Format()` method returns a string representation of the state machine in the [DOT graph language](https://en.wikipedia.org/wiki/DOT_(graph_description_language)), e.g.:\n\n```dot\ndigraph {\n  OffHook -\u003e Ringing [label=\"CallDialled [IsValidNumber]\"];\n}\n```\n\nThis can then be rendered by tools that support the DOT graph language, such as the [dot command line tool](http://www.graphviz.org/doc/info/command.html) from [graphviz.org](http://www.graphviz.org) or [viz.js](https://github.com/mdaines/viz.js). See http://www.webgraphviz.com for instant gratification.\nCommand line example: `dot -T pdf -o phoneCall.pdf phoneCall.dot` to generate a PDF file.\n\n### Async triggers\n\nIf [UniTask](https://github.com/Cysharp/UniTask) is installed, the `StateMachine` supports `async` entry/exit actions and so-on:\n\n```csharp\nstateMachine.Configure(State.Assigned)\n    .OnEntryAsync(async () =\u003e await SendEmailToAssignee());\n```\n\nAsynchronous handlers must be registered using the `*Async()` methods in these cases.\n\nTo fire a trigger that invokes asynchronous actions, the `FireAsync()` method must be used:\n\n```csharp\nawait stateMachine.FireAsync(Trigger.Assigned);\n```\n\n**Note:** while `StateMachine` may be used _asynchronously_, it remains single-threaded and may not be used _concurrently_ by multiple threads.\nBring your own synchronization.\n\n## Project Goals\n\nThis page is an almost-complete description of Stateless, and its explicit aim is to remain minimal.\n\nPlease use the issue tracker or the if you'd like to report problems or discuss features.\n\n(_Why the name? Stateless implements the set of rules regarding state transitions, but, at least when the delegate version of the constructor is used, doesn't maintain any internal state itself._)\n\n[Visual Studio 2015 and .NET Core]: https://www.microsoft.com/net/core\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorundumgames%2Fstateless-for-unity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorundumgames%2Fstateless-for-unity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorundumgames%2Fstateless-for-unity/lists"}