0 Favourites

Weapon Reload (take from one variable and give to another)

  • How do I take from one variable and give to another. Forgive me this is probably the stupidest question in the world, but I have just started a project, it is a top down shooter and I want to have reloading weapons so how to I take 15 bullets from my ammo variables and put them in my gun magazine variable and if there was only 3 bullets in my ammo only move them instead of going into the negative?

  • magazine=ammo

    Every time you update the magazine value after ammo is picked up simply set value of magazine to the variable value of the ammo variable. Have another action that sets the value of ammo to 0 if its less than 0.

  • if I set the value of the magazine to the value of ammo won't that just keep setting the magazine to full every time I fire I am sorry you lost me I don't suppose you could create an example.

  • My bad, I was simply explaining how to transfer the value of a variable to another and stop it from going negative. I failed to read whole request.

    If someone hasn't done so, when I get off work I will throw something together.

  • You will use the min() expression. This will give you the smallest number out of the numbers you provide.

    So when you reload, you will subtract min(ammovariable,15) from ammovariable. If ammovariable is more than 15, it will subtract 15, if it is less, it will subtract ammovariable, leaving you with 0.

  • I threw together a .capx https://drive.google.com/open?id=0Bx-ufu2WID53c2ZiZzlmNXFETlk (click download arrow in upper right corner of screen).

    It allows you to reload from an ammo variable until its empty and continues to fire until the mag is empty as well.

    Its pretty self explanatory. I am sure it can be condensed quite a bit by someone much better with math.

    That said/done.. there is a reason that most games simply have an ammo pick up that refills your weapon completely then destroys itself and doesn't track how many rounds are in magazine, pick up, or have been fired.. without that logic the above capx would have been just a couple of lines.

  • worked perfectly, I just copy and pasted it into my game thank you so much

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Glad to hear.

    It could easily be scaled up to include more magazine sizes as well.

    Your also not limited to using globals (unless mag/remaining/ammo states carried to other layouts), you could put ammo as a player instance variable and then capacity/magazine/remaining as instance variables of a weapon sprite.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
132 10,993
Burvey's avatar
Burvey
Unread hot topic
69 6,661
PrinceofMars's avatar
PrinceofMars
Unread hot topic
62 7,007
megatronx's avatar
megatronx