Variables

4 minutes
Share the link to this page
Copied
  Completed

Transcript

Hey there. In this lecture, we learn about variables. While programming anything, numbers are used a lot, like in our previous lecture, we used the number five to move our ship. So we use five in all the events. So if you wanted to change the speed of the ship to say eight, then you would have to change all the events one by one to the new number. But there is a way to make it so that you only have to modify that value once for that you can use variables a variable simply a name that holds a value.

So let's say you want to create a variable to store your boom and speed. So you can create a variable called move speed and set its value to five. So this is your own variable, and you're giving it a name and a value. So you can use this variable name somewhere and this value will be used there. So the advantage of using a variable is that you can change its value once and it will change everywhere. So let's open up gamemaker and go to the shape box.

I'll open the creative and we will be creating a variable in this event, since it runs only once in the beginning. In the actual source variable, we need the Assign variable action, so I'll drag it into the event. Now this action is used to create a variable or modify its value if it was already created. So here I'll create a variable with the name move underscore speed, and in the value I'll enter five, so we created a variable called move speed with a value of five. So let's users I'll open the arrow key events and change minus five to minus mu speed and five to move speed. So now instead of using the value five directly, it's going to take that value from the variable.

But now if I change the value of this variable to one and then run the game, the instance will move slower. So now changing this value to it. Well make sense to Move faster. So basically with a variable, I'm able to control a single value in multiple events. So now let's learn more about how variables work. I'll create a new object and name it or test, I'll place it in the room.

Now in this object, I'll add the creative and I'll add the Assign variable action and create a variable called a with a value of zero. Now I'll add the drive and in the actions, authors draw an artist draw value action. This action basically draws a value in the role so you can also draw the value of a variable. Now the caption is syntax that shows up before the value so replace caption with a. Now here I'll enter the value that needs to be drawn which is a variable a. Now these are the x&y coordinates where this value will be drawn, so I leave them as zero by zero Now around the game, you can see that the value of a is being drawn, which is zero.

Now I'll go back to the object and add a step here and I'll add the Assign variable action here. As you know, this action is used to create a variable, but it can also be used to change the value of an existing variable. So in the name, I'll enter a since I want to change as value, I'll take the relative bogs because I want to add to a, and in the value I'll enter one. So whenever the stop event runs, one will be added to a, you can see that in the overview here, it says add one to a and that's what we want. Now run the game and you can see that the value of a keeps going up. In each step event, the value goes up by one and since there are 60 steps in one second, the value goes up by 60 each second.

I'll go back to the object and open the creative and I'll click on the plus sign here to create another variable, I'll name it B and set it to five. Now I'll open the step event and here change one to B. So estab, the value of b, which is five will be added to a, you can see that in the overview here, now we'll run the game. And you can see that a is increasing at a faster rate. So instead of going up by one, Easter is going up by five, which is the value of b. So now you should have a basic idea of how variables work.

They'll be used a lot when we start making our game. So let's do that in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.