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

Get help using Construct 2

Post » 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
14
S
4
Posts: 125
Reputation: 1,390

Post » 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
69
S
18
G
8
Posts: 639
Reputation: 9,549

Post » 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
14
S
4
Posts: 125
Reputation: 1,390

Post » 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
69
S
18
G
8
Posts: 639
Reputation: 9,549

Post » 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.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Sat Oct 01, 2016 2:02 am

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. :)
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
69
S
18
G
8
Posts: 639
Reputation: 9,549

Post » Sat Oct 01, 2016 8:41 am

worked perfectly, I just copy and pasted it into my game thank you so much
B
14
S
4
Posts: 125
Reputation: 1,390

Post » Sat Oct 01, 2016 2:29 pm

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.
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
69
S
18
G
8
Posts: 639
Reputation: 9,549


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 47 guests