Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Implementing Finite State Machine using C++

Implementing Finite State Machine using C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++game-devtutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 9350237
    wrote on last edited by
    #1

    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.??

    D CPalliniC 2 Replies Last reply
    0
    • M Member 9350237

      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.??

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Member 9350237

        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.??

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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;
        }
        // ...
        }

        In testa che avete, signor di Ceprano?

        M 1 Reply Last reply
        0
        • CPalliniC CPallini

          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;
          }
          // ...
          }

          M Offline
          M Offline
          Member 9350237
          wrote on last edited by
          #4

          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??

          S 1 Reply Last reply
          0
          • M Member 9350237

            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??

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups