# Weapon switch

Discussion and feedback on Construct 2

### » Tue Sep 06, 2011 8:05 am

Hi, I have what I see as a complex problem...
I have a few weapons for the player in the game. I want the player to be able to hold 2 weapons at a time and switch between them when pressing a key. When the player goes to a shop and buys a weapon the weapon should replace the current 'active' weapon.

I am not asking you to do the whole event sheet, but some examples or perhaps directions on where to begin would be appreciated.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,455

### » Tue Sep 06, 2011 6:28 pm

i'm sure someone will 'help' you. but i would like to suggest that what i do when i start working on game logic, is make bullet statements, or short sentences. if you can break down what you wan to do into short statements, it will almost literally tell you how to build/write the events for it.

i only mention it because, as i was reading your thread, i could see the events in each sentence you wrote.

good luck...
B
61
S
20
G
15
Posts: 1,042
Reputation: 26,984

### » Tue Sep 06, 2011 6:52 pm

i did something similar, but i'm not sure if my solution is perfect.

i looked at the weapons as numbers.
1 = first weapon
2 = second ......
3 = third .....

if the next weapon is selected a variable called "current" would increment by one. selecting the previous weapon would decrement "current".

if the player would press the key to use the weapon it would check the value of "current" and if it is 2 the shot of the second weapon would be fired and so on.

i hope this helps you a bit.
B
11
S
3
G
2
Posts: 15
Reputation: 1,569

### » Tue Sep 06, 2011 10:00 pm

Thanks, guys, epecially Pete... I know where to start now.
So i'm thinking this: each weapon has a 'value'.
So if current_weapon = 1 then delete the current weapon and place Weapon 1.
I think i will need to store another variable second_weapon. When the key is pressed to switch, the values of these variables swap.

To destroy the weapons I have a condition system global variable if current_weapon does not equal (1, for example) then for turret1 play animation put away then destroy the instance.

To create the instance i would want it to wait until the animation 'put away' of the gun is finished before the 'take out' starts. I am not sure how to handle this. Any ideas?
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,455

### » Tue Sep 06, 2011 10:13 pm

You could also do all weapons as a animations in one sprite. Then instead of deleting instances and creating new ones you can just switch animations :)
B
140
S
58
G
37
Posts: 2,549
Reputation: 31,711

### » Tue Sep 06, 2011 10:26 pm

Hmm, even if the weapons are significantly different from each other in terms of what they shoot?
Interesting, but with supplementary animations for going to and from each weapon, I think this would add more complexity than simplicity.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,455

### » Tue Sep 06, 2011 10:29 pm

[QUOTE=sqiddster] Hmm, even if the weapons are significantly different from each other in terms of what they shoot?[/QUOTE]
Sure - you'd just have subevents, like this (indented means subevent):

+ On shoot
----+ Current weapon is pistol
----> Shoot bullet
----> Current weapon is rocket launcher
----> Shoot rocket

etc.

Having separate objects for player and their weapon sounds like a good way to do it - otherwise you have to redraw your player's animations (walking, running, jumping etc.) once for each weapon.
Scirra Founder
B
362
S
216
G
75
Posts: 23,114
Reputation: 180,470

### » Tue Sep 06, 2011 10:31 pm

How weapon shoot is usually defined by variables.
+If var "weapon" = 1
- set animation to "weapon1"
- set "fire_speed" = 5
etc.

and for switching
If animation "put away" finished -> set animation to "weapon2"

just an example :)
B
140
S
58
G
37
Posts: 2,549
Reputation: 31,711

### » Tue Sep 06, 2011 10:53 pm

This looks very interesting... I think I will give your method a try!

By the way, does anyone know a simple and efficient way using as less events as possible to establish a fire rate for a weapon?
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,455

### » Wed Sep 07, 2011 9:38 am

OK I have it all working and followed your advice, EXCEPT for the put-away and take-out animations for each weapon. I am a bit lost as to what to do for these. I have these animations as animations within the main weapon object but they are not used by the program as of yet.

Any advice as to how I can implement this feature?

Much thanks for all your help :)
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,455

Next