GameMaker: Adding a Quake-like Console to your games

A while back I decided to make some more debugging tools for myself and the first one I wanted to tackle was making my own Quake-like Console. I got thinking as to how I would implement this and came up with these simple ideas:

  • Spaces will separate everything
  • It would input contents in to a script as arguments
  • I could use a single script to print anything (variables, strings, etc) to it for debugging

Note: this is not a tutorial on how to make a console from scratch, it isintended to help you understand the project file linked here.

Interpreting what you type

GameMaker stores printable characters that you have pressed in a writable variable called “keyboard_string“. We will be clearing this when you open the terminal and using it to see what you have typed. When pressing enter it will run the “keyboard_string” variable through the interpreter and fill an array with the contents separated by spaces.

/*
String Logic, Turns commands that are
separated by spaces in to usable strings
*/
var st = 1;
var ed = 0;
var p = 0;
var chk = false;
keyboard_string = string(keyboard_string + " ");
for(var o = 0; o < 10; o++){
    exec[o] = "";
}
for(var i = 1; i <= string_length(keyboard_string); i++){
    if string_char_at(keyboard_string, i) == " " && string_char_at(keyboard_string, i -1) != " "{
        chk = true;
    }
    if chk == true{
        chk = false;
        while string_char_at(keyboard_string, st) == " "{
            st++;
            ed--;
        }
        exec[p] = string_copy(keyboard_string, st, ed);
        p++;
        i++;
        st = i;
        ed = 0;
    }
    ed++;
}

This code is probably over-complicated for the task but it’s doing the job. If you’ve typed something like “set health 5” it would spit out this array:

exec[0] exec[1] exec[2]
set health 5

Creating Scripts

To run scripts I’m checking the first command (exec[0]) in the snip above) to see if it matches any of the available scripts. If it does match then I input the following array in to the script (exec[1], exec[2], exec[3] etc).

// Run Commands
var run = exec[0];
if run != ""{
    if run == "exit" {game_end();}
    else if run == "inst" {t_inst(exec[1]);}
    else if run == "debug" {t_debug(exec[1]);}
    else if run == "clear" {t_clear();}
    else if run == "ver" {println(global.name + " v" + global.version);}
    else {println("'" + run + "' command not found");}
}

With this method you can simply use if statements in the scripts against the arguments to do what you like. Here is a SS of some scripts in my game. Depending on what kind of game you’re making it could be extremely useful to make console scripts to do tasks at run time during development & testing.

To print anything to the console all you have to do is use the println script and put your variable in it. This is very simple and doesn’t require you to be running a test compile from within GameMaker itself. I’m always displaying variables in it just to see if things are behaving the way I want them to!

Drawing

By default I’m drawing to the “Draw GUI” event. If your game has a lot going on in the GUI layer you may need to make sure the terminal has the lowest depth and therefor is drawn on top of everything.

If you’re going to change the font it’s best to keep using a monospaced font as Consoles, Code etc generally look nicer with monospaced fonts.

Conclusion

Getting this console to work in any game should be reasonably easy. At the top of the create event I’ve got some global variables set, you can change these to suit your game.

If this is useful to you please comment!

Share this postTweet about this on TwitterShare on FacebookPin on Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *