Home > SoulHow > SoulHow to actually do stuff with GML (Game Maker): Part 2

SoulHow to actually do stuff with GML (Game Maker): Part 2

June 7, 2008

NOTE: Comments are locked. I no longer answer questions about the Game Maker tutorials on this blog; I suggest you take any questions to the Game Maker Community. For more info, view the FAQ page.

Welcome back. The first part of this article can be found here. We left off just as we went 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 (and since vspeed adds its value to y every step), 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 boolean logic. 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“.  There is one more, xor (or “exclusive or”) but it is not necessary to describe in this guide.  Google it if you’re interested.

  • and – all parts of expression must be true for entire expression to be true
  • or – only one part of expression must be true for entire expression to be true
  • not – expression returns true when it is false and false when it is true

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 (“if a is smaller than 7 or b is greater than 4, set c to 1″). 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 current objective? Oh yeah, we want the object to turn around when either it hits the top of the screen or when 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 the top or bottom of the screen. Take a look:

if (y<=0 or y>=room_height) vspeed=-vspeed;

Hmm two new symbols.  Fear not, for they’re pretty simple to explain; <= reads “is less than or equal to” and >= reads “is greater than or equal to”.  For example, 6 <= 8, 2 >= 0, 5 >= 5, etc.

Anyways, as you can see, we check if the object’s y coordinate is either less than or equal to 0 or greater than or equal to 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 and do something. 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 will have an hp of 3. Each time it gets 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 (recall that 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.  Remember 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 and ignored if the expression returns false.

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 originally written in whatever object is inside the parenthesis. This can also be used with the { and } trick. “other” means we are talking about 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 I’ve written, 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.  Note, as mentioned below, I do not respond to questions asking to solve a particular problem one might have with one’s game; only comments which ask for clarification.

Good luck, friend.

If you enjoyed this article, please consider checking out the rest of the blog.

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 the Game Maker Community and ask there.  I can’t answer any such questions in this blog and the members at the GMC will be more than capable to help you.  For more info, go to this blog’s FAQ page.

Advertisements
Categories: SoulHow
  1. Mark
    September 26, 2009 at 3:49 am

    Haha! Dead-cool!

  2. Joel
    August 11, 2009 at 9:23 pm

    This tutorial is very useful, VERY USEFUL!

  3. Apotheosis
    May 25, 2009 at 3:09 pm

    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!

  4. Pe-ads
    March 18, 2009 at 7:09 am

    Thanks for the tutorial!

  5. Ian Simpson
    February 7, 2009 at 4:42 pm

    Brilliant – thank you for writing this.

  6. Burns
    September 9, 2008 at 3:46 pm

    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!

  7. Corey
    August 21, 2008 at 8:45 pm

    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.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: