Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[RELEASED] Logic StateMachines - Easily built State Machines in the Animator

Discussion in 'Assets and Asset Store' started by MxShepherd, Jul 27, 2018.

  1. MxShepherd

    MxShepherd

    Joined:
    Feb 19, 2017
    Posts:
    9
    Logic StateMachines

    I am happy to have my first asset released and to share my work with you! I have created a kind of visual scripting editor extension for Unity’s Animator system, so you can easily plug in your own code to animation states.




    Motivation

    While developing apps I always found it hard to decide on a satisfying pattern in which all my classes and functions are connected with each other or even where I should set the entry point. I have used to create some kind of “GameManager” classes that will handle the flow of my program, but sooner or later it got all messy and it would have been even worse for others or my future-self to understand my code without a proper documentation.

    The concept of finite state machines therefore is a neat way to structure and sort the logic of your program, but still there was that issue of those uncountable lines of code that emerge over time. It would need a proper documentation to stay maintainable and honestly: this is no fun part of game development. So, it would be great to establish a kind of documentation on the fly while you are just programming and designing your app.

    There are already visual scripting tools available which combine the concept of state machines and create a kind of documentation by their visual part, but I did not really need a tool to express “if(a > b) { … }”. As a programmer, I wished for something more generic, to control and visualize the overall flow and logic of my code and where I could then plug in my own functions.

    This is where I decided to create my own tool to fill those needs and after my colleagues have encouraged me to share my work, I have finally become an Asset Store publisher :)

    What it does

    I have created a small but powerful editor which relies on Unity’s Animator system (which is also a state machine btw.). In the Animator window you can create empty animation states and sub-state machines which you can connect to each other to enable transitions. This way you can build the different stages / phases / states (call it whatever you want) of your program. It is quite like visual scripting. You can see directly the intention and connections of your program and you can easily sketch around and plan ahead in which direction the code will flow. But this is nothing new in Unity, right? On those states and sub-states mentioned before you can add Components, so called “StateMachineBehaviour”. They decide, what will happen when you enter / exit a state or while the update on this state is running. But creating those components and filling them with logic is quite time consuming and requires a lot more of asset managing skills, because for every state you create you will also have to write a new script-component. Sounds bad, doesn’t it? At least this is what I thought. So I made this generic component, which you can add to any state or sub-state and then you just define

    1.) the state phase

    2.) which function in your program to call (with up to 9 types of input parameters)

    3.) under which conditions (optional)

    4.) and some else-statements (optional)

    That is all and you are ready to go. The nifty part of it is, that you can decide if you either design the big and overall states of your app or if you just want to create states with short and quick aspects of it. You can even set up your states on multiple layers, to run independent logic-islands in parallel.

    Overall concept (possible approach)

    So, how to use it?

    At first, just go and design your game! Populate the scene with your awesome assets and write those clever functions to put life into them. But now don’t worry where to get the ball rolling and where to store all the overall dependencies and interactions. First think about the different categories you will have to manage, e.g. your UI, levels, the player’s profile, etc. Now create distinguished controller classes as Monobehaviours where you then reference the target objects in your scene. In these classes, you put or link those clever functions, that will do their job on the referenced objects. Now, go to the Animator window to create the state machines, add the “CustomStateAction” Component delivered with my tool and set everything up to call the functions in your controller classes.

    For using functions that take input parameters, the following types are currently supported:
    • bool
    • int
    • float
    • string
    • Vector2
    • Vector3
    • Color
    • Material
    • Texture
    • Sprite
    • AudioClip
    • GameObject (as Prefab)
    Keep in mind, that Components on Animation states can only keep references to assets from your project folder, but not to scene objects. In order to reference scene objects with your functions called from the Animation States, set up the required references on your Monobehaviour Controller objects in the scene itself.

    Epilog

    The subject of statemachines and the classification of program logic is something that I find quite hard to explain, especially because I am no English native speaker. So, I am very grateful about any questions or suggestions you have so I can improve the description of my work.


    Further you can follow me on Twitter @ManasparkStudio

    or just write me an email: Max.schaefer@manaspark.studio

    Cheers.

    TL;DR: I have released my first asset, a kind of visual scripting editor extension. It extends Unity’s Animator system so you can easily plug in your own code to animation states.
     
    Last edited: Jul 27, 2018
    ZhavShaw likes this.
  2. MxShepherd

    MxShepherd

    Joined:
    Feb 19, 2017
    Posts:
    9
    For everyone who is interested in using my tool:

    Consider to take part in my small tombola and help me to promote it or write me a comment / pm.

    Thank you.