I’ve searched throughout the internet and the forums but all I found was switching between a weapon you have and one on the ground.
Basically I’d like to know how can I switch between 2 or more weapons I already own, simply by clicking 1 or 2, or scrolling the mouse.
So for example, I spawn with a pistol, and when I click 2, assuming I have the weapon, I switch to a shotgun.
Simple answer, yes you can.
You’ll need the two weapons, a weapon swap animation (or possibly split into two), an input event (button binding) for swapping weapons, and a boolean variable for PrimaryWeapon or something of that nature.
The basic idea is that on Input Event - SwapWeapons, you check your boolean to see which weapon is currently equipped, then hide that one (or toggle vis on it and a currently-invis mesh somewhere on the body) and turn off its functionality, while doing the opposite for your unequipped weapon. It’s really not super difficult, but you’re right in that there isn’t a lot of documentation or tutorials on the topic.
I am uncertain what issue you’re having with your current system, but it is very different than the one I proposed so no, they really wouldn’t work together but that’s okay.
The only issues I see with yours right off the top of my head are:
- You’d need to destroy the weapon
you’re swapping away from
- You’d need a way to check if you
have the weapon you’re swapping to
- And you’d probably be better off
spawning a child actor component
instead of an instance of the BP
itself for the purposes of blueprint
communication ease.
If you want to switch between two or MORE weapons then you can use an Array within the Player blueprint. (UPDATE 2: This will work if you use the mouse wheel to switch or a button that must be pressed each time you want to switch.) Call this array you create EquippedWeapons and make the type as the MasterWeapon blueprint or the main weapon blueprint you used to create all of the child weapons from, but make sure it is of its class, not references.
Create two functions for this array called Add Equipped Weapon and Remove Equipped Weapon and give both an input of the type of your master weapon. Make these functions add and remove one item from this array that you choose through the parameters.
Now create your variable that tells you what weapon class is equipped and name it what you want as the class Master Weapon or whatever you named your master BP. Then go into your input settings under Project Settings and add one for next weapon. Once done, create, in the Player BP again, the input action event and add a script to check if the next weapon is existent, if not set weapon to the first in the equipped list.
If you do it this way, and add some nodes in to change the physical weapon, you have yourself set to have a switchable array of weapons equipped. You can also check in the Add Equipped Weapon event that the length is less than some number to limit how much you can have in your inventory. Let me know if you need any more elaboration on this.
UPDATE: If you want to make it even easier on yourself in the future, you can even make a new Component to attach to your player that does all of this for you, and set events to communicate with the component; and on Begin Play you can have your save function load the information into the component. If you want some help with this let me know and I can elaborate a bit more on the Component aspect.
‘You can also check in the Add Equipped Weapon event that the length is less than some number to limit how much you can have in your inventory’
I’ve been trying to limit my array so that i can only pick up so many guns but at the moment the number is infinite almost.
How do i limit the amount i can hold?