Game Development Community

dev|Pro Game Development Curriculum

FSM in TGB is done!

by Gustavo Boni · 01/23/2007 (9:44 am) · 6 comments

In my last plan i talked about how i was trying to create a scriptable Finite State Machine in TGB. It was just some words about how i was trying to figure out a way to use that in the script-side. I was so excited that i forgot the rule #1: "No pics, no blog". Now, the FSM is done and i can show you something. ^^

img261.imageshack.us/img261/7701/008000001jm.pngPS: using Adventure Kit art asset

It's a very simple scene: at the top left is the warrior's house and at the bottom right is the bank. Basically the warrior has four states: Sleeping, Eating, Looking Treasure and Banking.
The state diagram for the warrior is the following: (The red square shows two states i didn't create yet.)

img262.imageshack.us/img262/208/statediagram4av.jpg
The initial state is sleeping, so when i start the game, the warrior go to your house to sleep and recover your atributes (health, fatigue and hungry level). The next state is "Eating", and when "hungry" become full, the FSM changes to the "Looking for Teasure" state. If the warrior found the treasure, the state is changed to "Banking", if the warrior "hungry" is very low, it return to the "Eating", the same with the "Fatigue" but it returns to the "Sleeping" state.

All the state logic is done like this:

function lookingforteasure::onExecuteState(%this, %var)
{
   if($warrior.hungry <= 95)
   {
      warriorStateInfo.setText("I'm hungry, need to have lunch");
      return $TRANSITION_LOOKINGFORTEASURE_EATING;
   }
   else
   {
      if($warrior.fatigue <= 90)
      {
         warriorStateInfo.setText("I'm tired, need to go sleep");
         return $TRANSITION_LOOKINGFORTEASURE_SLEEPING;
      }
      else
      {
         if(getRandom(1,100) <= 20)
         {
            //Found Treasure!
            return $TRANSITION_LOOKINGFORTEASURE_BANKING;
         }
      }
   }
   
   return $TRANSITION_DONT_CHANGE;
}

This is the reason I create the states in script. If you have suggestions, let me know, please. =)

It's all. Hope you liked it ^^

The next step is Messaging.

Thanks,
Gustavo Boni

#1
01/23/2007 (9:57 am)
Wow this is pretty cool :)
#2
01/24/2007 (12:13 am)
Nice nice!

Very interesting stuff :)
#3
01/24/2007 (4:46 am)
@Tim: Thanks! =)

@OneST8: Thanks too man! =)
#4
01/24/2007 (8:26 am)
Cool stuff. This looks remotely similar to my TorqueScript FSM superclass, with some minor differences. Mine is fueld by TorqueScript hackery. All states for an FSM are on the FSM's namespace and the state's are hashed. The only optomization I have in mind is to hash all the FSM types in a global singleton so there's only one instance of each type, but it's not too shabby for as small as it is.

Maybe we could collaborate to find a best solution and then post a resource for the community. Email me if you're interested.
#5
01/24/2007 (7:05 pm)
@Thomas: i've just sent you an email.
#6
04/24/2007 (8:43 pm)
I noticed your using the Adventure Kit art asset.. doesnt it have goal driven ai built into it?
Well im about to download it, so we will see!