How do I get a reference to my player's weapon?

Get help using Construct 2

Post » Sun Jul 19, 2015 9:36 pm

I have my player, and he has a weapon. When the user clicks, I want to tell the weapon to fire. Each weapon needs to have its own implementation of "Fire" as some of them shoot rockets, others bullets, others lasers, etc.

The "Function" object is not really a function object, it's an event broadcaster/listener object, so I can't just call function "Fire" or all weapons in the layout listening for "Fire" will fire.

How can I get a reference to the player's current weapon, and tell that specific instance to run its "Fire" implementation?
B
12
S
4
Posts: 48
Reputation: 910

Post » Sun Jul 19, 2015 9:53 pm

If I'm understanding the situation correctly you can simply try using a variable or something to check which weapon is currently equipped, so if shoot+variable=1 -> shoot weapon type 1; if shoot+variable=2 -> shoot weapon type 2, etc.
B
53
S
24
G
18
Posts: 295
Reputation: 13,115

Post » Mon Jul 20, 2015 7:13 pm

Sup with that? wrote:If I'm understanding the situation correctly you can simply try using a variable or something to check which weapon is currently equipped, so if shoot+variable=1 -> shoot weapon type 1; if shoot+variable=2 -> shoot weapon type 2, etc.


The idea of a generic "Fire" function call is to avoid having to write a conditional for every single weapon type in the game, and keep track of which number or string equals which weapon type. The weapon should handle any logic associated with it, and the player just needs to know that a weapon has a "Fire" function.

It's very bad practice in programming to do what you're suggesting, but I don't see any other option in C2...


Ideally if I'm forced to manage strings like this, the "OnFunction" action should have a dropdown selection of all declared function names, rather than having to manually type in the string again. Or just more variable types, or even an Enum object...
B
12
S
4
Posts: 48
Reputation: 910

Post » Mon Jul 20, 2015 7:54 pm

Well, perhaps this is just going way over my head but it seems to me like you'd need to run a weapon check at one point or another in the event sheet to know what projectiles to shoot.

I suppose you can make the shoot sequence something more generic like "On shoot -> set boolean to true" where the boolean is a family variable found in all weapons (in the same family), then code every each weapon to respond differently when their boolean is toggled, if that's what you're after.
B
53
S
24
G
18
Posts: 295
Reputation: 13,115

Post » Mon Jul 20, 2015 9:19 pm

So if you want each weapon to have different actions upon firing, why would you need to call a generic fire function?

You could ofcourse call a fire function and add the current weapon as a parameter and add the specific actions for that weapon in the function by comparing the parameter..
I'm not really sure what the question is..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jul 21, 2015 8:55 pm

The point of having a generic fire function is that each weapon could have an event sheet where it has it's own fire function, which says which projectiles to fire for itself. The player just needs to call CurrentWeapon.Fire() and the weapon handles the rest. No checks necessary.

Sup with that? wrote:Well, perhaps this is just going way over my head but it seems to me like you'd need to run a weapon check at one point or another in the event sheet to know what projectiles to shoot.

I suppose you can make the shoot sequence something more generic like "On shoot -> set boolean to true" where the boolean is a family variable found in all weapons (in the same family), then code every each weapon to respond differently when their boolean is toggled, if that's what you're after.


That might be able to work. How would you suggest I keep a reference to the current Weapon family member equipped in order to set the boolean?

LittleStain wrote:So if you want each weapon to have different actions upon firing, why would you need to call a generic fire function?

You could ofcourse call a fire function and add the current weapon as a parameter and add the specific actions for that weapon in the function by comparing the parameter..
I'm not really sure what the question is..


For that solution, what would I be passing? A string with the name of the weapon? Some number that represents the weapon?
B
12
S
4
Posts: 48
Reputation: 910

Post » Tue Jul 21, 2015 10:05 pm

I once did something similar, though it wasn't for weapons but could be adapted easily enough. In my case I checked if the object was on screen when the family variable signaled action, so it would only respond if it was equipped, hence in view. Not foolproof but I imagine there's lots of other conditions I could've substituted for something more elegant.
B
53
S
24
G
18
Posts: 295
Reputation: 13,115

Post » Wed Jul 22, 2015 2:57 pm

Sup with that? wrote:I once did something similar, though it wasn't for weapons but could be adapted easily enough. In my case I checked if the object was on screen when the family variable signaled action, so it would only respond if it was equipped, hence in view. Not foolproof but I imagine there's lots of other conditions I could've substituted for something more elegant.


That could possibly work, but my character has two separate hands for dual wielding, so I'm not exactly sure how I would handle that.

However I will try to implement this and I'll post my setup if it works or if I find anything more elegant.

If anyone has further suggestions I'm all ears.

Thanks
B
12
S
4
Posts: 48
Reputation: 910

Post » Thu Jul 23, 2015 2:15 am

It should technically work for multiple weapons. Basically what I did was have the action variable double as cooldown, so that if action button was pressed -> subtract from cooldown. When cooldown = 0 + object on screen -> action + reset cooldown. Since each object can reset cooldown to a different value, they could both be equipped and still function differently i.e. have different rates of fire and such.
B
53
S
24
G
18
Posts: 295
Reputation: 13,115


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests