How to manage multiple weapons in project

I currently making FPS game and currently i have 1 rifle and 1 pistol in it but when i make multiple rifles how to let engine know which one player currently have equipped without having like multiple isThatandThatGunEquipped valiables and then run a branch for each of them

The cleanest way to do this is to have a base class for all the weapons. If you have multiple pawns/characters that do not inherit from the same base class where you could store what weapon is equipped you could look at having interfaces return which weapon class you have. You can then have an enum inside the base weapon class that has weapon types. Games usually use this for changing holding animations. (Hip, Rifle, Pistol, RPG, etc)

2 Likes

Hey Player_003!

This would be a great time to introduce an enumerator into your game! This way you can:

  • Guarantee only having one weapon equipped at a time.
  • Give yourself room to expand gradually instead of having a pre-determined amount of weapons.
  • Manage all of your weapons with a single variable.

As an example, you can use something like this to determine what Long Gun/Pistol you are using:

Here’s some further information if you’d like to get to know enums, they’re extremely useful and I think you’ll get a lot of use out of them!

We wish you luck with your FPS project! You can let us know here if you would like any additional options.

3 Likes

Yea i was looking for something like because i seen it in some marketplace project but never know how it was called

Hey @Player_003. I would suggest to have a variable in your character holding a reference to the equipped weapon, something like CurrentWeapon, or PrimaryWeapon and SecondaryWeapon (in case you can carry 2), or an array like WeaponSlots in case you can carry many. Now, you would need a base class for your weapons, like WeaponBase and have all your guns inherit from this class. By having this you can then know what weapon you are holding by checking the class of the active weapon, or you could also have an enum inside the base class and assign it in the child classes.

1 Like