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

Get help using Construct 2

### » Fri Sep 30, 2016 3:07 pm

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?
B
17
S
4
Posts: 135
Reputation: 1,498

### » Fri Sep 30, 2016 3:11 pm

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.
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
79
S
22
G
8
Posts: 649
Reputation: 10,253

### » Fri Sep 30, 2016 4:38 pm

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.
B
17
S
4
Posts: 135
Reputation: 1,498

### » Fri Sep 30, 2016 5:40 pm

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.
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
79
S
22
G
8
Posts: 649
Reputation: 10,253

### » Fri Sep 30, 2016 5:51 pm

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.
B
59
S
29
G
122
Posts: 1,828
Reputation: 69,058

### » Sat Oct 01, 2016 2:02 am

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.
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
79
S
22
G
8
Posts: 649
Reputation: 10,253

### » Sat Oct 01, 2016 8:41 am

worked perfectly, I just copy and pasted it into my game thank you so much
B
17
S
4
Posts: 135
Reputation: 1,498

### » Sat Oct 01, 2016 2:29 pm

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.
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
79
S
22
G
8
Posts: 649
Reputation: 10,253