Search Unity

Finite State Machine Project

Discussion in 'Scripting' started by dart, Jun 27, 2010.

  1. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    I don´t know if the best place to post this is here or in the showcase, but here it is.

    I had to program a Finite State Machine to a project I´m working on right now, and I decided to share the class here since it´s very simple to include in many projects and makes the code cleanner than using lots of ifs and switches. It´s not as general and powerfull as behavior trees, but FSM is used by the game industry since PacMan, and for small projects it´s still very usefull.

    It´s based on chapter 3.1 of Game Programming Gems 1, written by Eric Dybsand, but coded in C#.

    I also implemented a complete project with a simple NPC to show how to use it. The NPC follows a path of waypoints. If it sees the player, it starts chasing him and returns following the path if the player moves too far away from him.

    If you have any question about the class or the project, just ask.
     

    Attached Files:

  2. TakuanDaikon

    TakuanDaikon

    Joined:
    Jun 6, 2010
    Posts:
    268
    Thank you for that. I am in the process of building an FSM myself, based on the one in AI for Games (with ideas from the GPG series as well), but this may just have saved me the effort. I look forward to poking around the code :)

    .
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Thanks, I was just about to dive into creating something similar from scratch... no need to reinvent the wheel when you have it layed out. Thanks again.
     
  4. beloto

    beloto

    Joined:
    May 9, 2010
    Posts:
    1
    Thanks.

    Do you have this one in JS.

    And if you have it where can be found?

    Thanks in advance.
     
  5. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,060
    Very nice Dart! You should post this on the wiki.
     
  6. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    Thanx. Very useful.
     
  7. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    Thanks guys.

    Quietus, I will put this on the wiki in the next few days.

    Beloto, right now, I don´t know much about JS Containers to port this code. I will check the documentation and see if it´s easy to port. Any help porting is very welcome.
     
  8. GusM

    GusM

    Joined:
    Aug 27, 2005
    Posts:
    585
    Very nice simple AI, thanks for sharing.

    BTW, I tried it in Unity Iphone and I got some errors. But my scripting knowledge is too poor for even understand the errors. Anybody can try it out, please?

    1 Parsing error in FSMSystem.cs (83,17)

    2 errors expecting ";" in NPCControl.cs (61,46) and (109,42)

    Thanks.
     
  9. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    The FSM framework is on the wiki now. I optimized some parts of the code, and include 2 other methods to make the FSMState more flexible.

    http://www.unifycommunity.com/wiki/index.php?title=Finite_State_Machine


    I don´t have Unity for iPhone and I don´t know how the Mono compiller works to this plataform, but all the errors were on lines with Generics. Maybe if you use the "old" sintax it fixes the problem:

    Code (csharp):
    1. (npc.GetComponent(typeof(NPCControl)) as NPCControl).SetTransition(Transition.LostPlayer);
    But you´ll have to rewrite all the code, since I use the generic way of getting a component from an object.
     
  10. JDonavan

    JDonavan

    Joined:
    Oct 3, 2009
    Posts:
    105
    Check your player settings and make sure the Api Compatibility level is set to .NET 2.1. I've not tried this code but the error you're getting strikes me as what you'd see if you had the compatibility level set to .NET 1.1.
     
  11. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,090
    thanks for sharing this ;)
     
  12. wingrider888

    wingrider888

    Joined:
    Oct 27, 2010
    Posts:
    38
    Awesome stuff!

    Just one question, if i had to modify this FSM to be able to implement each state as a stack and have a pop push feature(Stack based FSM) how would i go about it?
     
    Last edited: Dec 7, 2010
  13. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Thank you, less module that I have to do it myself. Regarding, convert C# to JavaScript, I would do it if it is OK with you, Mr. dart.
     
  14. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    wingrider888, I think what you need is a stack or a pushdown machine, and the kind of computations a pushdown machine solves are different from the ones a FSM solves. Maybe this will help you understand what I´m talking about http://en.wikipedia.org/wiki/Pushdown_automaton

    You can change the Dictionary member of FSMSystem class to a Stack class(http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx), change AddState and DeleteState to PushState and PopState (you´ll have to change the code because in a Dictionary, you can insert or delete any member, not just the highest one on the stack). I´m not even sure the function PerformTransition will be usefull anymore in this case.

    MightyMao, go ahead and port to JavaScript the project but share the code with the community. Thanks in advance for your help
     
    Last edited: Dec 7, 2010
  15. luisanton

    luisanton

    Joined:
    Aug 25, 2009
    Posts:
    323
    It seems to be the FSM week! I've just coded mine in C# from the design that Mat Buckland offers in Programming AI by Example. Its main advantage is that state transitions are embedded in each state. As each state is a class, things are easy to control if you keep each class and its FSM in the proper folder.

    There are so many solutions for FSMs that it's difficult to choose one! Which model is 'the best'? I guess it depends on context...
     
  16. Mike Stampone

    Mike Stampone

    Joined:
    Dec 8, 2010
    Posts:
    21
    Post-Turing Machine!

    Where each rule is a four tuple

    <state, symbol, new state, action>

    and action is new symbol, move left, or move right.
     
  17. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    That´s exactly what I´m doing: each state is a class. Eric Dybsand and Mat Buckland used the State Pattern to implement their version of FSM, so they all use the same idea.


    That depends on a lot of factors and not all of them are technical, like budget, time to release the project, plataform you are developing to, even the type of game. There´s no universal solution
     
  18. brad_ict

    brad_ict

    Joined:
    Sep 14, 2010
    Posts:
    69
    The connections between the assets and scripts are broken right now because the Library folder isn't included in the zip; it's just the Assets folder. Can you update the zip to include the Library folder?
     
  19. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    Thanks for doing this and for the book recommendation, I just picked it up!

    I'm just getting into AI and really having fun with UnitySteer. I'd like to try out Angry ants behavior tree, and I'm curious, what is the difference between a FSM and a behavior tree? Are they essentially the same, or would one use both?

    Bryan
     
  20. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211

    Project updated, but I think it was made with Unity 2.6. You´ll have to convert it to Unity 3. Check this link too.

    http://www.unifycommunity.com/wiki/i..._State_Machine
     

    Attached Files:

    Last edited: Dec 15, 2010
  21. newborne

    newborne

    Joined:
    Jul 16, 2012
    Posts:
    31
    does someone uses this? and is there more example of it since i am just a beginner!
     
  22. mrferrys

    mrferrys

    Joined:
    Feb 5, 2014
    Posts:
    7
    Dart thank you for this very useful script.
    I modified the code to make it generic.(<T transitions, E states>).

    Dart version

    Generic FSM (See Attached File)
     

    Attached Files:

unityunity