Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What FSM Library do you use?

Discussion in 'Scripting' started by zee_ola05, Jun 25, 2014.

  1. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Hi.

    I use state machines most of the time in my projects. However, I usually just write my own, very simple, state machine class. And it usually fails me and leads me to bad design compromises. Since it is very popular, there must be something out there that has already been tried and tested by many.

    Basically, what I need is an FSM library that is capable of having sub-states. Being able to pause the current state, execute the substate, then resume back to the current state.

    So for this thread, I have two main questions:
    1. What FSM Library do you use?
    2. Do you know a library that does what I described?

    Thanks!
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I highly suggest PlayMaker.
    http://www.hutonggames.com/

    It's well supported. They communicate on the forums. Widely used. Battle tested. Especially powerful if you know C#. Yo dawg, we heard you like FSMs so you can put sub-FSMs in your FSMs. We've used it in every project since we got it. It's #5 Top Grossing on the Unity Assets Store as I type.

    I'm not affiliated with them, but super happy if you couldn't tell. =)
     
    Last edited: Jun 25, 2014
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Unity gems has a great three part series where they provide all the source code. I've found the final version is actually really well implemented, not sure how it compares to whatever you were writing though!
     
  4. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    I use a simple one from the book Programming Game AI by Example. I originally converted the C++ code to C# for an XNA game (which wasn't too tough). The code came straight over into Unity no problem.

    It's not particularly sophisticated, but that's what I like about it. It's easy to understand and to extend. What I especially love about it is how you structure things. Each state is its own class with a transition into method, execute method, and transition out method. Each entity (like an AI enemy, menu, whatever) owns a state machine and passes itself into the singleton states. Keeps things really encapsulated. There's a global state and the ability to revert to whatever the previous state was (not sure if that's similar to your sub-state concept).

    I've beefed it up slightly to allow states to be pushed and popped off of a queue, which can be handy for certain types of menus.

    I've messed with Playmaker for just a bit, but I prefer a more code-centric approach.
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Not to hijack zee's thread, but how do you deal with timing within your FSM? Do you use coroutines in your transition in/out methods, and then send a "complete" message when they finish? Or does a state's out method overlap with the next states in method?
     
  6. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    I use NodeCanvas for FSM and Behavior Trees and Dialogue Trees. All in one solution for AI and Cutscene.

    http://nodecanvas.com/

    You can write your own code to extend it. Very easy and well supported by the Developer.

    You cant get wrong when you link nodes visually, thats why I recommend NodeCanvas.
     
  7. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    The code I'm using uses an Enter and Exit method for each state that's more intended as just an instant initialization or cleanup. So they wouldn't be used for timed transitions. For transitions that take X amount of time, I typically use a separate state or just handle it manually within the "execute" part of the state (by checking timers on the entity). That's possibly not the most elegant way to do it, though I guess I haven't yet run into that too much.

    If there are FSM's that handle that sort of timing stuff, that could be an advantage over the simple one that I've been using. I suppose if I run into the need to do fancier stuff more, I should look at other options. I do own PlayMaker and NodeCanvas; just haven't taken the time to dig into either of those. I tend to shy away from systems that require a lot of dragging boxes around (I'd rather type in exactly what I want and move on), but I should check both of those out more.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    I second PlayMaker. It's solid, widely-supported, and easy to understand and trace at runtime because it's visual.

    If you really want an FSM framework that's only code, Lost Zombie's FSM has worked well for me.
     
  9. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    YES! I'm actually using FSM used in the part 2. I am trying to understand fully how he implements Callable states in his part 3 tutorial. I really like his implementation. :)
     
  10. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    I toyed with PlayMaker for a few hours. I don't like it too much, primarily because its states aren't MonoBehaviour. Specifically, it was because I had trouble integrating FingerGesture with it. It may be because I'm just stupid but I will try PlayMaker sometime again.

    I like the idea of pushing and popping states. I'd love to try Lost Zombie's FSM. Too bad, it isn't free. Maybe next time when I got a few spare bucks.
     
  11. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    This is actually 2nd implementation I've adapted. Most of my FSMs are based on this book. It is so good and easy to understand. However, I like to cross "basic" so I'm looking for a little more sophisticated. So I'm looking for something that supports sub-states.
     
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    You're not stupid, it just takes some time to familiarize. It's usually pretty straightforward to communicate between Playmaker FSM's and other scripts. Check out the Script Control action category. I think there are official FingerGestures actions from the FG author, though I haven't checked them out myself. You can also drop questions on the Playmaker forum if you have trouble with something, there's a lot of activity there on a daily basis.
     
  13. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Thank you for these links. I will definitely check these out.
     
  14. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    I think doing timing within the Execute probably fits the paradigm best anyways. If you make the enter/exit non-instantaneous, I could see issues arising because it might be hard to tell what state you're actually in. Thanks for the reply.