In this tutorial, Aaron discusses the significance of variables in GameMaker and in programming languages in general. He guides you through the process of creating a variable, navigating potential warnings, and underscores the importance of using descriptive names for variables.
Lesson 6 Transcript
Variables inside of GameMaker and pretty much across all languages are data that you can access and manipulate by a name that you set. This is super valuable information.
You’re going to be using variables left, right and center as soon as you understand how to do it. So let me show you.
We already have a creative event inside of here, but if you don’t add one now the creative event is a great place to set variables because it only triggers one time and you can make them just once and then you can access those variables everywhere.
So the term variable really just means changing something that changes depending on circumstance, and that’s really what it is. So a variable can be any name and it can hold anything that you want.
So let’s test it out. Let’s create a variable called speed.
If we type that, we suddenly get a list that pops up and these say built-in variables and then we have constants and functions and what is all this?
Okay, let’s not worry too much about the rest of these, but let’s talk about built-in variables really quickly. Because a built-in variable is a reserved word that you are not allowed to use.
You can set these but you can’t do a whole lot more than that. You can read them and set them, but GameMaker Studio has already said this is going to do something very specific and you are not going to alter what that can do.
So these are very helpful and we’re actually going to use them here and there, but you cannot control what speed actually does inside of your game. So if it has green, it is built in and we might want to rethink about using it instead.
Let’s name this something different. Let’s call it move speed, and then that’s our variable, but we have to set it to something. So if we don’t set it, we get an error over here that says unnecessary expression, move speed, used as a statement, blah, blah, blah.
We’ll talk about what that actually means later because it’s not that helpful of an error statement, but we have to use the equal sign and then give it some kind of value.
What I want to do is give it a value of three and then a semicolon. So now essentially you can think of move speed as equaling three. So that’s really cool. We can then use this anywhere, but you might be wondering why we need to do that if we can just put three there instead well move speed.
You can now put lots and
Lots of places to control all of the movement of your player. And then if at any time you wanted to change it to increase it, decrease it, stop it. You just have to change it in this one spot.
So let’s come into the step event and let’s say instead of adding five, let’s add our move speed. So already we have this in two places and if we want to change it, we can just come in here and change it right there and now these have both been changed.
You can start to imagine when you have a lot of events with a lot of movement, you want to control it with a variable because it will save you so much time. The rule of thumb here is that if you use some piece of data or a number or anything like that more than one time, you should make it into a variable.
So we’ve done that.
Now, a variable can also hold other kinds of data such as if we type in my name. The way we actually put in text inside of our game is with double quotes and double quotes.
This is what’s referred to as a string. A string is just a line of text. That’s essentially all you need to know. There’s some technical jargon that you could get into, but GameMaker Studio just says, everything inside of these quotes is considered a string and I’ll do anything you want with it, assuming that it’s allowed.
So some of the things that you can do with a string are find out what’s inside of it. You can alter its contents, you can get just the numbers, you can get just the letters, pretty much anything that you want to manipulate when it comes to a string, you can do, but it has to know that it’s a string.
Now, GameMaker Studio is super relaxed, like I’ve said when it comes to variables as well.
So we can come in here and now set my name equal to 10 instead. And now my name equals 10, not Aaron, which is kind of confusing other programming languages. If you come and try and do this, it will throw an error because there are different data types.
We’re not going to get into that too much right now, but essentially GameMaker Studio is very relaxed and easy to work with.
Now you’ve probably seen this pop up a couple times already. This is a warning. This will not keep your game from running. You can still run it totally fine. If I press F five right now, the game will run. All right.
This warning simply means I have not used this variable anywhere else. And a lot of times when you get that warning it’s because you misspelled something. So if we come into the step event and I said
Move speed without a capital S, all of a sudden I’m going to get this warning over here, and that should be a flag to me to say, oh, I’ve made this variable in here. So if I’m getting a warning that it hasn’t been set or used anywhere else, I’ve probably mistyped something.
So let’s fix that.
Okay, now we’ve got that fixed, this warning, that’s okay. Just make sure that if it’s a warning in the Create event, you’re probably fine. If it’s in another event, then you’re in trouble. So make sure you watch that.
So we’re going to be using variables all over the place and all the time. So you make them with a name and I’m going to highly encourage you to use a descriptive name. Some programmers don’t and it can get really hard to tell what’s going on because you do not have to say, move speed.
I could say that is my variable name equal to one, and I could copy this and I could paste it in here and I could run and it will work. Nothing wrong with that. GameMaker does not care what the name of it is, as long as it’s not a built-in variable.
But this right here is not useful. You are never going to remember what this is supposed to be, and you’ll have a very hard time typing it out anywhere else. So I’m going to use descriptive names as we go along.
Sometimes we’re going to be a little bit longer, but it will be much more helpful if you ever take a break or come back or you’re working with someone to know exactly what this variable does and probably where it is getting used.
So that’s just some good advice that I encourage you, highly encourage you to take up. Next, I’m going to introduce you to the manual, which sounds super boring, but GameMaker’s Manual is super helpful and actually one of the best that you will find when it comes to helping you program.