{"id":18647678,"url":"https://github.com/artcom/hsm-cs","last_synced_at":"2025-08-08T07:13:03.235Z","repository":{"id":142420082,"uuid":"127015273","full_name":"artcom/hsm-cs","owner":"artcom","description":"Herarchical Statemachine for C# / Unity3D","archived":false,"fork":false,"pushed_at":"2018-08-24T12:28:01.000Z","size":1268,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-11T18:32:46.579Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/artcom.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2018-03-27T16:25:30.000Z","updated_at":"2025-03-05T18:36:43.000Z","dependencies_parsed_at":"2024-04-14T02:31:06.457Z","dependency_job_id":null,"html_url":"https://github.com/artcom/hsm-cs","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/artcom/hsm-cs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fhsm-cs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fhsm-cs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fhsm-cs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fhsm-cs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artcom","download_url":"https://codeload.github.com/artcom/hsm-cs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fhsm-cs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269380278,"owners_count":24407605,"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-08-08T02:00:09.200Z","response_time":72,"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":[],"created_at":"2024-11-07T06:27:25.174Z","updated_at":"2025-08-08T07:13:03.214Z","avatar_url":"https://github.com/artcom.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HSM for Unity\n\n## General\n\nA hierarchical statemachine for Unity implemented in C#\n\n![image](doc/exports/advanced.png)\n\n## Installation\n\nTo install this library add `hsm` to your `bower.json` file and run `bower install`.\n\n## States and State Machines\n\n### Setup\n\n![image](doc/exports/simple.png)\n\nImport the hsm:\n\n```cs\nusing hsm;\n```\n\nStates are specific by creating `Hsm.State` instances. They are then passed to the `Hsm.StateMachine` constructor. By convention, the first state passed is the initial state:\n\n```cs\nState a1 = new State(\"a1\");\nState a2 = new State(\"a2\");\nState a3 = new State(\"a3\");\n\nStateMachine a = new StateMachine(a1, a2, a3);\n```\n\nAlternatively the state machine can also be constructed by passing `State` instances as arguments:\n\n```cs\nStateMachine sm = new StateMachine(\n    new State(\"a1\"),\n    new State(\"a2\"),\n    new State(\"a3\")\n);\n```\n\nOr by adding them separately:\n\n```cs\nStateMachine sm = new StateMachine();\nsm.AddState(new State(\"a1\"));\nsm.AddState(new State(\"a2\"));\nsm.AddState(new State(\"a3\"));\n```\n\nA fluent interface is provided here too:\n\n```cs\nStateMachine sm = new StateMachine()\n.AddState(new State(\"a1\"))\n.AddState(new State(\"a2\"))\n.AddState(new State(\"a3\"));\n```\n\n### Initialization\n\nThe state machine is then initialized by calling:\n\n```cs\nsm.setup();\n```\n\nThis starts the state machine, activates the initial state(s) and is now ready to handle events.\n\n## Entry and Exit handlers\n\nEach state can have an `enter` and an `exit`. They will be invoked when a state is entered or exited:\n\n```cs\nState a = new State(\"a\")\n.OnEnter((data) =\u003e {\n    // your code here\n})\n.OnExit((data) =\u003e {\n    // your code here\n});\n```\n\nOptionally you can receive source and target state as well:\n\n```cs\nState a = new State(\"a\")\n.OnEnter((source, target, data) =\u003e {\n    // your code here\n})\n.OnExit((source, target, data) =\u003e {\n    // your code here\n});\n```\n\n## Actions and State Transitions\n\n![image](doc/exports/simpleWithTransition.png)\n\nEach state has a map of event handlers which will be called when the state receives the corresponding event. Event handlers are added to the handlers list of each state by passing event name, target state and an optional action:\n\n```cs\na3.AddHandler(\"T3\", a2);\na3.AddHandler(\"T3\", a2, data =\u003e {\n    // your action\n});\n```\n\n## External, Internal and Local Transitions\n\n![image](doc/exports/simpleWithInternalAndLocalTransition.png)\n\nExternal, internal and local transition kinds can be specified via the `TransitionKind` enum. External transitions are used by default. When using internal transitions the target state must be the similar to the handling state.\n\n```cs\na3.AddHandler(\"T3\", a2); // external by default\na3.AddHandler(\"T3\", a2, Transition.External);\n\na.AddHandler(\"T4\", a2, Transition.Local);\na1.AddHandler(\"TI\", a1, Transition.Internal, data =\u003e {\n    // action\n});\n```\n\n## Guard Conditions\n\n![image](doc/exports/simpleParallelGuarded.png)\n\nA Transition can be guarded by a specified condition:\n\n```cs\nc11.AddHandler(\"T5\", c12, data =\u003e {\n    return (data[\"v\"] == null);\n});\nc21.AddHandler(\"T5\", c22, data =\u003e {\n    return (data[\"v\"] != null);\n});\n```\n\n## Sub-StateMachines (Nested)\n\n![image](doc/exports/simpleSub.png)\n\nState machines can be nested in other state machines by using the `Hsm.Sub` adapter class. All events are propagated into the state's sub-state machine, and the sub-state machine is initialized and torn down on enter/exit of its containing state:\n\n```cs\nSub a = new Sub(\"a\", new StateMachine(\n    new State(a1),\n    new State(a2),\n    new State(a3)\n));\n```\n\nFor more details on how to construct a Hsm.Sub consult the [tests](lib/hsm/Editor/tests/testSubmachine.cs).\n\n## Parallel State-Machines (Orthogonal Regions)\n\n![image](doc/exports/simpleParallel.png)\n\nParallel state machines are constructed with using `Hsm.Parallel` adapter class. All events are propagated to all orthogonal state machines contained in the `Hsm.Parallel`. An event is treated as handled as soon as one of those state machines handles an event succesfully.\n\n```cs\nParallel c = new Parallel(\"c\",\n    new StateMachine(\n        new State(\"c11\"),\n        new State(\"c12\")\n    ),\n    new StateMachine(\n        new State(\"c21\"),\n        new State(\"c22\")\n    )\n);\n```\n\n## Debugging\n\nTo get the current active state configuration call:\n\n```cs\nList\u003cstring\u003e = sm.getActiveStateConfiguration();\n```\n\n## Development Setup\n\nTo run the test suite:\n\n```sh\n$Unity_APP -runTests -projectPath `pwd`/example_project -testResults ../reports/editormodeTests.xml -testPlatform editmode -batchmode -logFile\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartcom%2Fhsm-cs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartcom%2Fhsm-cs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartcom%2Fhsm-cs/lists"}