Welcome back. The first part of this article can be found here. We left off just as we went back to the target object, after configuring the bullet.
So, we want this object to move up and down until it’s hit by a bullet. So once again, add a create event, and in it put an execute code action. In this code box, we are going to use a variable similar to hspeed, but for moving up and down (vertically). It makes sense that this would be called vspeed.
So type this code:
vspeed=10;
Recall that because higher y values move down, this will send the target downwards.
We don’t want the target going off the screen, so we have to tell GM that when we hit the top (y=0) or bottom (y=room_height) of the room, we go the other way. For this, we’re going to use what’s called a boolean operator. Remember when I was talking about the true and false stuff? Well that’s called booleans. Basically it means stuff that has to do with either the number 1 (which represents “true“) or 0 (which represents “false“). The three most used boolean operators, which would naturally do stuff with booleans, are called “and“, “or“, and “not“.
- and – all parts of expression must be true
- or – only one part of expression must be true
- not – changes true into false and visa versa
Basically when we use one of these operators, we split an if expression into two parts, and join them with one of these. Check the following code:
a=5;
b=3;
if (a<7 and b>4) c=1;
a is set to 5, and b to 3. Once again, let’s read the next line out loud. “If a is smaller than 7 and b is greater than 4, set c to 1.” Well 5 is smaller than 7, but 3 is not greater than 4, so because both parts don’t return “true” the whole thing inside the parenthesis turns out to false. Read the line out loud again and hear that this is true. Changing the “and” to “or” changes the final result; had we used “or” instead of “and“, the whole if expression would return true because only one part must be true. Finally, had we used something like “if not(a<7) c=1″, which utilizes the “not” operator, we would take the result of “a<7″ (true) and change it to its opposite (false). Therefore the whole expression is false and c is never set to 1.
Okay, once again, sorry, I’ve branched off into some other discussion. Back to our game. Now what was our original objective? Oh yeah, we want the object to turn around when either it hits the top of the screen or it hits the bottom of the screen. Place this code in a code box in the step event of the target object; we want to continuously check whether the object hits a wall. Take a look:
if (y<0 or y>room_height) vspeed=-vspeed;
As you can see, we check if the object’s y coordinate is either less than 0 or greater than the room’s height and if so, we do something mildly odd: vspeed=-vspeed. What this does is “reverse” the vertical speed; so if vspeed were 10 (moving down), it would be set to -10 (moving up), and visa versa.
Finally, add a collision event with objBullet so the bullet can hit the target. Inside this collision event, put yet another code box and type the following:
show_message("Hit!");
game_restart();
Now we have two functions – show_message, and game_restart. game_restart is obvious; it restarts the whole game and doesn’t need you to give it any arguments. As for show_message, it brings up a message box showing the text in the string you type. Remember that whatever you want to come up must be contained in double quotes because it’s a string. See the SoulHow to code with GML (here) if you’re unfamiliar with strings.
So now run your game and watch as everything works perfectly. The bullets fire, and if you hit the target, a message comes up and the game restarts. But let’s do one last thing. Let’s make it so that the target’s a little stronger. It should be able to take a few shots before being destroyed. So we want to give it an hp variable!
Open up the target object again and go to the create event. Add a second execute code action and in it place some code to make your hp variable:
hp=3;
Now the target has an hp of 3. Each time we get hit with a bullet, we want to take away from this hp, until it reaches 0 at which time we show the message and restart the game. As our last bit of coding we’re going to write for this guide, go back to the collision with bullet event and change the code to include our variable. We’re going to use a -= line, an if statement, and the two lines we already had there:
hp-=1;
if (hp<=0)
{
show_message("Hit!");
game_restart();
}
with (other) instance_destroy();
So in this code, we deduct 1 from the hp, and then we get to our if statement. This works just like all the other ones we’ve used, but the <= means we’re check if y is smaller than or equal to 0. That way, we catch 0 and any negatives that might somehow pop up. If this expression returns true, we enter the code block and perform both lines inside it. Recall that when using the { and }, it turns into a block which means that all lines inside the curly braces are read when the expression returns true.
Now there’s one more thing that you may not understand: the last line, which is called a “with” statement. Basically what this does, is it takes the line of code after it and reads it as if it were in whatever object is inside the parenthesis. This can also be used with the { and } trick. “other” means we use the other object involved in a collision. So this destroys the “other” object. We need to do this because if we didn’t, the bullet would continue going and trigger the collision event multiple times. Before, we were just restarting the game right away so it didn’t matter.
Re-run the game and see how you have to hit the target 3 times before it shows the message. Hooray!
We’re finally done. I hope you’ve taken in all that, and I also hope you can use it to help with your games. If you found something unclear, you can either go back and reread it, or just post a comment with your question and I will try to clear it up both in a reply-comment and by editing the original article. Good luck, friend.
Got questions? Complaints? Found an error that I missed? Just want to say hi? Post a comment!
If you have a technical question, such as “How do I do this” or “this is not working right” (relative to something with your Game Maker game after reading this tutorial), please head over to this community(SoulRed Productions Community) and post in the “SoulScroll discussion” forum.
Filed under: SoulHow
Awesome that helped so much!!! I’m making a game with coding so I looked it up online, saw this, read it, and now my game will go somewhere! You should do more tutorials on the other variables.
Great tutorial, this is the first free resource I’ve found that clearly explains how to perform each step, what it does and why it does it.
Much appreciated!
Brilliant – thank you for writing this.
Thanks for the tutorial!
Dude, your tutorials are awesome!
i used the first pieces to code my first mana bar,
with this other stuff im going to code a fishing program,
only easy stuff, just so i can start out
=D
thanks!