<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jussi Tuomi</title>
    <description>The latest articles on DEV Community by Jussi Tuomi (@jushii).</description>
    <link>https://dev.to/jushii</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F109260%2Fd9b1cdd2-0cc1-47b5-b11e-be6639c3d1b4.png</url>
      <title>DEV Community: Jussi Tuomi</title>
      <link>https://dev.to/jushii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jushii"/>
    <language>en</language>
    <item>
      <title>State machine controller for Unity</title>
      <dc:creator>Jussi Tuomi</dc:creator>
      <pubDate>Tue, 23 Oct 2018 13:09:58 +0000</pubDate>
      <link>https://dev.to/jushii/state-machine-controller-for-unity-4fno</link>
      <guid>https://dev.to/jushii/state-machine-controller-for-unity-4fno</guid>
      <description>&lt;p&gt;On my free time I love to create game prototypes using C# in Unity. One of these prototypes is a turn-based game where I need to have multiple game states and a fine control over them.&lt;/p&gt;

&lt;p&gt;I started working on a typical finite state machine (FSM) for controlling the game scenario and player input in different game modes, such as &lt;code&gt;battle&lt;/code&gt; and &lt;code&gt;exploration&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First the state machine looked something like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;StateMachine&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;StateExplorationUnitSelected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateExplorationUnitMoving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateBattleUnitSelected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateBattleUnitMoving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateBattleSelectingSkill&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateBattleCastingSkill&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Immediately I started to stress about how bloated the single state machine would become as the codebase grows in size. This is when I divided it in two state machines:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ExplorationStateMachine&lt;/th&gt;
&lt;th&gt;BattleStateMachine&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;StateExplorationUnitSelected&lt;/td&gt;
&lt;td&gt;StateBattleUnitSelected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateExplorationUnitMoving&lt;/td&gt;
&lt;td&gt;StateBattleUnitMoving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;StateBattleSelectingSkill&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;StateBattleCastingSkill&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;After dividing the state machine in two, I wanted to have the ability to bundle related state machines together and allow their states to change the active state machine based on what happens in the game world. For example if player encounters an enemy in &lt;code&gt;StateExplorationMoving&lt;/code&gt; - that state can call &lt;code&gt;ChangeStateMachine(BattleStateMachine);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I created a parent state machine class that can hold a collection of child state machines:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PlayerStateMachine&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ExplorationStateMachine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BattleStateMachine&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the above table &lt;code&gt;PlayerStateMachine&lt;/code&gt; is a parent state machine for controlling child state machines. &lt;code&gt;ExplorationStateMachine&lt;/code&gt; and &lt;code&gt;BattleStateMachine&lt;/code&gt; can be considered as the states of the &lt;code&gt;PlayerStateMachine&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After having this structure in place, I created a StateMachineController class that holds a list of parent state machines and runs their update methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y5nB5ygw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/W9V6xEA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y5nB5ygw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/W9V6xEA.png" alt="StateMachineController.cs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I extracted the code from my prototype and started editing it to be more generic. You are free to use it for your own Unity projects if you want - and feel free to create a pull request if you have ideas to make it better. The project can be found from &lt;a href="https://github.com/jushii/StateMachineController"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>csharp</category>
      <category>unity3d</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
