Gw Temp

Menu

Tutorial - 'Game Maker and Batch Files' by The_Pup

An item about Gamemaker posted on

Blurb

An interesting tutorial, explaining how to use batch files in conjunction with Game Maker.

Body

I'm doing this tutorial for informational use for Game Maker 5.0 users(aiming at the beginning to intermediate group and some advanced ones). Not many(big lie, very rarely) people who use GM use, or know how to work some of the useful and/or lesser known functions GM in their favor(me included). The lengthened title should actually be "Batch files and Game Parameters for Game Maker 5" but it ain't that catchy at all. It's probably the first tutorial on the subject of GM parameters so don't expect alot out of this since it's hard to do a tutorial on something not covered or explained to in person and not done very well in the help file.

----------

If you ever seen some of those DOS based games, playing with batch GIF converters, or making a bunch of shortcuts by hand you would already know a bit about Parameters and Flags. For those of you who don't know what a parameter is then here's an example:

Say that you want to make a cheat code outside of your game, automatically go to the in-game level editor at startup, or get into a different/harder difficulty setting without needing to hassle with making an options menu with about 10 commands that should automatically come up at the beginning of the game.

So we need to execute several options inside the program during it's start up phase. Parameters are like script functions in GM.

This is easily solved with batch files(with the *.bat file extension). They can execute multiple commands with a click of your mouse. But BE WARNED, batch files are DOS based and SHOULD NOT BE MESSED WITH. YOU COULD ACCIDENTALLY WIPE YOUR OWN HARD DRIVE with just about 10 bytes of code(yes, DOS is god, so treat with respect). Even though most newer Windows Operating systems don't depend on DOS, DOS can still do lots of damage if your not careful.

Ok, with that out of the way and most of some newbies with computers and DOS scared half to death I will show you an example of the type of batch file I will use(you can make one by typing it out in Notepad and saving it under the All Files preference with the extension .bat) promptly called "test_execute.bat"(and it's very, very basic and not harmful):


;The semi-colon is a comment just like // in GM
@echo off ;optional but remove to see what's happening
echo This line tells DOS to relay some type of information to you
echo. ;Skips a line(empty)
some_program.exe /a /b /c ;This executes a program with the flags /a, /b, /c set


The last line is the most important one(you don't need the others but they help a bit in organizing yourself). The flags /a, /b, /c are Parameters in modern Windows terms and they usually consist of one or 2 letters. Just think of them as on and off switches to different lights in your house. But instead of lights and a house it's your functions and game.

Game Maker 5 provides 2 functions in dealing with parameters:

parameter_count(); //Counts how many parameters there are including the program name
parameter_string(n); //Returns the parameter n while the program name is parameter 0

The most useful of the 2 is "parameter_string" although "parameter_count" does serve its purpose well. You can't assign parameters with either functions sadly, but all that is figured out.

Create a script called "return_param":


//Start Copying "return_param"

var n;
n = 0;
if parameter_count() > 0 //Check to see if there are more parameters than 0
{
repeat (parameter_count()) //Does next action a certain amount of times
{
n+=1; //Skip to next
define_param(parameter_string(n)); //Perform action
}
exit; //Finish script when done
}
else if parameter_count() < 1 //If parameters is less than 1
{
exit; //Finish script
}

//End Copying


Next make a new script called "define_param"


//Start Copying and name script "define_param"

switch (argument0)
{
//Place parameters here
case "/a": recordera = "On"; break; //Example parameter
case "/b": recorderb = "On"; break; //Example parameter
case "/c": recorderc = "On"; break; //Example parameter
}

//End Copying


If you have never made a "Switch-case" statement before you should read the help file since this is easy stuff and is shorter and more organized than "If-else" statements. I am not going to explain it since the help file does it better. But if you do notice that the flags are written out as strings, that's how it should be done like all text variables.

Ok. Now make an object and name it whatever you want. In the creation event put in this code:


//Start copying to Creation code of an object

recordera = "Off";
recorderb = "Off";
recorderc = "Off";
return_param();

//Create Batch file to execute

if file_exists("param_count.bat") = false //Check to see if there is no batch file
{
file_open_write("param_count.bat"); //Starts creating batch file
file_write_string("param_count.exe /a /b /c");
file_close();
execute_shell("param_count.bat",0); //Executes newly created batch file
game_end(); //Ends the current game so we don't have 2 games running
}

//End copying


Now in the Drawing event of the same object we want to put in this code:


//Start copying to Drawing event

draw_text(x,y,"Executes a batch file. Below are the variables.")
draw_text(x,y+15,"/a = " + string(recordera));
draw_text(x,y+30,"/b = " + string(recorderb));
draw_text(x,y+45,"/c = " + string(recorderc));

//End copying


Place the object somewhere in the first room and save the GMD but don't run it yet. Go to the file menu, choose the option "Create Executable," and then same the file as "param_count"(the .exe is added at the end automatically). Now run the game. It should run, execute a DOS box, end itself, and then the DOS box opens up the program again. And all the flags in the window should say they are "On." Now exit the program and then enter it again and it says that all the flags are "Off" as coded into the object.

In the same folder there should be a file called "param_count.bat" since it was made by the program. Double click on it and then it will execute the exe and turn all the flags on. Now right click it and select "Edit." Now delete "/b" and save the batch file and execute it once again. Now in the program it should say that flag "/b" is "Off." Delete flag "/a" in the batch and save then execute the program. Flags "/a" and "/b" should be "Off" and flag "/c" should be on. Note that the parameter name can be(without the quotes of course): "/edit" or "/cheat" or "/lores", etc..

To use these in GM you can make a room with one object in it that checks parameters and switches variables or global variables to true/false and sends the player off to different rooms based on those parameters(ie a room for the main game or to a room that contains the level editor). You could make it unlock a special cutscene in the middle of a game or a different route. Othertimes you may want to make a special level. Or maybe you want to put in some special content like Easter Eggs or special "pictures"(*cough*). Other times you may make an alternate suit for the main character(ie Like the Justin Bailey code for Metroid for a suitless Samus Aran). Or you could make cheats such as infinite ammo or invincibility. The possibilities are endless.

ciao,
Shaun