How to Program a Simple Weapon System (Guide)

Game Maker Studio 2Gameplay Tutorials

One of my most popular series is creating a dynamic weapon system in Construct.

dynamic weapon system is when you swap between all weapons in your game that can be triggered as manual (pressed/tapped) or automatic (held down).

Now, we are going to do the same in Game Maker, and it’s really easy.

Triggering a bullet automatically is all about using the correct input control. Whether you use a keyboard, mouse or gamepad, all you have to do is find the “down” command.

How do you use the down command?

When working in Game Maker Studio 2, the down command is not explicitly stated like the pressed command is. So the code for holding down a mouse button would be this:


In this example, we are checking to see if the left mouse button is being held down. Therefore we have our trigger for an automatic weapon.

To check for a manual based weapon we need to explicitly tell Game Maker like so:


By adding _pressed, we are now just checking to see if the left mouse button was clicked, and not held down.

How do you make an automatic weapon based on this?

Now that we know how to get the input, how do we actually fire a bullet automatically?

We wrap it in an if statement like so:

if (mouse_check_button(mb_left)) {
    //fire bullet automatically

Because of the if statement, we are checking to see if we are holding the button down, and if so, the code inside should continuously fire the bullet.

But.. how could we make both automatic and manual weapons as a dynamic system?

The easiest way to do this is to store all the different types of weapons within one simple integer variable named weapon (in the create event) like so:

//create event
weapon = 1;

Lets say we have 4 weapons:

  1. Pistol (manual)
  2. Shotgun (Manual)
  3. Machine-gun (Automatic)
  4. Automatic Shotgun (automatic)

Each number represents that weapon. So, 1 (default) our player is firing the pistol.

If weapon equals 2, the player would then fire the shotgun, so on and so forth.

How do we swap between them?

The easiest way to go about this is to write 2 if statements to control:

  1. The maximum amount of weapons we have
  2. Swap between them.

Since we have a weapon variable, all we need to do is add to it.

By default we have it at 1. Lets add 1 to it every-time we press the space key like so:

//step event

The above code is triggered when we press (tap) the space key, and increment our weapon variable by 1. Now when we tap space, our weapon variable equals 2 (shotgun). Tap it again, 3(machine gun). Tap it again 4(auto shotgun). Tap it again 5(uhhhh… we don’t have 5!).

Now we’ve gone too far.

How do we control the max amount of weapons we have?

Simply compare weapon to see if it is over our limit of weapons like so:

//step event
if (weapon > 4){
    weapon = 1;

This if statement resets the weapon variable back to 1 after we go over the maximum amount of weapons we’ve programmed.

What’s the downside of doing it this way?

There are a couple improvements we could make to this…

But for today, we want to keep it simple and have it work, so the more weapons you add, you have to remember to change that number!

Recommended: Make the hard-coded max weapon number into a variable called max_weapon and change that every time you make a new weapon.

How do we fire off automatic and manual weapons with this system?

Now that we know how to detect input, and store weapons, we can fire them effortlessly within what are called nested if statements like so:

//step event
//manual weapons


    if (weapon == 1){
        // fire pistol
    if (weapon == 2){
        // fire shotgun


//automatic weapons


    if (weapon == 3){
        // fire machine gun
    if (weapon == 4){
        // fire auto shotgun

Your Quest (+10 xp): Open up your desired software, and program your own dynamic weapon system.

Your Challenge (+50 xp): Add a bullet object that has a speed, and is destroyed when it is outside of the layout.

If you read this far, feel free to reply to this e-mail and let me know what you thought of this tutorial!

Good luck adventurer,