Archive for the 'GML' Category

Scripting With the Game Maker Language

Although Game Maker’s visual environment provide a friendly interface to many users, some people (particularly experienced programmers) may find it more natural to construct programmes using the text based Game Maker Language (GML).

In this short series of posts, I’ll repeat some of the other Game Maker tutorials using GML. So if you’ve ever fancied trying your hand at writing actual programme code, why not give this a try…?:-) Note that this series assumes some familiarity with writing games the visual way in Game Maker.

To start with, I’m going to replicate the “Catch a Clown” game described the introductory Game Maker tutorial.

The approach I’ll take is to write a series of series of GML scripts that can be attached to events associated with game objects, Setting up the game objects and events needs to be done using the visual editor (at least until I can figure out how, or if, we can write scripts to set up objects and rooms directly!)

To use Game Maker Language scripts, you’ll need to run Game Maker in its Advanced mode (set it from the File menu). Scripts can then be created from the toolbar, or from contextual menu raised by right clicking on the scripts folder in the left hand sidebar palette.

To begin with, we’re going to recreate the a single room game, with wall elements round the edges, and a clown that bounces around inside the room. The aim of the game is to splat the clown by clicking on it.

As I’m teaching myself GML too, will start by learning how to attach scripted events to the game objects. TO start with, you’ll need to:

- create a room;
– create a wall object with a solid wall sprite;
– create a clown object with a transparent clown sprite;
– put wall objects round the edge of the room;
– pop a clown object somewhere in the middle of the room.

To get things moving, we’re going to create a script that will set an instance of the clown object moving when it is created with speed 4 in a random direction.

Create a new script from the toolbar or scripts resource folder; double click on the new script to raise the script editor. In the Name textbox in the status bar at the top of the script editor, give your script a name. For convenience, you might prfix it with scr_. I’m going to call my first script scr_moveAny.

When writing a script, there are certain syntactic conventions we need to be aware of. Firstly, programme code nees to be contained within curly braces: { }

Secondly, separate lines of code need to end with a semicolon – ;

So how do we go about writing a GML programme. GML has a wealth of predefined functions, which are all described in the Game Maker Help menu. But as novices, we donlt know what any of those functions are, or what they do. So let’s start to build up our knowledge somehow.

Before we get into the code, remember this: writing programming code is not what programming is about. Progrtamming is working out the things you need to tell the computer to do so that the programme does what you want. Writing the code is just that – taking the programme, an coding it in a particular way. You can write your programme – the list of things you want the computer to do – anywhere. I often doodle outlines for the programmes I want to write on scraps of paper. Having worked out the programme, we can then write the code.

So what programme are we going to write for starters? Well, when the clown object is created, we need it to start moving in a random direction with a specified speed.

Looking through the GML help file manual, in the Moving Around section, I noticed a function called motion_set(dir,speed) which Sets the motion with the given speed in direction dir.

Hmm… okay, so my programme code might look something like:

{
motion_set(dir,4);
}

So if I attach this script to the clown’s Create event, it should start to move in direction dir with speed 4. But what’s dir? Digging around a little bit more, it’s a direction given in degrees. So to move randomly, I need to set dir to a random number between 0 and 359). I know a bit about other programming languages, so I guess there’s a probably a “random” function… and there is: random(N) which returns a random real number (i.e. it might have lots of bits after the decimal point) between 0 and N.

To get a whole number (known as an integer) we can put the random() function inside another function, called floor(M), which rounds the number, M, contained in the brackets down to the nearest whole number. That is, if we write:

floor(random(360))

the random(360) function will return a random number between 0 and 360 (such as 124.9734), and the floor function will round it down to the nearest whole number (in this example, 124).

Great – so we can get a direction angle for our motion_set() command. We could just replace the dir term in the motion_set function with the floor(random(360)) expression, or we could use a variable. A variable is like a container we can use to represent a (possibly changing) value. In GML, we need to declare a variable before we use it with as follows:

var dir;

var is a special reserved word that tells Game Maker the next word is the name of a variable, in the above case, dir. We can then set the dir variable to a numerical value:
var dir;
dir=floor(random(360));

The whole script looks like this:

{
var dir;
dir=floor(random(360));
motion_set(dir,4);
}

You can check that the code is “correct” in a syntactical sense (i.e. you can check you’ve got the brackets and punctuation right, if not the logic) by clicking on the 1010 button (“check the script for syntax errors”).

If you save the script, we’re now in a poistion to attach it to the clown object. Open the clown object window, add a Create event, and from the control panel on the right hand side, add the “Execute script” action.

Select the clown object and add a Create event. It would be nice if we could configure Game Maker to easily allow us to attach a script to this element more directly, but in Game Maker 7 at least, we need to do this from the control tab on the right hand sidebar palette in the object editor window. The element we’ll be needing is the Execute Script code element:

Game Maker Exectute script element

Select your script from the list of named scripts that are available from the drop down listbox, and attach the desired script to the object itself:

Now play the game. Hopefully, you should see the clown start to move in a random direction at the required speed when the game is started.

So what’s next? If the clown bumps into a wall, we need it to bounce off. We might also want to play a sound as the clown does so. Create a new script, and call it something like scr_wallBounceSound. The function move_bounce_solid(adv) (“Bounces against solid instances, like the corresponding action. adv indicates whether to use advance bounce, that also takes slanted walls into account.”) looks handy. I’m not sure what adv is, but I’m guessing true or false…

Let’s try this:

{
move_bounce_solid(false);
}

Save the script, add a collision event to the clown object that detects a collisions with a wall object, and attach the scr_wallBounceSound script to it. Run the game – hopefully your clown will start to move in a random direction and then bounce off the walls…

Now lets add a sound. Searching for sound in the help file turns up dozens of sound related GML functions, but sound_play(index) looks relevant (“sound_play(index) Plays the indicates sound once. If the sound is background music the current background music is stopped.”). index is the number of the sound file, as ordered in the sounds folder in the resource sidebar, and starting with the index number zero. I have two sounds in ,my game, one for wall bounces, one for when the clown is clicked on, so I choose the approariate one. My script now looks like this:

{
move_bounce_solid(true);
sound_play(0);
}

And finally… In the simplest version of the original game, the idea was to click on the clown to catch it. Catching it has the effect of increasing the score, playing a sound, repositioning the clown to a different location, and setting it moving in a random direction again.

We know how to play the sound and get the character moving, so all we need to figure out is how to increase the score, and move the clown to a new location. In the GML help pages the section on “Score” tells us the name of the variable that is defined by the game to hold the current score: score.

To increase the score by 10, we can write one of two things. Either:
score=score+10;
That is, set the new value of the score to equal the current value of the score, plus 10.

Or we can use the shorthand form: score+=10

To reposition the clown, the help file comes to our rescue again. In the Moving Around section, we find the function move_random(hsnap,vsnap) Moves the instance to a free random, snapped position, like the corresponding action. I think we can just set the hsnap and vsnap values to 0.

So here’s the script that we want to attach to the left-click mouse event on the clown object:

{
score+=10;
sound_play(1);
move_random(0,0);
motion_set(floor(random(360)),4);
}

Okay, I think that’s enough for now… except to look at how we might save and load scripts. In the scripts menu is an option to Export a selected script. The export looks something like this:

#define scr_clickedClown
{
score+=10;
sound_play(1);
move_random(0,0);
motion_set(floor(random(360)),4);
}

It might therefore seem reasonable to suppose we could edit a who range of scripts in a text editor outside of Game Maker and save them in a single text file. Something like this maybe?

#define scr_moveAny
{
var dir;
dir=floor(random(360));
motion_set(dir,4);
}

#define scr_wallBounceSound
{
move_bounce_solid(true);
sound_play(0);
}

#define scr_clickedClown
{
score+=10;
sound_play(1);
move_random(0,0);
motion_set(floor(random(360)),4);
}

And it does indeed seem to work… as long as you save the file as a text file with the suffix .gml

Finally, finally, it’s just worth saying that if you want to leave notes to yourself in a GML programme that are ignored by Game Maker, you can do. They’re called “comments” and you prefix them like this:

// a comment in a programme that is
// ignored by Game Maker;

That is, use double slash… And how are comments typically used? A bit like this:

//script to handle the left-mouseclick event when a clown is clicked on
// this script should be attached to the clown object
#define scr_clickedClown
{
score+=10; // add 10 to the score
sound_play(1); //play the squished sound
move_random(0,0); //move the clown to a new location
motion_set(floor(random(360)),4); //move in a random direction at speed 4
}

That is, we can use the comments as programme code documentation…


Categories


Follow

Get every new post delivered to your Inbox.

Join 56 other followers