Help needed with Weapon Swapping using BP

To start things off I am working on a TPS/FPS and would like to implement weapon swapping. Right now I have an Actor Weapon Master BP with Child Actors which are the actual weapons. Now I have gotten this far by watching a TPS tutorial by Kyle Dail on YT. It feels like I have everything I just can’t figure out how to pick up where he left off. I had stumbled across this thread ( Weapon Swapping? - Platform & Builds - Epic Developer Community Forums ) and the comment by BubbaBonkers seems like it is exactly what I need to do but I already have an Equipped Weapon Var that is calling on the Weapon Master BP to fire the equipped weapon. So, should I make a different var and make it an array for what Bubba explained or change my current Equipped Weapon Var to an array and follow from there? If you need any pictures just let me know and I will be happy to share.

Yeah, it seems like the best way to handle this would be to have an array to contain a list of the weapons the player has access to. You can have it contain all of the weapons by default or only have weapons the player obtains … but that’s a different issue. This array can either contain actor references, meaning your weapons will need to be spawned in the world already and hidden/collision disabled until the player “equips” them OR actor classes, meaning you’ll need to spawn/despawn weapons as the player equips them. There’s pros and cons to each approach. Pros of using actor references is you can more easily store data on the actor without fear of losing it such as ammo counts. Cons of using actor references is you’ll have a bunch of extra actors floating around in the world hidden and disabled and it feels messy to me. But it’s probably still a better solution than spawning the weapon each time by class as that seems like it may take more processing to do and doesn’t allow you to save values easily since the data is lost when the weapon is de-spawned, you’d need to store the data prior to de-spawning it and re-apply it when spawning again. Anyway, let’s assume you do the first method.

So you’d need an array (ex: available_weapons) of the type of your child actor reference. It will be empty. Probably best if you disable visibility and collision on these actors by default.

On begin play, you would need to run a Spawn Actor From Class for each weapon you want and add it to the array. Then call the Equip Weapon function/event/

Then an Equip Weapon function/event. I don’t know how you’d want to reference each weapon. If you want to scroll the mouse wheel or press a number, you could simply hold an index into the array. So you could have an int variable “currently_equipped_weapon_index” with a default value of 0 (automatically equip the first weapon). You could also have it have a default value of -1 to indicate the player starts with no weapon equipped … it kinda depends on your setup if a player would ever be without a weapon. So use currently_equipped_weapon_index as the index into the available_weapons array. First I’d make sure the index is greater than 0 and less than or equal to the last index in available_weapons to ensure we’re not going to get an array index out of bounds error. Then simply run a Get on the available_weapons, using currently_equipped_weapon_index as your index into that array. You’ll get an actor reference as a return value. Enable visibility for that weapon and boom, weapon equipped. You’ll need to disable visibility on any previously equipped weapons as well. I imagine the flow of it would be as follows:

Equip Weapon (event) - Index passed in as parameter
Branch - Check if index passed in as parameter is valid (greater than 0 and less than or equal to the last index in available_weapons)
If true, connect to the next Branch. If false, do nothing - we called the function with an invalid parameter
Branch - Check if currently_equipped_weapon_index is greater than 0 and less than or equal to the last index in available_weapons
If true, get a reference to available_weapons at the currently_equipped_weapon_index, disable visibility for it. Continue to the next node
If false, do nothing specific, just continue to the next node
Set currently_equipped_weapon_index equal to the index passed as a parameter
Get a reference to available_weapons at the currently_equipped_weapon_index, enable visibility for it

Now the only thing that’s missing is a way to cycle through them. If you press a number key, you could simply call “Equip Weapon” using the key entered, minus one (since arrays are 0-indexed, the first weapon corresponds to the weapon at index 0 in the array).
If you scroll up on the mouse, you could get currently_equipped_weapon_index, add one and check if it’s greater than the last index in available weapons. If it is, use “0”. If it’s not, use the sum. Call “Equip Weapon” with that value (I suggest using a “Select Int” for that).
If you scroll down on the mouse, it’s very similar to scroll up except you need to check if it’s less than 0. If it is, use the last index in available weapons. If it’s not use the difference, pass it into “Equip Weapon”.

Please let me know if anything doesn’t make sense and I’ll try to clarify. If needed I can draw this up when I get home.

I really appreciate the amount of detail you used for this. I will be giving it a shot tonight and am pretty sure I understood everything. If not I will let you know if I run into some problems. Sorry about the late reply had gotten distracted working on modeling and importing a modular hallway.