Lesson 3: Comments
We take a break from the coding process to discuss the important yet often overlooked aspect of adding comments to your code. Give your variables descriptive names and discover how to create both single and multi-line comments in GameMaker to make your code more readable and manageable.
Lesson 3 Transcript
I want to take a quick break from coding and talk about something that is fairly important, but oftentimes gets overlooked. And that is comments.
So anytime you’re working on a complex, large project, it’s important to leave comments both for yourself, for your future self, or for anyone else that you’re working on.
The way we are doing it is by making our variables descriptive names. That is super helpful in the long run. But the other way is to actually leave comments in the code itself. And you’ve already seen these.
When we make a new event, this top line here is actually a comment, so we’ve ignored it for now, but let’s take a look at it more in depth.
Every event, you can have a description here at the top, and if we add something right here, save it. It actually shows up over here in the event, which is pretty cool.
Sometimes this can be really helpful, other times it might not be necessary. But setting a description on these is a good thing to know how to do. And the other thing that’s really important is to actually leave inline comments.
And there are two ways to do this. Two slashes, which are right next to the shift key, will make that line completely into a comment.
So you can then type anything you want here. And GameMaker will not try to do anything with this. This is only for you reading it. This is also a really helpful way of commenting out code that isn’t working or you’re trying different things with.
You can comment it out and then you can uncon it out later. So that’s helpful too. The other way of relieving a comment is called a multi-line comment, which is a slash, and then an asterisk, which on mine is shift eight.
And this turns it into a multi-line comment. So you end it with another asterisk and another slash somewhere down the road.
And everything in between here is taken as a comment. This is a great way to comment out a large comment.
If you need to explain something that you’re doing that’s very complicated, or if you have a big chunk of code that you want to take out because you want to test something different, that’s a quick and easy way to do that.
So use comments because they are essential for maintaining sanity. When your project starts getting larger, get into the habit of doing it now on smaller projects, and it will just be easier later on.
And now let’s get back to the next video to create what we’re going to eat as our snake.
Lesson 4: Snake Food
In this video, we take the next step in our Snake game development by creating food for the snake to collide with and “eat.” Learn how to create a new sprite for the food, set its dimensions, and assign it a color before placing it in the game room.
Lesson 4 Transcript
So what we’re going to do now is create the food that our snake object is going to be able to collide with and destroy, and then we’ll be able to add to our score. So let’s first make a Sprite.
Let’s come in here, right-click create Sprite. We’re going to name this S P R food, and we want this to be significantly smaller.
So let’s go ahead and resize this image and it’s going to be just 16 by 16. And press apply full screen just so that we can see the image. Double-click to open up the editor.
And I’m just going to do a yellow, choose any color and make an ellipse just like that. And that’s all we need to do for the Sprite.
Let’s come down and make an object called O B J Food. Assign that Sprite to it, and then go into our room and let’s place that food right here.
So now we have two objects in our room, the snake we can control and it moves. The food has no logic in it right now. It just has a Sprite associated with it. Nothing else.
So we’ve talked about events already. What event can we use to make it so that when we touch this food, it goes away?
It can add an event called collision. And down here you can see that now that we’ve added more objects, we have more choices for collision. So what we want to do is add a collision with O B J Snake, and I’m going to say destroy self.
So now how do we actually go about destroying ourselves?
We can’t be watching the player and pause the game every time they touch it and come in and remove the object. That doesn’t work. We know how to add objects like this, but that’s not the same as destroying them.
Instead, what we have to do is use something called a function. A function is a block of code that does something when you call it, and I’m going to take the whole next video to talk about that.
But what I want to do right now is just show off the one we need to get rid of this food when we touch it. So type out instance underscore, destroy, and then do brackets.
Okay? Instance underscore destroy. Now press F5. Let’s collide with this. And you should see that it goes away instantly.
There we go. So what is this doing?
Well, it’s doing exactly what it says because we collided with something. This calls this code and it just removes itself from the game.
But how exactly does that work? And what exactly is it doing underneath the hood? That is what we’re going to talk about next.