Event-Based Programming
Summary
In this video, Aaron explains the basics of GameMaker Studio and the fundamental concepts of 2D game development. He discusses:
how the X and Y coordinates work in the GameMaker grid system,
how to modify object properties, and
the importance of the ‘step’ event that runs every frame of the game.
He also covers the significance of different events like ‘create’, ‘destroy’, ‘collision’, etc., and touches upon the importance of semicolons in programming languages, despite GameMaker being lenient about their use.
Lesson 5 Transcript
So with our project still up, let’s take a few minutes to talk about what we did.
In that last video. We created a Sprite, an object assigned an event to it, and wrote some code. But what does this code do?
Well inside of GameMaker studio and with two D games in general, they run on a grid. So if we open up our room, we can actually see down here these numbers are the X and Y on our grid, and you can actually see the grid here because we have toggle grid.
If we take that off, it gets rid of that whole toggling system, but we’ll keep it on because I think it makes sense to see it. So this grid, oh, and by the way, the way I’m zooming in and out is holding control and using my mouse wheel to go in and out just in case you ever need to do that.
(00:55)
If you ever want to full screen, you can click this center fit button, which is also really, really helpful. So this grid is where everything is at your level.
In every single room, you’re going to have this grid and it has an X component and a Y component. So the X component is from left to right. So you can see if we move our mouse, it gets bigger and bigger and bigger.
Now if we move down, the Y component gets bigger and bigger. That is how we think about it. The top left is going to be zero. Zero, bottom right is going to be the maximum of our level, both in width and in height.
So looking at our code, when we typed Y, that is where this object is currently at. So if we double-click, we can actually see a bunch of properties.
(01:52)
Now they’re kind of hard to spot, so I’m going to hold control and zoom in a lot. Now we can see that there’s a lot of stuff in here, but if we come and look right here where it says X, it says four 80. So that is the X coordinate of our player.
The Y coordinate is 64. So we get the Y coordinate with this keyword. A keyword is a reserved word inside of GameMaker studio or inside of any programming language.
So Y refers to the Y position of our player and we are saying, take Y, which is where we are, add five to it, and then assign that back to our why. And we do that inside of a step event. Now a step event is just something that runs every frame of your game.
And games these days all run at 60 frames per second.
(02:57)
So this logic is happening 60 times every single second, which is really, really impressive. Computers are very powerful.
So getting the Y coordinate, assigning, which is one equal sign, two are y plus five, and then it just does that over and over, which is why the player moves down and down and down because the Y coordinate goes from the top to the bottom.
So if we are increasing the Y by five every single frame of our game, then it’s going to move down, down, down. So let’s try something different.
What if we only said Y equals five? Well, that means our Y position will be set to five 60 times a second, but it’s just setting it to the same spot so it doesn’t move, it’s just stuck right there.
Okay, that’s not as interesting.
Let’s press Ctrl Z to bring that back. Alright, so we can increase our Y.
(04:02)
Can we also increase the X in the same way? Sure, X equals x plus five semicolons. Now you might also be wondering, we’re going to increase the size of this first. You can increase the size with F8 and you can decrease it with F7. You might be wondering what is the semicolon all about anyway?
Well, in most programming languages you have to tell the computer when you are done with that specific piece of logic. And a lot of times that’s done with a semi.
So GameMaker studio is very, very, what I would call generous and lenient. So if we leave off a semicolon, nothing bad will happen. It will still run and it will still work perfectly fine.
But in other programming languages, if you ever move out something like C, if you leave off a semicolon, the program will not run at all.
(05:02)
You’ll get big red errors and you’ll get nothing at all. So you have to have it in some languages and it doesn’t matter in others.
GameMaker Studio is lenient so you can have it or you cannot, but I tend to put mine there simply because it’s a good programming practice because you might not always use GML and someday you might move on to something else.
And when you do that, having good coding practices in place will save you a lot of time and frustration. Let’s run this now and see if this works.
(05:39)
There we go. He’s moving down five a second and he is moving right five a second, which is exactly what we would expect.
Now, what are the other events inside of GameMaker studio? Well, if we click at event, we can actually see them and most of them are self-explanatory.
Create happens one time when it gets created only once and you can put the code inside of there. This event is really great and we’re going to use it all the time destroy.
So when we destroy that object, this code will run also very useful, clean up. Not going to worry about that right now, so don’t even worry about it.
Step event, you have step, which is kind of the middle one, begin and end. If you ever really needed to control the order of your code, which is very important, you could do that with these begin and end steps.
(06:35)
Alarms we’re going to talk about later, but basically it’s a timer that you can set and then it will run after that set amount of time, draw event, really, really great and it allows you to draw Sprites to the screen that you don’t have objects for.
This is something we’ll also use a lot. Mouse controls, everything you do with the mouse, moving it around, pressing keys down, this is really helpful.
Key down is when a key is being held down. So any key on your keyboards, you can check right here with this event.
Key pressed will only trigger once when the key gets pressed. Same thing for key up. When it is released, that code will run. Gesture is for mobile-based games, very useful but not something we’re going to be talking about. Collision is when objects actually start touching each other in the world.
(07:31)
This is very helpful and we will use this quite a bit. There is another, which contains a lot of ones such as outside the room intersecting the boundary of your level, game start, room start, animation end, and so on and so forth. These are all really useful and we’re going to touch on quite a few of them as we get going.
And the last one is asynchronous, which we’re not going to worry about at all. They are for doing stuff online with web games or for getting information from the internet, which we’re not going to worry about right now.
So those are the events that you can have.
They trigger when they basically say they will. So create and destroy and step event is really the one that is kind of strange. Step events will trigger every single frame of your game.
All of the other events are purely event-based. So when this gets created, the create event will trigger and for the most part, that’s what most of the events are.
When something happens, GameMaker will go in and check all of the objects that are on your level and say, Hey, we just started a room. Does this object have a room start event? Does this have a game start event? If so, let’s run that code. Otherwise don’t worry about it.
So GameMaker is event-driven, which is really, really great. The
(08:54):
Step is kind of a cheat because you can drive events with it. Instead of having events drive, we’re going to use the step event a lot to our advantage.
It is very, very crucial, but we’re going to talk about all of this, how to use them when to use them, and what to put in them as we go about making our games. What I want to introduce you to next is variables, what they are, how to use them, and why we absolutely need them in our games.
Leave a Reply