Download

(C++) How should you handle weapons?

I’ve been stuck trying to create an old school weapon system like in Doom or Half-Life where you’re running around with 10+ weapons that you can switch between at any time. I would like some guidance on how I’m supposed to implement something like this. Please let me know where I’m wrong.

What I currently have is a C++ base class for my weapons that holds pretty much everything I need for the weapon like damage values and all that. All the different weapon classes are blueprint classes that derive from the base class. I’m having trouble figuring out how to access those weapons.

On the player class I have a TSubclassOf<BaseWeapon> variable for each weapon that I want, so that I can easily change the class if I want to, and as far as I know you can’t reference a blueprint class through code. I intend on using that class to get its damage and all the other stats. But since it’s a TSubclassOf<BaseWeapon> it could be any child class of that, so the code doesn’t know exactly what class it is and therefore cannot be used to call functions from it, right? And casting has so far just crashed the whole engine, although I’ve never used casting in actual code before, only blueprints, so I could be doing something wrong there. My cast looks like this:


 ABaseWeapon* PistolCast = Cast<ABaseWeapon>(PistolClass); 

(“PistolClass” is a TSubclassOf<ABaseWeapon> like I said)

I have a mesh component on the player that I intend to set to the model of the weapon class (I haven’t gotten here yet, though, due to to the problem above).

So, ultimately: Should I be using TSubclassOf<>?

I feel really dumb right now.

TSubclassOf<> is a class, not an instance. The cast is failing because the two types are unrelated.

You need to spawn a weapon actor using the Pistol Class variable. I’d suggest looking at how the ShooterGame example project handles weapons and inventory. It’s an old example, but it still serves as a decent starting point.

Ah, right. Thank you so much, TheJamsh.

You can also look at the unreal tournament 4 source as inspiration. There are also a bunch of packs on the market place that can be a good base if you don’t feel like coding it up from scratch. I myself wrote our own weapon system that we may put out on the market place soon and I have a ton of customizeability but if I could go back I’d use a pack from the marketplace to save some time. They may have random limitations but you can just work with the limitations and get a game out.

At a high level, have an inventory system that keeps track of all of the weapons carried by a character. Have an input action that switches weapons and have something that is used to determine what type of weapon to switch to. As I seem to recall, UTWeapon had a WeaponGroup field and we bound the input action SwitchWeapon X to allow you to quickly switch to the first/next weapon in that group. You could do the same thing. In your base weapon class have a member WeaponGroup and when you get the SwitchWeapon action, find the weapon with that group and switch to it. You could come up with a way using the weapon’s class but that’s way overkill. A tag on the object would work too, but a simple int works just just fine.

Download Shooter Game.
It’s free and everything is in there…