Command Line Parameters

A tutorial by Andrew Barber aka Chronic

Command line parameters are another feature of Game Maker that I believe to be overlooked, this tutorial is going to show what they are, and one of the ways they can be used.

When you run an executable (.exe file), this is normaly done without a command line. A parameter is basicly an argument that you pass to an executable.

Parameters are seperated by spaces. The first parameter is 0, this is always the full path and executable name for example: "z:\folder\game.exe". Anything after 0 is what was passed to your game

game.exe -parameter1 -parameter2

You'll notice I put a hyphen before each command, this isn't really needed but you'll find that its very common to see this being used in other programs.

Using command line parameters is very easy. Game Maker has two functions for this, one of them to get the amount of parameters parameter_count(). The other is to list the parameters passed to the executable parameter_string(n).

To give an example of what a command line can be used for, some programs will allow you to load a file in to it, such as notepad. If you supply a filename to notepad it will try to load that file. If it can't load it you will be asked if you want to create it. You can try this out for yourself by clicking your Windows Start button then clicking Run. In the box simply type "notepad test.txt" and click OK.

command line example

Now that you know what a command line is, how can it be used in a game? Well, one example I can think of is to have it activate cheats, which is what i'll be covering.

First of all start a new project in Game Maker and add yourself a room and object. Add the object to the room and close the room, as we'll not be using it anymore. In the object add a create event and a execute code action, this is all we really need for this example.

The script for reading the parmeters is relatively simple. All thats needed is a for loop to iterate through the parameters, and a switch case to tell the game what to do with them.

  for (i = 1; i <= parameter_count(); i += 1) {
    parm = parameter_string(i);
    switch(parm) {
      case "-parameter1": show_message("CHEAT 1"); break;
      case "-parameter2": show_message("CHEAT 2"); break;

Testing this in Game Maker will do nothing, you will simply get your room and no messages. This is we have not ran the game with any parmeters to test them. To do this you must save your game as an executable.

Once you have your executable saved you can run it several ways, one of them like you did with the notepad test. Just remember to provide the full path to your game or you may get a "file not found" error. Another way to run it is to create a shortcut to the executable and then edit it to change the "Target" path to include your parameters like shown below.

"z:\folder\game.exe" -parameter1 -parameter2

That pretty much wraps up this tutorial, i hope you've learnt a thing or two from it. You can find an example of this tutorial at the link below.

  • Command Line - Example