Game Maker…
FOR BEGINNERS TO START UP!
By Phantom_Of3
Information:
August 02, 2007. Hello, I am Phantom. I have used Game Maker since 6.0, and for the longest time I struggled with it horribly. Finally, I am learning it well so I am writing this guide for two reasons: so that you can learn it well too, and because I know what it is like to struggle with Game Maker.
What is this guide designed to do?
This guide is supposed to start you off as if you just got Game Maker and help you quickly enough to start learning about its language! This is probably one of the better guides for doing that.
I am an advanced user. Will this guide still help me?
Depending on how much you know about Game Maker, this guide may or may not help you. This guide is specifically designed for beginners or people who are having trouble with Game Maker, but if you think that this guide would help you, go for it!
How will this guide be written?
While writing this guide I will explain things the best I know how. I know the different struggles that beginners have since it took me forever before I even made a first game with Game Maker.
How does this guide work?
This guide starts you off as if you didn’t know a thing about Game Maker. It then works your way up until I can finally stop writing this guide since I would figure you knew it by then.
So…when should I start reading this guide?
It is recommended that you start reading this guide as soon as you get it, but you can do it on your own time chapter by chapter. If you need to mark it, just write it down on a piece of paper.
What version(s) were you using when you wrote this?
I was using Game Maker 7.0 when I wrote this. However, it should be usable for any better version just as well, and some lower versions.
CHAPTER ONE
BEGINNING STUFF
Since this guide is made specifically for Game Maker, it comes natural that the first step to this guide would be to download Game Maker. I would recommend downloading version 7.0, since it is the newest version. You can download Game Maker at: www.yoyogames.com/make! The yoyogames site is the main Game Maker webpage. However, you can see the old Game Maker webpage at www.gmnl.parahosting.net! If you read this guide and the old Game Maker site clone is not up, that probably means that the administrator stopped hosting it (that site is not by the maker of Game Maker, Mark Overmars. That site is from someone at the GMC (Game Maker community)). The Game Maker community is the forums for Game Maker. There, you can ask other people questions about your problems etc. The URL for the GMC is: www.gmc.yoyogames.com!
Okay, finally you have downloaded Game Maker. You are pretty excited to make games. What if one day you made Games like Quake or RuneScape or Diablo? You feel like life is just flowing your way.
Then you take a look. Game Maker looks a little bit…complicated. They really expect you to learn this strange coding on your own? And this Drag and Drop seems a little bit confusing…
Not anymore! This guide will teach you the basics of it all. Once you know the basics, Game Maker really seems a lot less confusing!
Something everyone always says is, “Start off simple.” Sometimes, hearing that phrase can make you frustrated. Unfortunately, it is very true when it comes to Game Maker. So simple is how we will start off!
I told you that we would start off as if you didn’t know anything about Game Maker, so that is what we will do! Open up Game Maker. It will probably say stuff about registration. You don’t need to register it, but when you get experienced it is a great idea. For the time being, just click, “Don’t Upgrade Now.” If you want to register it right away, it is $20 but if you think that you really want to do Game Maker and that you will find a way to do everything, I am not against you registering.
Now, you have opened Game Maker. Why not take the time to look at all the groups of files that you see on the left corner. They are titled: Sprites, Sounds, Backgrounds, Paths, Scripts, Fonts, Timelines, Objects and Rooms. Then below it is: Game Information, Global Game Settings, and Extension Packages. Here is a list of the file groups’ names and what they do:
Sprites
Sprites are images within the game (besides backgrounds, of course. However, you are able to use sprites as backgrounds, incase you wanted animation in your background. But for the time being, and most of the time, just use backgrounds as backgrounds and sprites as sprites.) To draw a sprite, just right-click on the file folder and click: create sprite. Now, you are inside the sprite editor. Here, if you click on the pencil, you are able to edit the image. To make animation you must copy one frame, paste it, and edit the next frame. Those are the very tiny basics of spriting, so since the rest is rather self-explanatory, you should be able to figure out the rest.
Sounds
Sounds are sound effects or music that you use in the game. To make a new sound, you must use your own program. Out of all the free sound-making programs, I would suggest Audacity, since it is a pretty decent one (you need a microphone for your computer to make sounds, or you could have someone do it for you). Since Game Maker comes with them, you can just right-click on the sounds file and load a sound from a file. This built-in sound editor is primarily used to adjust the volume and pan (though you can do effects if you have registered Game Maker). If you click edit sound, sometimes it might bring you to a better sound editor but it is more complicated, so I would suggest that you just stick with audacity.
Making background music for your game can be tricky as well. The best free program that I would recommend for this job is Anvil Studio! You don’t need a microphone for this; however you can use one to sing notes into the computer. This program allows you to make musical notes with different instruments, all using the same program! It is probably one of the better free music-making programs.
Backgrounds
Backgrounds are just the back images of the game such as: space, lava, a castle, a field, or anything else. It is used to make the game look better and more believable. Sprites and backgrounds can be hard to make. The Game Maker sprite and background editor aren’t always the best way to do it. If you have Paint or MS Paint on your computer, those are good programs for drawing images, although a program like Paint Shop Pro or Microsoft Photo Draw are probably better to use (especially for more advanced graphics).
Paths
Paths are used for things in the game. You can let something have a path so that it only goes in that direction. Paths are executed in the object (see further below).
Scripts
Scripts are used in the game to execute code in GML. There is also a way to just execute code. The only difference between the two is that scripts have to have arguments. We will talk about GML later in this tutorial.
Fonts
Pretty self-explanatory. They are just what you can use in a game before you write different words. That way, you get to choose the font for what you write!
Timelines
Timelines can be executed inside an object. A timeline can have many different actions happening in it. It is good to use in a game where you want a lot of stuff to happen all of the sudden, but it may not always be necessary.
Objects
Finally I can tell you what objects are. Objects are things within the game that are programmed to do whatever you program them to do. However, sometimes you might make an object with no programming if it is just used for the looks of the game etc.
Pretty much, objects are usually anything in the game that plays a part. Sprites are not objects, however most of the time you want to give objects sprites so that you can see them. You can look at it this way: without objects, there would be no game. Why? Objects are what make a game a game. Perhaps you have an object that is a character, maybe it is an enemy UFO. Maybe it is a book, a TV, a weapon, or anything like that! Objects are what you are programming to make the game work. You can’t have a game without objects.
Rooms
Rooms are any scene of the game where you can see something or where something is happening. A level is a room, a menu is a room, and the places that you put credits are rooms. Rooms are any part of the game where something is going on. You can’t have a game without a room!
Other things:
Game Information: In here, you write the information for the game such as instructions. If you keep the Global Game Settings the same, players can view the Game Info by pressing F1.
Global Game Settings: This is just where you control basic things for your game, like how big the window is etc. It is important to keep track of this.
Extension Packages: These are only available in the registered (“Pro”) edition of Game Maker. They are just used to extend Game Maker’s capabilities. You have to either make them yourself of have someone else make one for you.
CHAPTER 1.1
BEGINNING STUFF:
First Creations
Ahh…yes, the first game. It is often when you start that game that you wonder if you should quit even trying to make games. I remember trying to make my first game. Making your first game can be very tough and frustrating. I know what it is like when you try to start off too big and then all of the sudden you start confusing yourself. I am here to make sure that you make an easy enough first game.
The first game that we will make is a simple little game called hit the ball. You may have already made this and not learned anything, but we will make it differently and I will explain.
YOUR CHOICE
The following part is your choice: you get to choose whether you want to draw the sprites or find them in the Game Maker file folder called: Sprites. Now, if you want to be the kind of person who makes games by himself (meaning you would do the Spriting, Programming and Sound/Music making) then I definitely suggest that you draw your own sprite! If you are going to do this, it is recommended that you start looking at other peoples sprites by using the magnifying glass in the sprite editor so that you can see the pixels, then you can use them as an example. Also, this guide comes with a book about designing video game graphics, by Ari Fieldman. He is the one who drew all the 1945 sprites, so you could probably learn from him. However, I can not promise you that his book will help since I have not spent the time to read it.
Sound/music making will go the same way: if you plan on making games all by yourself, you will want to make the sounds and music as well. Recommendations for free programs that make sounds/music are up at the top (on page 4). I have included different graphic making programs as well.
TIME TO BEGIN
Alright, why don’t we begin right now? Now that you have either drawn a ball and a wall or taken it from a file, be sure that they are both a sprite.
Now, click on the Objects tab. Choose the sprite that you made to be a wall. Click on the checkbox that says “Solid”. Now, click OK, since we don’t need to program the wall any more than this.
Now, we are going to program the ball. Don’t get too worried, you will have me to help you.
First, add the “create” event. Now, you see all the images on the side. Those are called Drag and Drop (D&
) icons. We will use them to start off.In the Move section, you will find an icon called “Move Free”. Click on it. Now, what we are about to do will seem weird at first, but I will explain it. Set the direction to: random(360). Then, set the speed to: 5. Now, I’ll explain what this means.
Direction is a built-in variable that you can use to tell the game what direction you want something to go in. Speed is a variable that indicates how fast something goes. Here is how to do this in code (you don’t have to do this, but if you want to you can):
Direction is the variable which means the direction that something goes in. Speed is the variable which means the speed of something. So here is an example of how to make something keep moving in random directions:
In the create event of the objects, click on the drag and drop icon called “Execute a piece of code”. Now, type in: direction=random(360). That indicates that the object can now go in any direction, and it chooses it randomly.
Next, type in: speed=5 (you can change that if you want a different number). Now this is what it should look like:
direction=random(360)
speed=5 /*Like I said, you can change the speed to the number that you want to change it to.*/
Now, if you have a wall in the game, you could execute the same code in the collision with wall. This way, it always moves in a random direction with 5 speed.
Hint: When writing a variable, always use lowercase letters (unless you defined the variable yourself). If you use capital letters in built-in variables, the game will not know that you want to use the variable. For instance, let’s pretend I clicked on the Check variable D&
action. Let’s pretend I put in DIRECTION as the variable and said: if DIRECTION is equal to 5, alarm 0 = 200. If I did that, the game would probably make an error stating: [Unknown variable DIRECTION], because I did not put it in lowercase.Now that we have finished the Create event, let’s move on to the Collision event with the wall object. All you have to do here is find the icon that says: Bounce. You can find this in Move section.
Now, all you have to do is make it bounce not precisely against solid objects. Then click okay, and we will move onto the first-to-last event.
When you click on Add Event, click on what says: Mouse. Then click on left button. Now we have an event for when the left mouse button is pressed.
Now you have a choice. If you want to make a sound that sounds like something is being hit or clicked, go ahead. If you want to do it the easy way and find a sound like that by loading a sound from Game Maker, that works as well. Be sure to name the sound: hit.
Now, in the left button event put in the action: play a sound. When that opens up, make it play the sound “hit”. This makes it so that the sound “hit” will be played (pretty obvious).
Now, click on the D&
action that says: Set Score. Set it to 1, then click on the “relative” box. I will now explain what the relative box does.Relative in this case means that it is added/subtracted. For instance, if we set score to 10, the score is equal to 10. But if we set it relative to 10, 10 points are added to the score.
If we set it to -10, the score is equal to: -10. But if we set it relative to -10, 10 point are taken off of the score. There are a few other versions of relativity. One of them you can learn by looking at the 1945 example by Mark Overmars.
Now that you know what that is all about, it is time to put in the next action. In the Move tab, there is an action called: Jump to Random. Click on it. When it opens up, just leave the snapping values to 0 and click OK. This action sends you to a random position, which is useful here since the object is being clicked on.
Now, once again click on Move Free and set the direction to random(360) and the speed to 5. This way, the ball will start moving in a random direction again. If you have already forgotten what this all means, go back up and read it again until you understand it.
Well, that is all the programming for the ball object for now. That wasn’t so hard was it?
Now, go to the ROOMS tab which is a little below the OBJECTS tab. Right click on it and left click “create room”. Now, click on the objects tab and place walls all around the room and place two balls in the center of the room.
Now, click on the settings tab and look at where it says: room speed. Right now it should be at 30, so change it to 60. That way, your game will run faster. Oh yes, by the way, the room speed is what determines the steps per second. This can be used to speed up games or slow them down.
Well, as long as you have read this right, you should now have a very simple game. But if it doesn’t seem right to you, you can look at Mark Overmars’s version of this and try to figure out if you read anything wrong.
ADDING TOUCHES
Since you just made your first game, why not add a simple little touch to it? Go back to the ball object and in the Create event, set alarm 0 to 3600 (which is equal to a minute, since the room speed is 60 steps per second). To set an alarm, just go to the Main2 tab and find the function that says: Set Alarm. Now, like I said, make it set alarm 0 to 3600.
Now, make an event for alarm 0 by clicking add event, then clicking alarm, and then going to “alarm 0”. Now, in the score tab find the action that says “show the highscore table” and click on it. If you want to add your own touches to the highscore, like a background or a font, you can do this in here. But if you want to now you can just click “OK”.
Now in the Main2 tab, just click Restart Game. Now, in one minute the game will show the highscore table, letting you insert your name and next to your name the score will be shown. Then, when you are done with that the game is restarted. Now, let me explain what alarms are:
Alarms are a little bit like alarm clocks. You set them to a certain amount of steps. Then you make events for them, telling the game what they will do. They can be very useful in many ways. This is the best I can explain it, so I would suggest that if it doesn’t make sense you look at this game until it makes sense to you.
Now, time for your assignment:
Since you are very new to Game Maker, you need to learn what all of the Drag and Drop functions are. Unfortunately, I have chosen not to spend the time telling you what all of them do because they are already explained in the GM manual. Saying that, your assignment is to read what the GM manual has to say about all of the drag and drop functions. If you don’t know how to do this, all you have to do is open Game Maker and click on the question mark! That is where you will find everything. But incase you want it all in one document, you can get a document at the yoyogames website called gmaker docs. This is the entire manual. If you downloaded this GML tutorial with all of the other resources, you will find it in the “Other Game Maker Tutorials” folder (when inside, you then go to the folder marked “Stuff from the GM7.0 manual”. Then just click on “gmaker docs”.
The manual may seem a little confusing, so be sure and read all about D&
functions until you finally think you have memorized it. Then, we will go on to the next lesson: variables.CHAPTER 1.2
BEGINNING STUFF
Variables (and a look at GML)
WHY YOU HATE MATH
Often times in math, variables can make some problems rather confusing, such as in a problem like this: 800/55*3-x*5/99*989/300-9=100,000. What is the value of x?
When you see a problem like that, you will almost instantly do one of two things:
1. Faint.
2. Grab a calculator and mindlessly hit buttons.
In Game Maker however, variables are often your friend. There is no such thing as a good game without variables. Some variables are defined by you, other variables are already built into the game. Here are some basic variables that are built into the game:
mouse_x: this variable returns where the mouse is (in the x coordinate). What I mean by “returns” is that it lets the game know where in the x coordinate the mouse is (as you probably know, every room has x and y coordinates). Here is an example of how you would use it:
if mouse_x == 250 /*== is often used instead of = when you are checking a value. E.g. (example given): if x == 3 alarm[0]=5*/
instance_create(500,300,obj_blackhole)
What the above script would do is check if mouse_x was equal to 250 and if it was, it would create the instance of an object called obj_blackhole at x: 500, y: 300. By the way, what I wrote within the /* and */ signs would not do anything in the scripts; they would make a comment so that the reader could see what I was writing. You can also do this by putting // at the beginning of the script, but I prefer using /* and */.
Wow. Look at that! You have already learned one code and we haven’t even gotten to basic GML (incase you aren’t sure what I mean by this, we just looked at the GML action called: instance_create).
Now for another important variable: mouse_y. This variable is just like mouse_x except this one checks where in the y coordinate the mouse is. E.g.:
if instance_exists(mouse_x,mouse_y,explosion) with (other) instance_destroy()
Once again you have seen script…I really need to get to the basic GML section of this guide! Anyway, what the above script would do is check if there is an object called explosion at the same x and y coordinates as the mouse and if there was, the other object (which would probably be the explosion) would be destroyed! That is how you use the with statement. What it means is that the next thing applies to the object you define in the variable. E.g.: with (other) instance_destroy is a commonly used code because it tells the game that the other object is destroyed! So with (other) means applying to the other object.
The next variables that we will quickly go over are: x and y. These are very common variables, so chances are that you already know them. In Game Maker x and y just refer to the x and y coordinates in a room. E.g.:
if x == 350 && y == 600
instance_create(random(room_width),random(room_height),bomb)
The above script would check if x is equal to 350 and if y is equal to 600. And if they were, and object called bomb would be created at a random position within the room width and room height. You will notice that rather than saying and in the script I said &&. They are both the same exact thing, so if you want to use and you can and if you want to use && you can. Look at the script below:
if x == 50 && y == 90
This checks if x is equal to 50 and y is equal to 90 (this is only the starting of a script). Now look at the script below:
if x == 50 and y == 90
This script also checks if x is equal to 50 and if y is equal to 90. My point is that && is the same thing as and.
Now, onto the next variables: room_width and room_height!
room_width is the variable which means the width of the room. room_height means the height of the room. Just to let you know, x is the room width and y is the room height. You already have seen an example above involving room width and height, so I won’t go any further.
The next variable that we will look at is called: visible. This variable checks whether the indicated object is visible. Here is an example:
if visible == false visible == true
The above script is very simple: it checks if the object is visible and, if it isn’t, it sets the variable visible to true (meaning that the object is now visible).
The next variables are pretty self-explanatory: xprevious and yprevious! These variables refer to the previous position of x and y that you were in. Here is an example:
In the collision event with something like a wall you could put this:
x = xprevious
y = yprevious
What that means is that you go to the previous x and y position. That way, when you collided with the wall you wouldn’t go through it, but you would stay in the same place that you were at before you collided with the wall.
The next variable: friction. Here is what the GM manual says:
Friction slows down the instances when they move. You specify the amount of friction. In each step this amount is subtracted from the speed until the speed becomes 0. Normally you want a very small number here (like 0.01).
I asked someone what friction is. Here is the answer I got:
Friction is used to slow an object down using the speed variable. When a key is pressed friction should be set to 0 otherwise it would cut into the speed variable. Then when a key is released you would set the friction back.
I think that those are the best ways to define friction: they slow down an object until it completely stops. It would be hard to give an example of that as it is something that you learn about as you get through Game Maker.
The next variable is solid. It indicates whether an object is solid or not. If an object is solid, it can’t move. It is just there (for instance, you could make a wall solid since it doesn’t need to move). Here is an example:
if solid == true alarm[0]=60 else solid=true
The above script checks if solid is true and if it is, alarm 0 is equal to 60. Otherwise, it sets the variable solid to true. You notice at the top that I used the statement: else. You may not be familiar with this expression. It means otherwise. So in that script if solid is true alarm 0 is set to 60 but OTHERWISE solid is set to true. I pretty much repeated what I already said about three sentences ago just because I wanted you to understand it.
Next up: direction and speed. You already should know what these mean: direction is the direction which something goes in and speed is how fast something goes. Here is an example;
direction = random(360) speed=5
As you should remember, that code makes you go in any random direction at speed 5.
IMAGE VARIABLES:
QUICK EXPLANATIONS
The variable image_single makes a sprite only use one subimage so that it doesn’t have animation. E.g.: image_single=1. The variable image_alpha sets how transparent an object is. If image_alpha is equal to 0.7, it is 7/10 transparent. The variable image_speed sets how fast a sprite goes through its subimages. The variable image_angle sets the angle of an image (use a number between 0 and 360). Oh yes, here is something that Blade_ Gamer told me:
To make something point toward the mouse use:
image_angle=point_direction(x,y,mouse_x,mouse_y).
THE BURNING QUESTION
How do I define variables? That is what you may be thinking if you remember me saying that you can define them.
I will give you a good starter game to teach yourself how to define variables. To start out, either make a character or draw your own (this game won’t be displayed, it is just for your learning. Therefore, it doesn’t matter how good or bad the character looks). Now, draw a sprite for a ghost or get it out of a file. It doesn’t have to be a ghost, but we’ll pretend it is while I talk you through this tutorial.
Now, in the character’s create event, click on the action that says, “Execute a piece of code.” (There are ways to do this in D&
, but I want to get you into GML since it is what you need to get anywhere with Game Maker).Now, write this into the code:
invincible=true
alarm[0]=90
The variable that you just put in (invincible), is the one that we are defining. Now, click on “Add Event” and go to keyboard event and click on: <Left>. This event defines what happens when there is a keyboard event for the left key, whether it be when the key is pressed or when the key is released.
Now, execute this code: x-=3. This code may seem confusing, but it is actually quite simple. It means that 3 is taken away from the x coordinate, meaning that you move left since the x coordinate is being taken away from. Codes like x=x-3 are the same exact things as x-=3, but it is much quicker to just say x-=3.
Now, make a keyboard event for the right key and execute the code: x+=3. This makes you move 3 in the right direction.
Now make a keyboard event for the up key and execute the code: y-=3. This will make you move up. The reason for this is the fact that in y values, the more you move up, the more values are taken away.
So obviously, in the keyboard event for down you say: y+=3 since that will make you go down.
So far, you have a character that moves around. Now, in the alarm 0 event, put this code: invincible=false. Since in the create event alarm 0 is set to 90, the way this game will be set up you will be invincible for the first three seconds of the game.
Now, in the collision event with the ghost object, execute the code:
if invincible == true exit instance_destroy() game_end()
What the above code means is that if the variable invincible is equal to true, the entire event (which is the collision with ghost event) is exited. This makes it so that you are invincible for the first three seconds because if you collide with them and invincible is true then they go right through you!
And as you can see, if invincible is false, when you collide with the ghost you are destroyed and the game is ended. Now, click on add event and click on other, then click on outside room. Now, execute this code:
{
if (x < 0 && hspeed < 0) x = room_width + sprite_xoffset;
if (x > room_width && hspeed > 0) x = -sprite_width + sprite_xoffset;
if (y < 0 && vspeed < 0) y = room_height + sprite_yoffset;
if (y > room_height && vspeed > 0) y = -sprite_height + sprite_yoffset;
}
The above code was made by Mark Overmars. It means that if you are outside of the room, you will go to the other side. I will not try to explain what everything means since it is all a little complicated. Plus, a good way for you to learn what this means is by examining it and trying to make sense out of it. That being said, I’ll leave that part to you. If it takes you a while to understand, that is alright. If I feel like it in another version, I might explain what that all means.
Now, make an object for the ghost (or whatever it is). Now, in the create event execute this code: direction=random(360) speed=5 and in the outside room event execute this code: direction=random(360) speed=5 and click execute code again and put in this:
{
if (x < 0 && hspeed < 0) x = room_width + sprite_xoffset;
if (x > room_width && hspeed > 0) x = -sprite_width + sprite_xoffset;
if (y < 0 && vspeed < 0) y = room_height + sprite_yoffset;
if (y > room_height && vspeed > 0) y = -sprite_height + sprite_yoffset;
}
There, now you have an example game on how to make variables. So save this game as something like: variable_example.
UH OH…HOMEWORK!
Now it is time for your assignments. These won’t be too hard—really, they won’t. I want you to look at the example game that comes with Game Maker called pacman (or if the only version you have is 7.0, it is called treasure). Look at it carefully and you will learn things like changing the sprite when you click left for instance so that it will show you moving left. There are many other good things you can learn. And if you only have Game Maker 7.0, you may not have these tutorials but if you can use the tutorials: 1945 and street race, you will be able to learn more about variables.
As an optional assignment you can also make a few more examples for yourself on how to define variables. Have fun!
CHAPTER 2
Basic GML
Now we will learn some basic GML. Basic GML is not very difficult, as you might have learned from looking at the examples I gave that used some basic functions. Advanced GML isn’t too hard once you learn it either, but looking at advanced GML when you don’t know much about basic can get you confused into thinking all GML is hard.
To start this lesson, I would like it if you read the examples I gave above in the variables section, especially since variables are also important while using GML.
Now we will start off with the very basics. The if statement is a very common statement which is used to check something. E.g.: if image_single == 1. That code checks if image_single is equal to 1. That is not a complete script of course, just part of one.
Now, I asked a person on the GMC named Stryke about other statements similar. I like his answer as he explained well so I will post it here for the explanation:
Okay I'm going to use examples:
for(i=0;i<5;i+=1
//this loops until this statement is false: i<5{
global.inventory[i] = "Long Sword"; //set inventory items from 0-5
}
Basically this is what it does:
global.inventory[1] = "Long Sword";
global.inventory[2] = "Long Sword";
global.inventory[3] = "Long Sword";
global.inventory[4] = "Long Sword";
global.inventory[5] = "Long Sword";
The variable starts off as 0 since i=0.
It checks if i<5, if so, continue with the loop.
Adds 1 to the variable "i".
Now everything that's in the for statement is executed until i<5 equals false.
Now while statements..
There basically like if statements except they loop until it's true.
That means the game would be frozen until the statement is true.
while(!place_free(x,y)) //while x,y are not collision free
{
x = floor(random(room_width)) //random x
y = floor(random(room_height)) //random y
}
Basically what happens is like how you read it.
While at position x and y is not collision free, set x and y to a random position. This is an "if" statement that never ends until the x and y is a place where is collision free.
Now do statements was it?
It's pretty self explanatory really…
do
{
x = floor(random(room_width)) //random x
y = floor(random(room_height)) //random y
}
until(place_free(x,y)) //until collision free
Just like how you would read it. Randomize the x and y positions until the x and y positions are collision free. Keep in mind that they WILL loop and freeze the game until the statement is true.
A switch statement is an if statement but if you several different situations, that you could do all at once.
switch(sprite_index) //switch statement for sprite_index
{
case spr_CharacterLeft: //if sprite_index = spr_CharacterLeft
character_direction = "left"; //set direction to left
break; //skips the rest of the switch statement
case spr_CharacterRight: //if sprite_index = spr_CharacterLeft
character_direction = "right"; //set direction to right
break; //skips the rest of the switch statement
}
Return is only used inside scripts, this is return the real value, string, etc. When executing a script. Example below..
//inside a object
dir = direction_check(); //example
//inside the initialize_monster_list
switch(sprite_index)
{
case sprite_Character_Left:
return "left";
break;
case sprite_Character_Right:
return "right";
break;
}
dir will equal "left" if sprite_index is sprite_Character_Left and dir will equal "right" if sprite_index is sprite_Character_Right.
This will return the data structure list ID of the monsterList.
Okay, those are all of the statements that I could find in the Game Maker manual. You may have a little trouble understanding some of them from here, so try to read this until you understand most of it. Don’t forget that some of these statements are advanced so it is easy to imagine how you could have trouble understanding these.
Now that you know statements, there are some other things that you should know:
The ! statement. That means not. E.g.: if image_single!=1 checks if image_single does not equal 1. The not statement can be used instead of !, but ! is faster.
The || statement means or. E.g.: if image_single == 1 || image_single == 2 checks if image_single is equal to 1 or 2. By the way, if you want to be quicker, rather than using || for or you can just use the or statement.
The and statement is explained in the variables section. But just as a quick review, and is the same as &&. It may be a little bit faster to write and. E.g.: if image_single == 1 && image_alpha == 0.5 alarm[0]=60. That script would check if image_single is equal to 1 and if image_alpha is equal to 0.5 and if they all are, alarm 0 is set to 60.
The else statement is one I have already explained as well, but I will again. Else basically means otherwise. E.g.: if gravity == 2.5 alarm[0]=900 else gravity = 2.5. This script checks if gravity is equal to 2.5 and if it is alarm[0[=900 and if it isn’t (which is what else means), gravity is set to 2.5!
Note: Throughout the guide, I have put a period at the end of each code. I just thought that I would make it clear that you don’t use periods in coding. The only time you would use them is with global variables, which are variables that apply to all objects. E.g.: if global.tankai == true is a time where you use a period. By the way, local variables can only be used in the object it is defined in while global variables can be used in all objects!
BASIC COMMANDS
instance_create(x,y,object)
This code creates the instance of an object. The x and y should be changed to the value of the x and y coordinate where the object should be created, and object should be changed to the name of the object that is being created. This is clearly one of the simplest functions in GML.
Alarm[#]=#
This is how you do alarm functions in code. Change the first # sign to the alarm number and the second one to the number of steps that you want to set it to.
instance_place(x,y,object)
This checks if an object is at a certain position. Change x and y to the x and y values you want and object to the name of the object that is being checked.
if place_free(x,y) /*change x and y to the x and y coordinates which you are checking*/
The above code checks if an x and y position has no solid objects in them. To use this code to check for all objects instead, you would say: if place_empty(x,y). And of course, x and y are changed to the value of the x and y coordinate that you are checking. E.g.:
if place_free(500,330) x = 500 y = 330
That would check if x: 500, y: 330 had no solid objects and if it didn’t, it would move to position 500,330. You use place_empty(x,y) the same way so I’ll go no further.
sound_play(sound)
Sound play plays a sound (change the ‘sound’ in parentheses to the name of the sound). If you want to loop a sound (like if it was background music), use this code: sound_loop(sound). And by now you know to change the sound in parentheses to the name of your sound. Here is an example of the sound_play function:
if global.soundoff == 1 sound_play(coolsound1)
GIVE ME ROOM! (SOME CODES INVOLVING ROOMS).
room_goto_previous()
This makes you go to the previous room. E.g.:
if instance_exists(500,500,car) room_goto_previous()
room_goto_next()
This makes you go to the next room. E.g.:
if instance_exists(500,500,car) room_goto_next()
room_goto(room)
The above code makes you go to the room of your choice (change the ‘room’ in the parentheses to the name of the room). E.g.:
if global.leveldone == true room_goto(lvl_complete)
if room_previous(room)<>-1
I know this code looks very complicated, but it is quite simple. It checks if there is a previous room (don’t change the name ‘room’ in there since it is a variable meaning the room itself). E.g.:
if room_previous(room)<>-1 room_goto_previous()
if room_next(room)<>-1
This checks if there is a next room. It is the exact opposite of the above. E.g.:
if room_next(room)<>-1 room_goto_next()
Note: (putting the <>-1 isn’t actually necessary).
OTHER REFERENCE
For learning some other codes, see Ablach Blackrat’s guide that shows you how to do D&
actions in GML. The topic is pinned in the Novice Q & A forum in the GMC. Just for the information, a lot of the codes that I put up were found in that guide, except I explained them a little differently usually.You could also look at General Leo’s guide to GML. Here is the link:
http://www.gmc.yoyogames.com/index.php?showtopic=151003
This guide pretty much covers everything and more than his does, but his might cover a few extra things.
CHAPTER 2.1
Basic GML
The theorem of relativity
Now it is time for you to learn a little more about basic GML, starting off with relativity. You may remember when we did codes like: x+=1. If you were to do that with pure D&
actions, first of all you would have to find the action called: Jump to Given Position. Then, you would have to put 1 in the x box and mark the relative box. Before you even knew about the D&
action, you did the equivalent code. Good job! Perhaps this guide is working out for you.Since we are already on the subject of relativity (by this I mean making things relative, of course), why not learn a little bit more?
If you saved the hit the ball game we made (the first game we made), go to that file. Otherwise, just read what I have to say. Now, when you are in that file, go to the event for the left mouse button. Now, delete all of those actions and we will do all of this in code.
First of all, where it says play sound hit (it should if you added it anyway), type in the code: sound_play(hit). That will make the sound called hit be played. Next, put in: score+=1. That is a great example of relativity; it adds one onto the score. In D&
, you would have to set the score relative to 1.Now, for the jump to a random position action, put this code: x = random(room_width) y = random(room_height). If you don’t know what that does, see the variables section that I made. Now, lastly, say: direction = random(360) speed = 5. You should also know what that does by now.
The code should look like this:
sound_play(hit) score+=1 x = random(room_width) y = random(room_height)
direction = random(360) speed = 5.
I know that you are thinking this code looks messy. We will soon get into how to make code less messy (which is good for finding errors).
Now, my main point in showing you this is so that you can see how to do relativity with variables. If you want something added on or taken off, you just put the variable name and then -=# or +=# (# refers to the number you use). Here is an example:
if instance_exists(me.x,me.y,bullet) health-=10
The above code would check if the object called bullet was at the same x and y coordinate as the object called me and if it was, 10 health is taken away (probably from object me).
To shoot a bullet is a little harder. You must go to the sprite editor and set the origin to the center. Then, say, instance_create(x,y,obj_bullet). You will have to mess around with the x and y values, though, until you get the right value. So, maybe x+18,y-4 works, maybe x-7,y+3 works. You have to mess around with it until it works.
CLEAN THE HOUSE, CLEAN YOUR ROOM, AND WHILE YOU’RE AT IT, CLEAN YOUR CODE TOO!
Now we will discuss how to properly and cleanly write GML codes. I will now show you an example of code that is so messy, your mom would force you to clean it up!
sound_play(shoot) instance_create(500,400,gun) if image_index!=spr_gun image_index=spr_car || image_index=spr_engine if gunai == false alarm[0]=random(200) else if gunai == true alarm[1]=random(360)
If an error for that came up, can you truthfully say that you would be able to easily and quickly find it? I don’t think so. So now I’ll show you how to properly write it:
sound_play(shoot);
instance_create(500,400,gun);
if image_index!=spr_gun
{
image_index=spr_car || image_index=spr_engine
}
if gunai == false alarm[0]=random(200)
else if gunai == true;
alarm[1]=random(360);
Yes, I know. It looks like it would take a lot longer to write script like that. But actually, it only takes maybe 20 seconds longer and you have to admit; it is a lot easier to read and follow. And if there was an error within the script, it would be a lot easier to find! Now that you have seen this, I will discuss the different signs shown above.
{ and } are block statements. { indicates the starting of a block and } indicates the ending of a block. A block is usually used to define actions, meaning things that happen. Here is an example:
if global.tankai == true
{
instance_create(tank.x,tank.y,tank_bullet)
}
That is a pretty simple code, isn’t it? All it would do is check if a variable called global.tankai was equal to the value called “true”, and if it was, it would create an object called tank_bullet at the same x and y coordinate that it was in! But can’t you see how it is easier to follow that then:
if global.tankai == true instance_create(tank.x,tank.y,tank_bullet)
When you look at it that way, you should see what I mean. So now you hopefully now what block functions are.
Now we will learn about ; functions. They are rather simple. They are not necessarily needed, but they can help you keep your code cleaner. You use them after you use a function. E.g.:
instance_create(300,600,waterbottle);
do i+=5 until i==50;
That would create an object called waterbottle at x: 300, y: 600. You probably notice the ; signs after the actions. As you can see, it is used to separate them. It is not necessarily needed, but it makes code look better and it helps you know which actions are separate from each other. So, it is a good idea to use ; signs and { } blocks. If you want to become a pro user, you should use them so that you don’t spend all your time finding errors in your script.
DON’T ARGUE WITH ME!
Now we will talk about actual “scripts” and arguments. Open up Game Maker. Now, one of the file folders on the left side is called: Scripts. Right click and the press: Create Script.
Now, I am going to quickly right this part of the guide since it isn’t too important. Anyway, just write this:
instance_create(argument0,argument1,argument2)
Now, make two objects and give one of them a sprite and name it: obj_thing. Now click on the object with no sprite and name it: obj_controller.
Now, while inside object controller, make a create event and then find the D&
icon called: Execute code. Right next to it is an icon called: Execute Script. Click on it.Now, choose for it to execute the script that you just made and for argument 0 write: 500, for argument 2 write: 300 and for argument 3 write obj_thing (or whatever you named the other object).
Now, make a room and put the controller object in there. Now, click the green button at the top to play the game. All that happens is that an object is created at position 500,300; the same name of the arguments. That is basically a quick tutorial to show you what arguments really are; they are just things that you define the value of so that you can use them in your script. Usually, you can just execute code rather than script if it is more convenient.
CHAPTER 2.2
BASIC GML
Basic AI
AI MATEY
Now you will learn some stuff about basic AI. Here is a list of what you will see/learn:
• AI…What is it?
• Examples of AI
• AI: basic techniques
AI…WHAT IS IT?
AI means artificial intelligence. Usually, when people hear that work they think about a talking computer or something like that. That is not always what AI is.
AI is what a game uses to know how to do things. In the asteroids game that Mark Overmars made, he used this AI:
// This script (that is specific for this game) creates the
// different types of ufo's at reasonable moments.
// It is sort of the AI for the game.
{
// depending on room, set the occurence frequency
life_appear = 1000000;
ufo1_appear = 1000000;
ufo2_appear = 1000000;
ufo3_appear = 1000000;
if (room == room0)
{ life_appear = 800;}
else if (room == room1)
{ life_appear = 800; ufo1_appear = 600; }
else if (room == room2)
{ life_appear = 700; ufo3_appear = 400; }
else if (room == room3)
{ life_appear = 600; ufo1_appear = 500; ufo2_appear = 900; }
else if (room == room4)
{ life_appear = 600; ufo1_appear = 600; ufo2_appear = 700; ufo3_appear = 400}
else
{ life_appear = 600; ufo1_appear = 500; ufo2_appear = 500; ufo3_appear = 400}
// increment the counters since last appearence
life_counter += 1; // time since last lifestar
ufo1_counter += 1; // time since last ufo1
ufo2_counter += 1; // time since last ufo2
ufo3_counter += 1; // time since last ufo3
// now create them when required
if (life_counter + random(life_appear/2) > life_appear && instance_number(rock_small)>1)
{ instance_create(0,0,lifestar); life_counter = 0;}
if (ufo1_counter + random(ufo1_appear/2) > ufo1_appear)
{ instance_create(0,0,ufo1); ufo1_counter = 0;}
if (ufo2_counter + random(ufo2_appear/2) > ufo2_appear)
{ instance_create(0,0,ufo2); ufo2_counter = 0;}
if (ufo3_counter + random(ufo3_appear/2) > ufo3_appear)
{ instance_create(0,0,ufo3); ufo3_counter = 0;}
}
That AI is a little confusing and is more advanced than we will get in this guide. But what that AI did is create a UFO at a certain time depending on the room. So basically, the game needs it to know when to create the UFOs! I showed that just so you would get an example of what AI is. Technically, AI is anything where the game has to do something.
EXAMPLES OF AI
Unfortunately, since AI is something that you learn how to do (not something I can just tell you how to do), the main way that you will have to learn it is through some examples. So I will show you some examples of basic AI.
In enemies create event:
if instance_nearest(100,100,player)
instance_create(sprite_width/2,sprite_height/2,bullet)/*that will create a bullet in the center of the sprite I believe. you may need to mess around with that such as using: spright_width-8 or something like that.*/
alarm[0]=30/*as long as the room speed is 30, this should make it so that the player has to wait a second before doing the next action.*/
Now, in the alarm 0 event:
if instance_nearest(100,100,player)
instance_create(sprite_width/2,spright_height/2,bullet)/*like I said, mess around with the values of sprite_width and sprite_height until it looks right.*/
alarm[0]=30/*This way, alarm[0] should keep being executed until the player is not near (according to the value which you define).*/
I will now explain what all of that would do. It would make it so that the enemy checks if the player is 100 pixels away in either width of height. If so, it would create a bullet from the very center of it and set alarm 0 to 30 (which is 1 second if you keep the room speed at 30). You will notice that alarm 0 has the same code. That means that every second, if you are 100 away from the enemy, it will shoot at you. Then, alarm[0] sets itself again so that it keeps working. Obviously, this doesn’t cover the programming of the bullet itself. For that, there are many things you can do. The simplest one in code would be direction = whatever and speed = whatever, but there is a lot you can do. You could also use this code:
move_towards_point(character.x,character.y,5)/*change the name character to the name of the thing that is being moved towards*/
The above code would make whatever object that is being released move towards the current position of the character, in this case. But if the character moved, the object would still move in the same direction as if the character were still there. This code can be quite useful sometimes.
Just to make a statement, since AI is telling the game to do certain things, a commonly used code would be: instance_create(x,y,object) since you are telling the game to create something. There are many different functions that are good to use for AI. Be sure to look in the manual, or you will never know what you are missing out on learning!
BASIC TECHNIQUES
Well, AI requires a lot of patience. It is often times hard to make AI that works perfectly without encountering any problems. So here are a few basic techniques:
1. Try to start off simple with AI as well. It isn’t the best idea to jump right into a RuneScape or Quake kind of game yet either until you are very experienced.
2. If you are unsure of what a function does, look in the manual. If you still don’t understand it, ask someone on the GMC. It is better to know for sure rather than guess.
3. Be prepared for what you are doing. If you think that you need to learn a few things first, a great way to start is the GM manual. And there is nothing wrong with asking other people either. People specifically go to the GMC just to help people like you and me.
Those are only a few techniques to learning AI, and they also go for learning GML—start off with something simple enough that you can do. If you need to learn about certain functions, look them up in the manual and then ask another player if you don’t understand. And always, I mean ALWAYS, be prepared for what you are doing. Too many projects get abandoned because the person thought that they could do something and then found out that they didn’t know enough. That is why I say start off simple and work your way up.
WRAPPING IT UP
Well, it is time for me to start wrapping up now. But let me tell you a few things:
1. Never let anyone tell you that you can’t do something. If someone tells you: “No, you can’t make a Pacman clone!” or “No, you can’t make an rpg, you are just too bad at programming!” Don’t listen to them. The way that Game Maker is made, you can do almost anything that you could want a game to do. However, listen to advice that seems reasonable. For instance, I am telling you that you shouldn’t jump right into an RPG before you know enough about variables and functions because RPGs require a massive amount of it. You should listen to that. Why? Because it makes sense: if you start making an rpg that soon, chances are you will have many struggles with it and it will make Game Maker too confusing. So when someone gives advice and it makes sense, you should probably take it.
2. Always look at useful examples and tutorials. You can always learn something new. And try to read the manual and understand it. If you don’t understand parts, many people will be glad to answer your questions.
3. If you didn’t catch a few things in this guide, try reading through it again until you think you understand everything.
4. Don’t ask so many questions that people are practically making your game for you. Doing things like that will never help you learn Game Maker.
ANOTHER GUIDE?
I loved writing this guide! It was very fun, and I have a feeling it will help many beginners. Since this is the first version of this guide, as long as enough people read it and make suggestions for me to add on, I will probably make another version of this guide, yes.
Also, I am planning on perhaps making a guide that starts you off as a beginner of AI and gets you advanced. Unfortunately, I have a lot more to learn about AI so don’t expect that kind of guide any time soon. If my guides get popular enough, I may even make more guides than that!
“If you like it let the author know. If you hate it, let the author know why.”
- Open Source Programming Adverb (but applicable anywhere)
That being said, I would appreciate any comments or suggestions you have for this guide. And yes, if you like it, I would like to know why you did and what you thought was good about it (since I hope for this guide to gain popularity so that more people will learn Game Maker programming.
To finish off this tutorial, here are some good GM mentors that you can contact at the forums:
-Insane-: He is a good GM mentor; however he gets many questions a day so if you don’t want to wait long you might consider asking someone else.
enandr: He is one of the mentors who seems to be less busy. If you want your questions answered quickly, he should definitely be your first choice.
e_barroga: Also a good GM mentor who seems to be less busy than some. He is also recommended if you want a question answered quickly.
Most of the other GM mentors would probably work as well. The trick to finding a good one is to see how long they have been on the GMC and the last time that they got on. If they have been on the GMC for a while, chances are that they are experienced. If they haven’t had an account for long though, it doesn’t necessarily mean that they are not experienced or that they can’t answer your questions. These are just tips.
If a mentor got on within the last day or so, chances are that they get on frequently so your question may be answered faster.
There are also a few other people that you can contact. General Leo, who wrote a beginner’s guide to GML. Or you could contact Bitwise Creed as well; he also wrote a guide introducing GML.
Aaron Saltsman wrote a guide to GML, so maybe he could help you. His email address is AaronSaltsman@gmail.com. Stryke just as well wrote a GML tutorial. You could pm him at the forums and ask him questions.
yo1dog likes it when people ask him questions. He likes to give them answers. His only condition: That you do the best to learn what he is saying. To ask him a question, send him a message at the GMC.
Grievous makes many examples. Perhaps if you asked him to make a specific example, he would.
If you think that I would be able to help you, feel free to pm me at the forums. My username is: Phantom_Of3. I might not be as good as some of the other mentioned people, but I know a decent amount about Game Maker so I wouldn’t mind trying to answer. Also, if you would rather email me, just email me at: Phantom_Of3@yahoo.com.
CREDITS
Now, to top it off, here are some people I would like to give special thanks to:
Any GM mentors who help. Many people out there need help, and thanks to you guys they can get it.
All GMC moderators. Without them, who knows what would happen?
Anyone who helps answer questions in Novice Questions and Answers. Thanks to them, many questions are successfully answered.
Anyone who has written a tutorial that helps with Game Maker somehow. Without tutorials, there would be many more people out there who never understood anything!
Mark Overmars, for his wonderful creation that gives hope to people with little computer experience.
Anyone who has answered my questions. Below here I will post some people who answered my questions extra-well:
enandr (he is a good GM mentor who likes helping out a lot).
e_barroga (he answers questions well).
Stryke (for answering my questions about statements like while, until and all of the others)
PwnageMage (for answering my question about what friction is)
You (for reading this guide).
Thank you for reading this guide! I hope it helped you a lot!
-------Phantom
Comments
Loading comments...