How do you create ammo?

how do you create ammo using Hamzah’s 2Dshooter, and also reloads.
and is it alright if you send a wick file for it ?

(Hi, im back for those wondering!)

Welcome back @FleschkJ! Could you give a link to the engine?

1 Like

the engine?
what engine? or are you talking about the 2Dshooter engine, and if so here

Please, I need someone to help

i have no idea of what that forum-made mess wanted to say. But try making your own shooter, with some basic code, like if this.isRight == true then shoot that side, else shoot the other side
If you’re wondering, you could implement that by adding and subtracting (depends on the side).

Glad to see my tutorial is still being used today.

First off, I’m guessing you finished the 2D shooter tutorial and want to make an ammo system, where you keep track of how much ammo the player has and reload in between.

To do that, you’d need to first create a few variables for ammo and reloading inside of the player’s default script.

// ammo
this.magazineSize = 32; // how much ammo you reload up to
this.ammo = this.magazineSize; // how much ammo you currently have
// reloading
this.reloadTime = 10; // takes 10 seconds to reload
this.reloadingSec = 0; // reloading progress, starts at 0 seconds

Next up, we want to make sure that the player has enough ammo before shooting, and to decrease our ammo count after shooting. To do this we’ll need to check if the player has more than zero ammo before allowing it to shoot, then decrease this.ammo by 1.

To do that, in the player’s update script, look for these lines:

And replace them with

if(isKeyJustPressed('space')&&this.ammo>0){ // If "space" key is pressed AND we have more than zero ammo
    shot1.clone(); // Clone the bullet
    this.ammo--; // subtract one bullet from our ammo
}

And now we want to reload our ammo as well—
In our case, we’ll allow the player to reload when they press r, or automatically reload when the player has zero ammo. We’ll start with the latter.

To do that, inside of the player’s update script, check to see if this.ammo is 0. This would mean that we’ve ran out of ammo.

if(this.ammo === 0 ){ // see if we have no ammo
    // RELOAD AMMO HERE
}

Now, you might be wondering how exactly we plan on reloading the ammo? Remember the two reloading variables we made in the beginning— this.reloadTime and this.reloadingSec?
The this.reloadTime will define how much time it takes to reload, meanwhile the this.reloadingSec will define how much seconds we spend reloading. While reloading, we’ll need to increase the this.reloadingSec variable until it’s equal to our reloading time, this.reloadTime

if(this.ammo === 0 ){ // see if we have no ammo
    this.reloadingSec += 1/project.framerate; // increase reloadingSec
}
note

If you’re curious why increasing this.reloadingSec by 1/project.framerate works, it’s because… well, it’s a bit complicated. To put it simply, think about what framerate, FPS, stands for. It’s frames per second. Meaning, say, if we have 12 FPS, then our code will run 12 times per second. So, we increase this.reloadingSec by 1/12 and we do that 12 times per second, when you add up 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 + 1/12 (twelve times) you’ll get 12/12, or 1— meaning it’ll add up to 1 every 1 second. There are slight imperfections since we might not always run at 12 FPS, but we don’t need to be super accurate.

Next up, you want to check when this.reloadingSec is equal to this.reloadTime, and when that’s true then we can reload and reset the timer.

if(this.ammo === 0 ){
    this.reloadingSec += 1/project.framerate;
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize; // refill ammo 
    this.reloadingSec = 0; // reset reloading timer
}

Now we also want to allow the player to reload when they press r. Keep in mind that you probably don’t want to allow the player to shoot while reloading, otherwise they can spam shooting while reloading and never run out of ammo. So, when you press r, you can simply set the ammo to zero, which would not only stop the player from shooting, but also automatically reload the gun, hitting two birds with one stone.

if(isKeyJustPressed("r")){ // if "r" is pressed
    this.ammo = 0; // reset ammo
}
if(this.ammo === 0 ){
    this.reloadingSec += 1/project.framerate;
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize;
    this.reloadingSec = 0;
}

Now this is a bit of a bonus but I’m also guessing that you want a text that shows how much ammo the player has. This should be easy— select the text object and create the text anywhere you want on the screen, then name the text object something like ammoText.

Then go back to the player’s update script we were working on, and let’s display the number of ammo we have in the ammoText. It’s important that you do this on top of the code we previously added.

// set ammoText to how much ammo we have
ammoText.setText(this.ammo + " / " + this.magazineSize);

if(isKeyJustPressed("r")){
    this.ammo = 0;
}
if(this.ammo === 0 ){
    this.reloadingSec += 1/project.framerate;
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize; // refill ammo 
    this.reloadingSec = 0; // reset reloading timer
}

And you might also want to set the reloading time when we’re reloading. Under the this.ammo === 0 if statement, we’ll display how much time there’s left for reloading by subtracting this.reloadingSec from this.reloadTime.

// set ammoText to how much ammo we have
ammoText.setText(this.ammo + " / " + this.magazineSize);

if(isKeyJustPressed("r")){ 
    this.ammo = 0;
}
if(this.ammo === 0 ){
    this.reloadingSec += 1/project.framerate; 

    // show how much reloading time is left
    ammoText.setText(this.reloadTime - this.reloadingSec);
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize; 
    this.reloadingSec = 0; 
}

Also, a bonus— the player might be too focused on the game than to notice when they’re done reloading or such. For this reason, it might be a good idea to color the ammoText red or something while reloading, and reset it back to black when you have ammo. For this one, we’ll work under the same if(this.ammo === 0) statement, setting the color to red when ammo is 0, else setting it back to black.

ammoText.setText(this.ammo + " / " + this.magazineSize);

if(isKeyJustPressed("r")){
    this.ammo = 0;
}
if(this.ammo === 0 ){
    this.reloadingSec += 1/project.framerate;

    // show how much reloading time is left
    ammoText.setText(this.reloadTime - this.reloadingSec);
    ammoText.fillColor = "red"; // color the text red
}else{ // if we DONT have zero ammo
    ammoText.fillColor = "black"; // color the text black
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize; 
    this.reloadingSec = 0;
}

Final code with comments:

Player Default Script
// ammo
this.magazineSize = 32; // how much ammo you reload up to
this.ammo = this.magazineSize; // how much ammo you currently have
// reloading
this.reloadTime = 10; // takes 10 seconds to reload
this.reloadingSec = 0; // reloading progress, starts at 0 seconds
Player Update Script
// set ammoText to how much ammo we have
ammoText.setText(this.ammo + " / " + this.magazineSize);

if(isKeyJustPressed("r")){ // if "r" is pressed
    this.ammo = 0; // reset ammo
}
if(this.ammo === 0 ){ // see if we have no ammo
    this.reloadingSec += 1/project.framerate; // increase reloadingSec

    // show how much reloading time is left
    ammoText.setText(this.reloadTime - this.reloadingSec);
    ammoText.fillColor = "red"; // color the text red
}else{ // if we DONT have zero ammo
    ammoText.fillColor = "black"; // color the text black
}
if(this.reloadingSec >=this.reloadTime){
    this.ammo = this.magazineSize; // refill ammo 
    this.reloadingSec = 0; // reset reloading timer
}
Other

Don’t forget to create a text object called ammoText. You can call it something else, though if you do then you’ll need to change the name inside of the code as well.

Also, you can always decrease the player’s reloadTime or increase the magazineSize as a powerup or such.

I hope this helps.
I just wrote most of this without testing anything, but it should all work out. If it doesn’t or if you have any questions or such, please lemme know.

1 Like

Thank you so much!

1 Like

I’m not sure if you will see this, but can you help me with my musket game?
just the gunplay, the Controls are, F to present, and then LMB to shoot

Example(Musket)2-19-2025_15-58-12.wick (1.1 MB)

edit: I think that you are a way better coder than me, insanely better, so I had to give you the wick file because I’d thought you would help on it

Yeah ofc, I don’t mind helping out.

In your example, you have a gun animated for shooting and what not, so rather than using the reloadingSec and reloadTime variables I created in my example, I figured it’d be better if you’re to base it off of the current frame number of the gun.

Example(Musket)2-20-2025_8-37-44.wick (1.1 MB)

Most of the code for things is inside of the player, rather than the gun.

Also fixed an issue where the shot changed directions by setting it to the scaleX of the player in the default script (when the clone is spawned), and set a timer inside of the bullet to make it disappear after 10 seconds (see default and update script of shot). It’s similar to the concept of the reloadingSec I explained earlier, once you see the code you should see the resemblance.

I saw that you had the f key for aimming the gun or lifting it back up (as you’ve explained). I decided to make the shooting button, space do the same thing when someone’s trying to shoot while the gun is on the musketeers back. You can change the space key with anything else through the keypressed script of the player.