How to reload 1 gun at a time?

I want to learn how to reload my Ar with 35 rounds and reload my shotgun with 2 but i dont know how to separate the two, it would reload my shotgun with 35

and i would also love to learn how to make an ammo counter! for my AR and Shotgun

@Thebosser_24 You could add a attribute to your gun script to manage this or create a game manager script for handling and displaying the count.


i am more worried about the guns reloading the right ammo right now
but i will work on that

@Thebosser_24 OK well two attributes added to your gun script would be a good starting point. Maybe something like ammoCount and reloadCount. The ammoCount would be the actual counter which could start off as a full magazine. You could decrement this every time you fire. Once at zero you could fire a message to the user that reload is required and not allow the fire mechanism in your shoot script. When reload is triggered you would just copy reloadCount over to ammoCount. Does this make sense?

1 Like

i have this…

Movement.prototype.onMouseDown = function(event) {
    if ('sniperrifle').enabled === true) {
       // shooting code 
        if (event.button === pc.MOUSEBUTTON_LEFT) {
            // shoot
            if (this.rounds !== 0) {
                var bullet ='bullet').clone();
                bullet.enabled = true;
                // update magazine
                this.rounds = this.rounds - 1;
// Function end

@Thebosser_24 Without seeing the rest of the code it looks good. Keep going. Now look for the R key to be pressed to reload. You can move the reload value to this.rounds. You look like you have the concept correct. I would make a gun script that can be used by each gun you have. That way the number of rounds and reload are related to each and not so global.

so make like a few bullet scripts?
and the code is in the universal “eveything” script
and this is my AR bullet script

var Bullet = pc.createScript('ARBullet');
// initialize code called once per entity
Bullet.prototype.initialize = function() {
   this.entity.collision.on('collisionstart', this.onCollisionStart, this);
   this.entity.collision.on('collisionend', this.onCollisionEnd, this);
// update code called every frame
Bullet.prototype.update = function(dt) {
   if (this.entity.enabled === true) {
       }.bind(this), 3000);
Bullet.prototype.onCollisionStart = function (result) {
   if (result.other) {

@Thebosser_24 A separate script that handles all aspects of the gun. You can have a quick look at what I did here.

Overview | Dashboard | WeaponTest | PlayCanvas | 3D HTML5 & WebGL Game Engine

Each weapon uses the same script but in their attributes I am changing specifics of each weapon.

here is my reload script if this is what you want

    if (app.keyboard.isPressed(pc.KEY_R)) {
        this.rounds = 35;
.bind(this), 3000);

heres a link PlayCanvas | HTML5 Game Engine

@Thebosser_24 I took a look at your link above. There are several things that I am having difficulties with trying to figure out. First I am getting a warning that you are over your used disk allowance. I see a lot of zip and .rar files in your folders. You should delete these as they will not do you any good being inside. app is not defined error comes up when trying to launch. I think this issue is in your Movement.js line 105 which I think the entire function is missing a curly brace. I can’t really get anything to move so I would suggest a little cleanup before we proceed. It will be much easier for someone to support you once you get things in order.


im working on all of that, i got another topic about the 105
so can we pause this topic until i get it done?
all of these errors have arose last night, and I’ve been trying to fix them

ok, my script is fixed and now my stuff is cleaned up, should be better

Working on my new game
can i change this script to reload a
Pistol (12 Rounds)
AssaultRifle (40 Rounds)
SniperRifle (8 Rounds)
can you give me an example of 1?

  setTimeout(function() {
    // Reload
    if (app.keyboard.isPressed(pc.KEY_R)) {
        this.rounds = 6;
}.bind(this), 3000);
    // reload gun magazine

Where is the timeout for?

@Thebosser_24 Hi… So do you have a new link to your game so we can have a look to the context of where this script will be used? Like @Albertos stated, Not sure what the timeout would be used for. I think it would probably attach a script directly to the gun itself which will include in it’s attributes something like reloadSize and a currentRounds variable which can keep track of rounds fired.

1 Like

i dont know what the timeout is for, someone gave me the script and that was in there

@Thebosser_24 Hey I took a quick look at your latest. These lines are outside the function. You have had this issue before. Could you move them inside your initialize?

    if ('Pistol')){
    this.rounds = 12;
}    if ('AssaultRifle')){
    this.rounds = 40;
}    if ('SniperRifle')){
    this.rounds = 8;
1 Like

its in, i need to start paying more attention to that :sweat_smile:

@Thebosser_24 Hey nice! I see your first person is working and now you have a very good base to start with.

1 Like