Search Unity

Finite State Machine (that works nicely with Inspector)

Discussion in 'Scripting' started by BigRoy, Oct 21, 2014.

  1. BigRoy

    BigRoy

    Joined:
    Jan 2, 2014
    Posts:
    12
    Hey guys,

    So I'm in the process of setting up a simple State machine class that I could re-use over the place.
    Coming from a non-game-dev background I'm a big fan of shuffling around values in the Inspector for debugging/prototyping.

    Some of my top priorities are to have it:
    • 1. Somewhat manageable through the inspector
      • Being able to debug/see what the current state is would be nice); I'm fine with pre-defining enum/states in code if not possible any other way!
    • 2. Object-oriented State Machine where the States are a class.
      • This just seems to be look so much cleaner than other stuff I've seen.
    • 3. Being able to change State easily with the new uGUI
      • Like easily set up a OnClick() to change a state to: "StateMainMenu". I could use 'strings' and parse it to Enum. But I would prefer something that would be linked more directly (referencing method/enum value). (Like one that works with a dropdown menu, as string seems so prone to human error)
    • 4. Nothing with "switch(state)", etc, etc. (Just doesn't feel modular enough to me)

    I found the Prime31 'StateKit' which seemed pretty sweet (looking at the code)! It seemed to cover point 1 and 3, but missed point 2 and 4.

    Feel free to point me to some nice pages showing examples, give me pointers on how to set up something that works nicely with the inspector. Or if you prefer something like PlayMaker over everything else feel free to convince me.

    At the moment I'm just looking to delve nicely into something can keep track of my states for UI and overall Game.

    Thanks in advance.

    -Roy

    EDIT: Not sure if it matters but my programming language of choice would be C#
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    This guy has a great tutorial (&code) for FSM, in my opinion. It does not use switches, and I did not try it with uGUI but I think it is worth a shot.
    I'd give you the website, but it seems to be down (it was up a few hours ago!)
    unitygems.com/

    Hope it helps!
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    PlayMaker. I love that thing. That's really only if you learn to write custom actions. Then you can do a lot with it.

    I've written a state machine before. Took me a while since it was my first time. Then I had to fix all the bugs with it. Then other team members had to learn my particular implementation of a state machine before they could work with it.

    IMO best to spend the $45 and be done with it. Unless you're doing it to learn and for practice in which case I highly recommend writing one!

    Screenshot is an example from my current game.
    Screenshot 2014-10-22 00.09.03.png

    With respect to the UI part of that, we use the Animator to control UIs here. I'm not sure how, I just feed the Animator some bools and ints, and another team member makes magic happen.
     
    Last edited: Oct 22, 2014
    TonyLi likes this.
  4. BigRoy

    BigRoy

    Joined:
    Jan 2, 2014
    Posts:
    12
    Thanks for the replies! :)
    Both are really informative.

    @MrPriest: I've seen the Unitygems.com one some time ago (I'll have a look again once it's back up!). Does that provide nice debugging/inspector functionality?

    @GarthSmith: Yeah. I know there are many people very happy with PlayMaker but somehow I'm also very interested in how to set up a nice FSM myself. (And what the best practices are there).

    If you're able to give some more insight on how your team is linking uyp the Animator with UI states I'd very happy to know about it! :D

    -Roy
     
  5. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    The FSM that is provided is just a class to use, you can probably add inspector functionality. I was thinking of doing that myself, but I might prefer to use PlayMaker instead - I don't have that much time for trial and error :) I do wish to do so one day, however.

    Also, thanks Garth Smith - I've just bought PlayMaker, it seems interesting, and is on sale, so why not.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've never felt tempted by PlayMaker before, as I thought it you pretty much had to build your whole game around it — in a sense, that you'd now be developing in PlayMaker, rather than developing in Unity.

    But that FSM editor looks pretty sweet. Is it possible to just use that, in an otherwise ordinary Unity game? Or does using any part of it drag in the whole ball of wax?
     
  7. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    My worries exactly!
    I won't try it tonight, but I will try it during the weekend. If it interests you, I'll tell you if I had to do any major modifications to make it playermaker-compatible, or if it was a simple integration.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    @BigRoy Here's another screenshot of an example UI. Each UI window has it's own layer in this Animator. I just set a couple of bools using Animator.SetBool() and that window becomes visible.

    I'm not sure how the windows tween in and out exactly. I have another team member who is able to set up the rest of the menu at that point.
    animatorUI.png

    @JoeStrout If you look in my PlayMaker screenshot from earlier, you see that the current state is asking the player a Yes/No question. This was a custom action coded by me that just sends a notification to our GuiManager to display the Yes/No pop up and get the information back. Most of our game does not run on PlayMaker. We use PlayMaker mostly for laying out AI decision making flow and for scripted events like that Merchant talking to the player.

    We do *not* use PlayMaker for things like inventory, saving the game to disk, building the game world, etc. We barely use the included PlayMaker actions at all. Almost every action we use is custom written.
     
    Last edited: Oct 22, 2014
    JoeStrout likes this.
  9. BigRoy

    BigRoy

    Joined:
    Jan 2, 2014
    Posts:
    12
    Great discussion about workflow/tools here. Really appreciate it! :)

    @Garth Smith: Thanks for the extra information. That sure looks like a nice suitable way to do it. I noticed nodeCanvas today and that one might also be an interesting tool for the job. Definitely since I'll likely customize a lot anyway I'm not too much interested in all the ready-to-go actions of PlayMaker. But I guess fellow artists (non-programmers) could enjoy that more... :)

    @MrPriest: Definitely keep us posted about how you like it!

    I'm still deciding on what the best next step is. Things are a bit busy with some other stuff so I'll likely be able to postpone a bit more (yay!) hehe... But once I delve deeper into actually integrating a FSM to my current project I'll definitely share my progress!
     
    GarthSmith likes this.