# How do I change weapons with a button.

Get help using Construct 2

### » Wed Jan 07, 2015 2:04 pm

Hello! I'm working on a shooter and got stuck with something simple, I hope you can help me.
--
The character have 2 weapons, one called buster and the other a homing attack.
I created 2 global variables (Buster level (1-3), and Homing level (1-3)).
So if the buster is set to 1 and above and homing to 0 it will shoot the buster. And if the homing is set to 1 and above and the buster to 0 it will shoot the homing.
When I press A to change the values it works on the first time, but not on the second time.
For example:
1)Press A;
2) If buster is set to 1, change it to 0 and change homing to 1. <<--- Ok, it works;
3) If homing is set to 1, change it to 0 and change buster to 0. <<--- The same thing, but don't work.
--
The strange thing is that if i change the order of the events, only the first one works. I'm getting frustrated here, trying to solve this thing.
Can you guys help me out?
B
4
Posts: 10
Reputation: 214

### » Wed Jan 07, 2015 2:37 pm

If you have only two choices then having two variables isn't necessary - just have a Weapon = 1 and do something like Weapon = abs(Weapon -1) on switch. This produces 0 1 0 1 0 1, etc on every activation. Or, simpler in a way - Have it as 1 and do Weapon * -1 on switch. Then compare <0 - one weapon >0 another weapon.
B
19
S
6
G
7
Posts: 1,101
Reputation: 6,146

### » Wed Jan 07, 2015 3:20 pm

I'd list all weapons in an array (weaponList), together with their current power (I'm guessing 1-3 is the power for each weapon, right?) so I could add as many weapons as I needed later. Also, I'd use a variable (selectedWeapon) to store which weapon is currently selected.

So I could use the following code to switch:

On <key> pressed >> Set selectedWeapon to (selectedWeapon + 1) % weaponList.Width

When shooting, I'd just check selectedWeapon and the power of that weapon in weaponsList, and create the blasts accordingly.
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 722
Reputation: 4,026

### » Wed Jan 07, 2015 3:28 pm

@brunopalermo - very elegant way of doing a size check with the modulus, I like it. Gotta write it down to remember.
B
19
S
6
G
7
Posts: 1,101
Reputation: 6,146

### » Wed Jan 07, 2015 10:52 pm

I don't have time right now, but tomorrow i'll check it out.
Does it fix the problem where you can't change the variable twice using the same button?
B
4
Posts: 10
Reputation: 214

### » Fri Jan 09, 2015 1:24 pm

I had a little trouble to understand the arrays, but i got it to work! Thanks guys!
B
4
Posts: 10
Reputation: 214

### » Wed Dec 09, 2015 10:38 pm

So, with a simple array, let's say we tried to recreate Jazz Jackrabbit's weapons: http://budwin.net/insectoid/dos/jazz/jazzweapons.htm

Width of 5 for the weapons (fast fire is not a weapon)
and in height we can have 1 cell for the current ammo (all begin 0 besides blaster with, say, 10)
another cell for hits required to kill enemy
another for the max amount of rounds before you can't collect any more ammo per weapon
and I guess a third to act like a boolean for fast-fire on or off (not sure if this could be done just via events though)

Now, would this be the best way of setting up an array for this type of simple weapons system? If so, could you get away with adding and subtracting/retrieiving figures from the array without touching the expression editor?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Thu Dec 10, 2015 6:25 am

brunopalermo wrote:I'd list all weapons in an array (weaponList), together with their current power (I'm guessing 1-3 is the power for each weapon, right?) so I could add as many weapons as I needed later. Also, I'd use a variable (selectedWeapon) to store which weapon is currently selected.

So I could use the following code to switch:

On <key> pressed >> Set selectedWeapon to (selectedWeapon + 1) % weaponList.Width

When shooting, I'd just check selectedWeapon and the power of that weapon in weaponsList, and create the blasts accordingly.

Do you have an example of this kind of idea that I could compare my own to? I've never used arrays and I think mine looks okay but I'm having a bit of trouble keeping my head together with it
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286