x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Weapon Swapping?

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.

Product Version: UE 4.11
Tags:
more ▼

asked Aug 15 '16 at 08:12 PM in Blueprint Scripting

avatar image

BlizzKrut
3 3 5 8

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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 the Array

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.

Create the Events

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.

Add Input Event

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.

more ▼

answered Apr 04 '18 at 12:29 AM

avatar image

BubbaBonkers
120 4 5 6

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Aug 18 '16 at 05:15 AM

avatar image

rbmiracle
232 10 9 23

avatar image BlizzKrut Aug 18 '16 at 06:20 PM

Any way I can work that with what I already have? alt text

alt text

avatar image rbmiracle Aug 19 '16 at 11:36 PM

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:

  1. You'd need to destroy the weapon you're swapping away from

  2. You'd need a way to check if you have the weapon you're swapping to

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

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question