GMS 2 Simple Finite State Machine

R

Rukiri

Guest
GM Version: Studio 1 - 2
Target Platform: Windows
Download: see code below
Links: N/A

What is a state machine?

A State machine is like a factory, your have several workers doing a specific task and at any time can change a task on the fly. State machines keep code organized, easier to find, and personally more readable "but that is subjective".


RPGState_ini() // Place in each object that needs to use a state machine
Code:
//--------------------------------------------
// RPG Finite State Machines
//--------------------------------------------

// Variables
_stateName = noone;
_stateMap = ds_list_create();

enum State {
   name, 
}

//--------------------------------------------
// End Script
//--------------------------------------------
RPGState_create(state <string>) // Create a state and store it in a database local to the object
Code:
//--------------------------------------------
// RPG Finite State Machine Create
//--------------------------------------------

// Variables
var _map = ds_map_create(); // Creates the Map for our RPG Character Database

// Add our State Map to our list
ds_list_add(_stateMap,_map); // Adds a character to a database

// Add our state to our Database
ds_map_add(_map,State.name,string(argument0));

// Return 1 if everything went okay
return 1;

//--------------------------------------------
// End Script
//--------------------------------------------
RPGState_count() // Number of states in our database
Code:
//--------------------------------------------
// RPG Finite State Machine Count
//--------------------------------------------

// Number of States in our database
return ds_list_size(_stateMap);

//--------------------------------------------
// End Script
//--------------------------------------------
RPGState_set(state <string>) // Set the state in which you need to activate the default state should be an idle state
Code:
//--------------------------------------------
// RPG Finite State Machine Set State
//--------------------------------------------

// Variables
var _i, _stateSize;

// Check to see if our state is in our database
_stateSize = RPGState_count();
for (_i=0; _i<_stateSize; _i++) {
   if(ds_map_find_value(ds_list_find_value(_stateMap,_i),State.name) == string(argument0)) {
       // The state was in our database and now we can set the state
       _stateName = string(argument0);
       return _i;
   }
}

// Show message if the state was not in our database and return -1
show_debug_message("The State your attempted to set was not found within the _stateMap");
return -1;

//--------------------------------------------
// End Script
//--------------------------------------------
RPGState_destroy() // destroy the database at the end of the game
Code:
//--------------------------------------------
// RPG Finite State Machine Destroy
//--------------------------------------------

ds_list_destroy(_stateMap); // Destroys our list
ds_map_destroy(_stateMap); // Destroy our map

//--------------------------------------------
// End Script
//--------------------------------------------
To switch states (place in your objects step event)
Code:
// States switch(_stateName) {   
    case "idle": scr_player_idle(); break;   
    case "walk": scr_player_walk(); break;
}
And to activate a state
Code:
if (keyboard_check(vk_down)) {
    RPGState_set("walk"); // set the state machine to walk
} else {
    RPGState_set("idle"); // change back to idle
}
Also if you simply just want to use numbers instead of strings use the real(); command and set the state like so.
Code:
RPGState_set("idle 0"); // the real(); command will remove the characters from the string and the output will be 0
Hope this helps someone :)

- Rukiri
 
Last edited by a moderator:
Top