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.
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
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.
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)
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:
(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.
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.
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?
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 Pushdown automaton - Wikipedia
You can change the Dictionary member of FSMSystem class to a Stack class(Stack Class (System.Collections) | Microsoft Learn), 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
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…
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
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?
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?