Shooting a weapon after it's equipped

I’ve made a blueprint script on my player character that can equip and dequip a weapon from anywhere in the world, but I’ve stumped myself on how to shoot the weapon when it’s equipped. I want each weapon that I create to have different parameters (fire-rate, magazine size, reload speed, etc) so that I don’t have to script every single weapons parameters onto my player character and then test to see which parameters I’ll use depending on what weapon is equipped. Thanks in advance!

I’m trying to fire my gun from the gun BP upon clicking Left Mouse when it’s equipped, and I want to be able to do this for any gun I equip.

This is the code inside of my gun (P90) that I want to be able to use after the gun is equipped on my character.

This is the code for equipping and dropping a weapon, just in case you need to see it.
(located in the player character)

Hello,

Instead of using a key input in your gun, create a custom event (lets call it ‘Fire Gun’) that triggers the fire code. You’ll want to make sure the basic logic for firing a gun is in parent class for all of your gun child classes (P90, AK47, etc…)

In your Character BP, after you equip the gun, set it as a variable (lets call that one ‘Gun’). When you unequip it, set the ‘Gun’ variable to nothing.

Add the Left Mouse Button Key Input into your character and get a reference to the Gun reference variable. From the Gun reference, pull out the ‘Fire’ Custom Event Call and put it after the Left Mouse Button input event. Between the Input Event and Fire, add a ‘Is Valid Node’. That will make sure it only fires if you have a gun equipped.

Let me know if this makes sense!

I agree with the above suggestion. You want to look into parent/child relationships. The parent should hold variables for all the gun specs you want to change (magazine capacity, fire rate, bullet type, cool down period, recoil etc). Then you create child blueprints off the parent and within each child you define the exact specs for each gun type. So AK47 will have 30 round magazine and a fire rate of 500 rounds/sec, but a barrett 50-cal will have a 10 round magazine and a fire rate of 1 round per second. A firing function within the parent BP will simply grab the variables necessary to determine fire rate and magazine capacity from the specific instance of the child weapon you have equipped. If you need to see an example of how to setup a basic parent/child relationship check out video #15 and 16 in the link below.

Thanks for the response! I created a “Gun” variable that is an object reference, but I can’t seem to get my “Fire Gun” event from the weapon class. I haven’t made a parent class that I can mold all of my guns off of, could that be the issue?

I took a closer look at your equip code and it looks like you’ll need to cast to your Gun BP before setting a reference to a Gun BP variable. Using just an actor reference won’t work because an Actor doesn’t have the ‘Fire Gun’ event in it.

Does that mean if I cast to the parent Gun BP that it will work for any child class of the Gun BP?

Yep, assuming that is where the Fire Code is. That is the beautiful thing about class inheritance.

I think the Child Blueprints Documentation does a pretty good job of explaining it

I put in a cast to my Weapon BP (parent class) and I tried to get the “Fire Gun” event from it but it would only show if I disabled “Context Sensitive” in the search menu. I also don’t know how to set the “Gun” variable to the weapon picked up and my code has no way of telling which child class the weapon picked up is. Also how do I set references? I’ve typed in the search bar for them but nothing comes up.
Here’s the code snippet I have at the end of my equip code:

You want an OBJECT reference cast to the parent class to provide you with the specific instance of the object which you can then call functions/events on. You don’t want a CLASS reference cast to the parent.

That makes a ton more sense. I did that and successfully grabbed the “Fire Gun” event but I still have no idea what to plug into the “Gun” variable to set which weapon is equipped. Is there some way to get the child weapon class from the “Break Hit Result” tab to plug it into the “Gun” variable set code?
Here’s my updated code if it helps:

Honestly, I can’t even see what is really going on in your original screen shots because they aren’t image links so zooming in just blurs things. But basically the “cast to weapon” needs to be a specific instance of the weapon object that you will then call the “fire gun” event on. If you check out video #1 and #25 in the link I provided above I go over casting and how to create references (the thing you need for the object pin of a cast node). If you use an overlap event to pick up the weapon, the overlapped actor would suffice for the object pin. If not, perhaps the references video will give you some idea of how to get the necessary reference for the cast node. You don’t need a “gun” variable.

Your “hit actor” from the line trace if that is how you pick up your gun then yes, you can use that and plug that blue wire into the cast node to the weapon. You can also store the hit actor as a variable if you like and then use the variable to plug into the cast node. It is the same thing just makes it easier to do other things with the hit actor if you need to later on.

I took a closer look at my code and I realized I had the “Fire Gun” event dispatcher hooked up to where it would run when I press F, which is the button to equip a weapon. I did what you said and hooked up the “Hit Actor” pin to the object pin and it worked perfectly! Thank you both for helping me out, I seriously appreciate it. Also your videos on Casting and Parent/Child functions are extremely helpful!
Also thanks for putting up with my ignorance on UE4 BP’s

No problem man, glad you got it working.