Basic Ammo

In this tutorial you will learn how to add Ammo for your character.


 

What you need

Before you start, you will need a basic game engine with a character and a shooting function.
You will also need a sprite for your ammo pick up box.

Download what you need:

Basic Shooting Engine
Ammo Sprite


When adding something to a game like ammo, you will need to remember the ammount you have during the game. YOu can use a variable for this but when you add a variable to an object, the value is forgotten when you move to the next level.

You can solve this by making a global variable, this basically remembers a value for every level in the game, when you change level, the value is not changed or forgotten.

To make a global variable, you need to set it when the game starts, we do this in the first room in the game using an object to set the value.


 

Creating the global ammo variable

We need to create an object that will set our variable, you might already a set game object, if you do, add the code to the one you have, you don't need two set game objects.

If you don't have a set game object,

Create a new object and call it, o_set_game

You won't need to give this object a sprite.

Event Create On the set game object, add a Create Event

We will now set a global variable, to set a global variable, when typing the variable name, you have to put, global. before the variable name so the game knows that it should be remembered on every level.

Icon Set Var Use the Set Variable Action from the Control Tab

Set the variable name as, global.ammo and set the value as 20

You can set the ammo to whatever you want, this is how many bullets you will start with.

Now put the set game object somewhere in the first room of the game,

Example Hidden Object

Because this object has no sprite, in the level editor, it will show as a blue circle with a question mark in it.

Don't worry, this won't show in your game, it only shows up to let you know there is a hidden object in your level.

Example Global Variable



 

Stopping your character shooting if there is no ammo

We only want our character to be able to shoot when you have ammo.

To do this, when we press the shoot button, before the bullet is created, we will check there is enough ammo, if there isn't, we won't create and fire a bullet, if there is, we will fire a bullet and reduce the ammo count by one.

Open your Character Object and go onto the Key Event where you shoot bullets.

We want to check the value of a variable, there is an Action to do this, IF Variable has Value

Icon IF Var From the Control tab, drag in the IF Variable has Value question action, the settings will appear.

First, you have to type in what variable you want to check the value of,

Type in, global.ammo for the variable

We next have to choose the opertaion, basically, what we want to check, this can be, if it is equal too, smaller than or larger than a value.

Select, small than, as the operation

We now have to type in the value we want to see if the variable (global.ammo) is operation (smaller than) the value (1)

Type in, 1 as the value.

This will now check if the ammo variable is less than one.

Example Var Check


With a question action like this, if the awnser to the question is true, it will do the next action block after if, if it isn't true, it won't do the block after it, it will do the next.

What we want to happen if the ammo is less than one is to just not fire a bullet, we can use the Exit Event Action for this. The Exit Event Action stops any of the actions after it from happening, so we will use this to stop the create bullet action and reduce ammo action from happening.

Action Exit Event Drag the Exit Event Action from the Control tab BELOW the IF Variable has Value Question Action.

Doing this will ONLY make Exit Event Action happen if the global.ammo value is LESS THAN 1, so 0 or less.


 

Reducing the Ammo

Now we want to reduce the ammo after we fire a bullet.

Icon Set Var Use the Set Variable Action from the Control Tab

In the settings,

Set the variable as, global.ammo

Set the value as, -1 and tick relative (relative to add or subtract from the value)

This will take 1 away from the ammount of ammo the player has.

Your actions for the Shoot Keyboard Button event should look something like this, click here


 

Adding an Ammo Box Pick Up

This is very simple to do,

Create a new Object and call it, o_ammo

Add a Collision Event with your Character Object

On the Collision Event,

Icon Destroy Instance Add the Destroy Instance Action to destroy Self, removing the ammo box on collision
Icon Set Variable Add the Set Variable Action, variable name is, global.ammo and value is 10 relative

Your actions for the ammo box should look like this, click here

 

That's it, you've made an ammo box, you can make as many of these as you want, try using parenting and a variable on the ammo boxes to save time when doing collision events.


 

Download Working Example

ammo_basic.gm6


 

Still Having Problems?

 

When I press the Space Bar or try to pick up the Ammo I get an error about a variable.

Make sure that you've spelt the variable name the same in all the places you use it, if a variable is spelt wrong or has letters in the wrong case, it will give you an error.


I can only fire one bullet but then no more.

On the Bullet Fire event, make sure the Set Variable is relative or it will instantly set ammo to -1.


When I pick up the ammo, even if I have 100 ammo, it sets my ammo a lower number.

Again, check that the Set Variable on the Ammo and Character collision is ticked relative.


How can I show the Ammo on the screen?

Have a look at the Displaying Variables on the Screen tutorial. Basically, make an object for display, add a Draw Event, from the Control tab, use the Draw Variable Action, set the variable as global.ammo and the x / y values as you want.