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

0 favourites
  • 9 posts
From the Asset Store
Players follower. use it for anything which follows the player
  • 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?

  • 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.

  • 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...

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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..

  • 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.

    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?

    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?

  • 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.

  • 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

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)