How to have the character switch weapons

Hey,
So I’ve finally gotten the basics down for my FPS, but i have one issue, switching weapons, i want to have the option to switch from my shotgun to my assault rifle but the video tutorials i watch on YouTube don’t work for me, both my Assault rifle and my shotgun are child actor classes, i presume it’s just having to switch the child actor class after pressing the interact button but this doesn’t work!

For a quick and easy solution for an fps: use bone animation to take the weapons down and up into view, gives 100% animation freedom and its super fast to implement, it also has the benefit of being fairly easy on rendering since destroying/spawning detailed weapon meshes could cause a slight skip in frames on stressed systems - just stop updating the animation on the weapon you don’t use atm.

If you use this method I would also recommend animating unique hands along with each weapon, since then you don’t need to keep updating ik/hand tracking, this is also good for frames.

Your question should be more specific. Which youtube video are you following? Which part does not work for you? What does your blueprint/c++ code look like? That way people would be able to diagnose the issue you have.

Anyway, a simple multi-weapon design is:

  • Your character should have an array
    of weapons, each weapon bound to a
    key (1,2,3…). The weapon can be
    attached to the character via
    AttachToActor() method.

  • Your character should have a
    CurrentWeapon variable that tracks
    the current active weapon. You want
    to hide all other inactive weapon.
    When an weapon becomes active, you
    un-hide it.

  • Your weapons should derive from the
    same base class that declares some
    common functions such as Fire(),
    AlternateFire(), etc. In each
    subclass weapon you implement these
    functions.

  • You should add debug information,
    such that each time you switch weapon
    by pressing some button, the name of
    the CurrentWeapon is printed out.

1 Like

Hey, the video i’m following is this one: UE4 Tutorial: How to setup weapon swapping - YouTube. it works perfectly, turns out i forgot to make the weapons child blueprints XD, but now i have the issue that i have a shotgun, and my assault rifle, two very different weapons, fire fully automatically, and have 30 rounds, how do i make it so my shotgun fires once every two seconds and has 8 rounds while my assault rifle fires at it’s usual full-auto rate and has 30 rounds?

I fast-forwarded the video and didn’t find the part that mentions how the weapon is implemented. Perhaps it only talked about how to switch between weapons instead of how to implement a weapon system.

As you may have already done, you would need to first create the base class Weapon with variables FireRate and MaxAmmo, then create two subclasses Rifle and Shotgun both inheriting from Weapon class. I assume you are using blueprint, in which case you need to initialize FireRate and MaxAmmo to the desired values in the BeginPlay() method of the two subclasses.

Let me know if this solves your problm.

In this case the ammo / max ammo data are represented by two parties. One is the internal data, which is an integer according to your BP. The other is a string, which is shown on the UI. The catch is the string will not update itself when the internal data changes. I assume the above BeginPlay() method belongs to character? Then you need to append a node here in BeginPlay(), informing the UI of the internal data change. Of course you need to associate your UI (either HUD or UMG) with your character in advance so that you can reference UI correctly.

My suggestion is to peruse, digest and follow the official documentation carefully. UMG UI Designer Quick Start Guide | Unreal Engine Documentation

Hey! sorry i’m unfamiliar with how to do this, i set up the ammo like in the image, but it shows up a 0 ammo with 0 maxammo when i set it up in my UI.

You can use those YT tutorials if your childs are independent actors if you know what I mean. If it is component is is harder. For example: basicweapon; basicweapon_shotgun and basicweapon_sniper could work, but if they are components of the character it will be hard. You we need to use somekind of class reference in BP or a AYourWeaponName* in c++

I’m sorry, i’m being a total noob here, but the issue i’m having is the fact that i want 8 ammo for my shotgun and 30 for my rifle, but if i try to cast to weapons i always end up with 0 ammo