Creating a Weapons template
My level of blueprint skills is pretty novice. I'm currently trying to learn to make an FPS game, what I wanted to do is create a Blueprint that acts as a template for creating weapons late on in the project, instead of creating a weapon 1 by 1 and then redo all the variables, connecting the nodes etc... I wanted to make 1 template with all the necessary variables (Ammo, Maximum ammo, Damage, Critical Damage, etc...) and how the weapons fire (Left click to shoot primary, middle mouse button for secondary) and then create Children Blueprints based off the template.
I wanted to know the best way of doing this. Because I thought that creating an Actor Component would work but I cannot add an Event for mouse button presses. So I'm not entirely sure which Blueprint Class to pick and then configure.
asked Aug 04 '17 at 08:33 PM in Blueprint Scripting
Sorry for the late response. In the editor, click the green "Add New" button. In the list, select "Blueprint Class". Now name this whatever you'd like (I usually do WeaponMaster so I can do a quick search for Master and find the Master of any class I want be it weapon, grenade, etc...). Now repeat clicking the green "Add New" button and select "Blueprint Class" again, only this time search for whatever you named the Weapon Template; this will be your first actual weapon (we will call it "FirstGunBP"). The Master or Template is only used for creating and setting variables, and creating events that spread across each weapon. Once you select and create it, open both. From here, if you want to put an event, say Mouse Click, you can put the event, even custom events, in the Template/Master and attach some code to it. Now, you can return to your FirstGunBP and add that same event. Then, right click on the event node and click on "Add Call to Parent Function". After that, you can connect any nodes to customize each weapon in each weapons BP. As for variables, any variable that you create in the Template/Master BP Class will show once compiled in the child actors, so any weapons you have made by using this method.
To cast to these weapons you will need a reference to the weapon itself, or you will only be able to get what you have the stats set as in the weapon. If you have a reference, you can read information only to that weapon such as upgraded damage, without the reference, it will only show what you programmed that weapon to start as. When you cast, as long as you have the reference, you can manipulate any stats you want. Also, it is a good idea to have a Function for Shooting one shot off. That way you don't have to reprogram shooting for each weapon, only how it shoots. (Ex) If you have a pistol and an assault rifle, you can choose to only call Fire Shot event on Mouse Down, or you can call it until the mouse is released, once or many times.
As far as Mouse Down events and control events, it is a good idea to have an event in your player characters BP that casts to your equipped weapon and fires its own event within the weapon, and it is also a good idea to have the shooting key in your inputs in the Project Settings. This allows you to run any checks your player may need to run to see if he can shoot such as Allowed to Shoot and Is Stunned, and your weapon can run its own checks like Ammo in Clip and Is Jammed and keeps things organized. Sorry for the long response, but I remember how frustrating this was for me to learn at first as well. Let me know if you need any explanations or anything more.
answered Apr 03 '18 at 11:57 PM
Thanks for your response! It's alright if it's late. That's one way to do it!
The way I've done it was created a data table (includes damage, weapon type, range, skeletal mesh, etc...) in which the blueprint template (I think I just called it GunMaster_BP) will grab it and then attached it to the player's pawn. It's easy since I can just add a new row in the data table if ever I want to make a new weapon.
I've marked your reply as an answer since it might help others if ever they have the same question!
answered Apr 04 '18 at 12:03 AM
There's literally tons of youtube tutorials on this but to answer your question, yes, you'll likely want to have an Actor Component as your parent class with all of your variables. Be sure to add a skeletal mesh component so you can swap that out in each child class with different weapons. If you want mouse button presses to be directly handled within the weapon class, you'll need to enable that blueprint to listen for input (by default its only the character). However, the usual thing to do is delegate; the first person template will show you from the character level, but a more pure way would be from the controller level. Listen for clicks there, and call a fire function on the parent. As long as your other weapons inherit from that one, and don't override the fire function, it's as simple as that.
answered Aug 04 '17 at 10:37 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here