Implementing Finite State Machine using C++
-
Hi Guys!, I've been trying to make a simple tracking game but I'm having trouble just setting up the basic game states. I'm hoping to use a finite state machine(FSM) to switch between screens but every article iv read about them is different and sometimes they leave out details, I couldn't understand it properly. For example if I've two screens , Menu and Game screen. And in game I've to implement states for a simple tracking game. What would be a FSM look like to switch between them.??
-
Hi Guys!, I've been trying to make a simple tracking game but I'm having trouble just setting up the basic game states. I'm hoping to use a finite state machine(FSM) to switch between screens but every article iv read about them is different and sometimes they leave out details, I couldn't understand it properly. For example if I've two screens , Menu and Game screen. And in game I've to implement states for a simple tracking game. What would be a FSM look like to switch between them.??
Member 9350237 wrote:
I'm hoping to use a finite state machine(FSM) to switch between screens...
What are the game's states? Inputs?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Hi Guys!, I've been trying to make a simple tracking game but I'm having trouble just setting up the basic game states. I'm hoping to use a finite state machine(FSM) to switch between screens but every article iv read about them is different and sometimes they leave out details, I couldn't understand it properly. For example if I've two screens , Menu and Game screen. And in game I've to implement states for a simple tracking game. What would be a FSM look like to switch between them.??
Quote:
but every article iv read about them is different and sometimes they leave out details, I couldn't understand it properly.
They are different because FSM are widely used, for different purposes. There are not may details to understand. A finite state machine is a rather simple stuff. You should understand your scenarion, though. By wild guess, you could do something like (or go with a more OO approach)
struct FSMData
{
FSMState state;
// other useful data
};void update_fsm(FSMData & fsmd)
{
switch(fsmd.state)
{
case STATE_MENU:
handle_menu(fsmd);
break;
case STATE_GAME:
handle_game(fsmd);
//...
}void handle_menu(FSMData & fsmd)
{
//...
if ( userSelection() == PLAY_GAME)
{
// cleanup here
fsmd.state = STATE_GAME;
return;
}
// ...
}void handle_game(FSMData & fsmd)
{
// ...
if (userInput() == QUIT_GAME)
{
// cleanup here
fsmd.state = STATE_MENU;
return;
}
// ...
} -
Quote:
but every article iv read about them is different and sometimes they leave out details, I couldn't understand it properly.
They are different because FSM are widely used, for different purposes. There are not may details to understand. A finite state machine is a rather simple stuff. You should understand your scenarion, though. By wild guess, you could do something like (or go with a more OO approach)
struct FSMData
{
FSMState state;
// other useful data
};void update_fsm(FSMData & fsmd)
{
switch(fsmd.state)
{
case STATE_MENU:
handle_menu(fsmd);
break;
case STATE_GAME:
handle_game(fsmd);
//...
}void handle_menu(FSMData & fsmd)
{
//...
if ( userSelection() == PLAY_GAME)
{
// cleanup here
fsmd.state = STATE_GAME;
return;
}
// ...
}void handle_game(FSMData & fsmd)
{
// ...
if (userInput() == QUIT_GAME)
{
// cleanup here
fsmd.state = STATE_MENU;
return;
}
// ...
}Hi Guys, Thanks for your prompt feedback!!. I'm still bit confused on how to proceed with FSM. Basically it's a tracking game using a haptic device as controller. Game protocol: There will be a start point and stop(target) point . When the curser stay at start point for 5sec , it'll start and wait until curser reaches target. If the curser reaches the target in 10 sec, is the criteria to increment the score. this will will be the state to handle game. But how to script this these criteria??
-
Hi Guys, Thanks for your prompt feedback!!. I'm still bit confused on how to proceed with FSM. Basically it's a tracking game using a haptic device as controller. Game protocol: There will be a start point and stop(target) point . When the curser stay at start point for 5sec , it'll start and wait until curser reaches target. If the curser reaches the target in 10 sec, is the criteria to increment the score. this will will be the state to handle game. But how to script this these criteria??
CPallini has given you a basic example for a FSM - you will understand that we're not here to provide ready solutions. You have formulated your game states - what stops you from extending that sample with that, or, at least trying?