October 1, 2023

What Are Functions


In this video, we explore the concept of functions in GameMaker Studio, highlighting their role in simplifying complex codes into single lines and facilitating various game actions.

Learn how to utilize built-in functions, understand the manual, and get introduced to debugging through the “show message” function.

Lesson 5 Transcript

So what we’re going to tackle right now is understanding functions and what they are and why you need to use them.

So if we click on this and press F one to open up the manual, it will take us to the page that explains everything that this does. And it’s a fairly lengthy description and everything, so we’re not going to read all of it.

But essentially what it does, you call this function when you wish to destroy an instance. So that’s how we can remove something from the game while the game is running and you can pass in something called an ID and an execute event flag.

And what that means is if you want to destroy something else, you can say instance, destroy, get rid of all the food or get rid of all the snakes or whatever the case is.

Or you can just call it inside of the object and then it will destroy itself.

Now a function is a block of code that runs when you say that one line and that block of code can be any length that you want it to.

So it can be huge; it can be small; it can call other functions that then call more functions and just do tons and tons of stuff. But essentially you are short-cutting some code into one line right here. And what’s really cool is that GameMaker Studio has tons and tons of these functions in here.

And really, what a game engine is besides the platform to create a game is a bundle of functions that make your life easier as a game developer.

So in GameMaker Studio, if we want to do something, there’s probably a function for that inside of here. If we wanted to get the distance from our snake to our food, there’s a function for that.

If we wanted to get how many foods there were in this room, there’s a function for that. If we want to change rooms, there’s a function for that.

Pretty much everything you’re going to want to do, at least at a basic level, there is a function inside of GameMaker Studio. And knowing what those are is half of the battle.

Because if you want to do something but you don’t know how to describe it, or you don’t know what the function is for it, it can be really, really hard to find it, which is why the manual here is also really, really helpful.

So if we come over here and we click on instance functions, there’s tons of stuff right here. These are all functions that do something and you can just click on it.

And if the name isn’t clear enough, this will just tell you if an instance exists. So if our food object is

In the level, we can check to see if it is there or if it’s not. And there’s functions for everything you want to do.

The really, really cool thing is that you can also create your own functions called Scripts. Now, we’re not going to talk about that now, but we will discuss it when we start making our space shooter.

In that module, we’re going to create our own scripts to do things that GameMaker Studio hasn’t created functions for, but I want to introduce you to just a few functions that we’re going to use quite often.

One of the very useful ones is show message. Now this function is specifically for what’s called debugging, figuring out what’s going on in your game. You would never leave this function in a game that you’re going to ship.

Now when you type out a function, you can see that it’s color changes.

GameMaker Studio always has them in the underscore. So this is how all of them are going to look. Each word is going to be separated by an underscore, and then down here you can actually see what you’re supposed to pass in.

When you pass something into a function, it’s called an argument. Sometimes you have to like this one. If we just close this, this is going to produce an error instance, destroy. We don’t have to pass something in.

This little asterisk down here means that we do not have to pass anything in. We could, but we don’t have to. Now show message says it wants an s t r and s t r is short for string.

So we know how to make a string quote word end quote. Now, if we run this and we collide with our food, we’re going to get a little box that pops up and says Hello.

So this is really, really helpful for when you want to see where your game is at or you want to test something that’s a function that we’ll probably use quite a bit.

Another really helpful function is a math function called random. And there’s a bunch of different random things you can do here.

But essentially what it does is it gets you a number, either a whole number, which is 1, 2, 3, just any number that’s not a point number.

Or you can get just a regular number of any kind. So 1.2 5 6, 2 3 or 25 point whatever. You can set a range between those like a random range to get zero to 10, get any number in there that’s really, really helpful.

Or I random range, which is the whole number or the integer number. These functions will just return a number. So if we were to assign a variable to this function called number equals random, and then we open this up and it tells us

It wants an X, and that’s not exactly helpful. The parameters or the arguments that the GameMaker studio functions want are not always very descriptive and sometimes unhelpful, but random.

You put in a number and on random, let’s take a look at the description. The argument that it wants is the upper range from which the random number will be selected.

Random 100 will return a value from zero to 99. So let’s close that and let’s try random 100. Close that out. And now what we can actually show inside of message instead of hello is our random number because it gives back a number between zero and 100.

So let’s run this one more time and check this out. As soon as we collide, we get a number and we didn’t specify it to be a whole number, so it’s actually a decimal, but that’s still really, really cool.

We’re going to use random in here a lot because a lot of times you want a random thing in your game to happen, because if your game is always the same, it’s not as interesting.

But that is a design topic discussion that we are going to have later on for now. That’s the basics of functions.

I’m going to introduce a lot of new functions as we go through this course, but every time I do, I’m going to explain it. We’re going to look at the manual and I encourage you to play around with it to make sure you understand it.

Because functions are the bread and butter of every game engine. You want to know which ones you can do, how to use them and to just throw them in your game all the time because they will make your life so much easier.

But that’s all I’ve got for function.

So let’s move on to instead of just destroying the food, actually acting like the snake has eaten it by increasing the score and spawning a new food somewhere else in the level.

Leave a Reply

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