Previous Blog Next Blog
Prev/Next Blog
by date

Finite State Machine in TGB for RPG Games

Finite State Machine in TGB for RPG Games
Name:Gustavo Boni
Date Posted:Jan 17, 2007
Rating:4.0 out of 5
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Gustavo Boni

Blog post
Wow! This is my first blog! In the first place i want to thanks all the community for the help in the forums to learn Torque, really appreciate that.

So, let's talk about what i'm doing with Finite State Machines (FSM)^^

Months ago i created an FSM class implementation in c++ to see how it works following the excellent AI book Game Programming AI By Example. It worked nicely and i wondered how good it will be to have it working in TGB.
Thus, i thought in a prototype of a RPG game, where you would have a Warrior, an Monster and a Banker guy.

Each of them will have own states like: Eating, Sleeping, Fighting, etc. The FSM will handle all the actions making the Warrior go back to home to sleep if tired, for example.

The script code will be very simple. Basically you'll have to create a FSM to the player:


%myFSM = new FSM();


And then, create your states:


%sleeping = new State(sleeping);


This is in the very beggining yet, i'll try to keep it as simple as i can.

Hope in the next blog show you some screens of this working too.

Thank you all for reading,

Gustavo Boni

Recent Blog Posts
List:01/23/07 - FSM in TGB is done!
01/17/07 - Finite State Machine in TGB for RPG Games

Submit ResourceSubmit your own resources!

Chip Lambert   (Jan 17, 2007 at 15:24 GMT)
I'm very intrigued with this Gustavo. Keep us updated!

Matt Huston   (Jan 17, 2007 at 15:30 GMT)
Funny coincidence because I was looking at this book just yesterday and thinking of AI for RPGs.

Phil Carlisle   (Jan 17, 2007 at 15:41 GMT)
I'd suggest having the FSM object be a scriptable class, but have the states as methods/accessor functions on the FSM class, mainly because there's no need to create a "state" object in script. You can add/update/remove state objects using the FSM's methods then.

i.e.

%myFSM = new FSM();
%myFSM.AddState("Resting");
%myFSM.AddState("Fighting");

You get the point. Its hard to keep the balance right when looking at torquescript vs C++, but I've found that the above interface works pretty well (i.e. the FSM is the scriptable object, everything else is managed by it).

Gustavo Boni   (Jan 17, 2007 at 15:55 GMT)   Resource Rating: 5
@Chip Lambert: ok Chip, thanks man ^^

@Phil Carlisle: yup, i got it Phil, but the reason to have the state in script is because eith the state object you can create the logic for this states. I mean, you will have 3 methods: onEntryState(), onExecuteState(), onExitState().

So, lets do it with the "sleeping" state.


function sleeping::onEntryState(%this, %var)
{
echo("I'm tired....");
}

function sleeping::onExecuteState(%this, %var)
{
if(tired)
{
return $TRANSITION_FIGHTING_SLEEPING;
}
}

function sleeping:onExitState(%this, %var)
{
echo("I have to wake up now... that sucks...");
}


Oliver Rendelmann - DerR   (Jan 17, 2007 at 16:44 GMT)
I own this book aswell. It's great! :) Keep it up.

Michael Cozzolino   (Jan 17, 2007 at 20:42 GMT)
I own this book too. Unfortuanately I haven't had much time to spend with it.

Martin Schultz   (Jan 17, 2007 at 21:01 GMT)
I own this book too :-) Keeping the logic in state objects make totally sense :-)

Phil Carlisle   (Jan 17, 2007 at 22:12 GMT)
Gustavo: Well, you could do those callbacks from the C++ object. I added a state entry into my FSM C++ class that basically had entry, update, exit script callbacks.

So I could have

FSM.addState("onEnter","onUpdate","onExit","MyStateName");

It worked fine.. but I guess each to thier own.

Gustavo Boni   (Jan 18, 2007 at 00:09 GMT)   Resource Rating: 5
@Phil: interesting way... but you should implement the callbacks in c++ code right? it's interesting, but i'm trying to let the developer create that in script, dont allowing him to tocuh the c+ code.

Tom Bentz   (Jan 18, 2007 at 05:45 GMT)
Interesting... do you have plans to release this as a resource?

Oliver Rendelmann - DerR   (Jan 18, 2007 at 06:29 GMT)
@Gustavo: I think Phil meant to call the TorqueScript functions you deliver from C++ which is perfectly possible.

You must be a member and be logged in to either append comments or rate this resource.