Creating a Pistol [and Reloading it!]

  • 4 favourites


Attached Files

The following files have been attached to this tutorial:



Download now 178.95 KB


1,795 visits, 3,033 views


This tutorial hasn't been translated.


Scripting the Pistol

Delete the player mirroring

Found in the platformer template's script (default: Event Sheet 1).

You'll be defining your own mirroring in the next step.

Align the Pistol and Redefine the Mirroring

Line 7: Every tick, set PlayerWeapon's Position to the Player at image point (1). Set the angle of PlayerWeapon towards the position (Mouse.X, Mouse.Y).

This makes it so that the Player looks like they are

carrying and aiming their weapon towards the mouse.

Line 8: If the X position of the Player is less than X position of the mouse, don't mirror the Player and don't flip their PlayerWeapon. (e.g. Player is at 100 and Mouse is at 150)

Faces the Player to their default direction

(to the right) and leaves the gun right side up.

Line 9: If the X position of the Player is greater than the X position of the mouse,

mirror the Player and flip their PlayerWeapon. (e.g. Player is at 100 and Mouse is at 50)

Mirror flips the Player sprite (to the left)

and flips the gun vertically because when the

gun rotates behind the player it's upside down,

so flipping it puts it right side up again.

Create a user interface for the pistol

In the layout, create two separate text objects named

Player_PistolClip and Player_PistolAmmo respectively.

Place them in the upper left corner and make sure they're

on the UI layer of the platformer template.

It's optional but you could also set both texts to a number like 0

or 8/50 so that you know they represent a bullet count.

The UI layer is set to parallax 0,0 which will prevent it

from scrolling as the player moves around. The Game layer

is 100,100 which makes the layer scroll. Also quickly check to make

sure that the Player and their PlayerWeapon is on the Game layer.

Line 10: Every tick that the player is not reloading, set Player_PistolClip text

object to the value of PistolClip, and Player_PistolAmmo to the value

of PistolAmmo. Later on these numbers will change smoothly to let the player

know how much bullets they have left.

This is so that it looks like the player has loaded in a full magazine, but in reality

they loaded all they had left. In this case (8/50) if the player has (8/7) left, they would look like they loaded in a cartridge only full of 7 bullets (and the logic for that may be that they forgot to load bullets or took a partially used magazine).

Create three global variables:

PistolReloading (0 or 1) [false or true]- Checks if the player is currently reloading

PistolClip (set to 8)- The amount of shots in one pistol magazine

PistolAmmo (set to 50)- The amount of ammo the player is carrying

The variables are global so that throughout the entire game

(e.g. the player moves to another layout) they will still have

the same amount of clip and ammo. Also, it is ammo and

not cartridges in case the player has less ammo than their clip to load.

Making the Left Mouse Fire

Line 11: When the Left mouse button is clicked, the player is not reloading and the pistol has bullets in its clip...

The sub-event is there because On Left button Clicked is already a trigger and two triggers can't be on the same event. 0.15 Seconds is also a sub event of On Left Click because if line 12 weren't attached to 11, it would think they're two separate events. Thus, line 12 by itself would trigger without a left click of its own.

After the conditions are satisfied in line 10 the script moves on to line 11.

Line 12 (sub event of 11): ...then every 0.15 seconds, Spawn a bullet in the pistol and remove a bullet from the pistol's clip.

This limits the fire rate of the pistol. If you do not want fire rate or want it to fire slower, remove it or increase the amount of seconds.

Initiate the Reloading with R

or Auto-Reload

Line 13: When R is pressed, the player is not reloading, has less bullets than a full magazine, and the player still has ammo, get ready to reload the weapon.

**If you remove line 13, then the player cannot reload manually and must empty

out their clip.**

Line 14: When the pistol has less bullets than a full magazine and the player still

has ammo, get ready to reload the weapon. Trigger this once.

The trigger once is not for the mechanics but for sound so that the audio won't

keep triggering over and over while the player is reloading.

If you remove line 14, then the pistol must reload manually.

Calculate the Ammo

Line 15: While the player is reloading, has less bullets than a full magazine and has ammo left to spare, add bullets to the pistol while subtracting each bullet added from the ammo. Wait 1.5 seconds and stop the player's reloading. If you want them to have no reload or reload slower/faster, remove this or tweak the seconds.


  • Order by
Want to leave a comment? Login or Register an account!