Weapon system design help

Hello friends,

So I’m working on a weapon system which I try to design as follows:
Each type of weapon inherits from a MasterWeapon class. So AssaultRifle, SMG, Pistol and Shotgun classes inherit from master weapon and I create different archetypes of those types for each weapon.

So for example if I had an M4A1 weapon it would be like:

MasterWeapon - this is an abstract class that holds no implementation
AssaultRifle - this class implements functions from the abstract class like Shoot() / Reload()
[INDENT]M4A1[/INDENT] - this class just hold the different configuration for the current weapon like Rate of Fire, Ammo etc

So my question is:
So what I need is my character to keep track of the two currently holding weapons (primary and secondary) So I thought of creating this:
public:
AMasterWeapon PrimaryWeapon;*

So, for example if I get a reference of M4A1 class, can I set it to the PrimaryWeapon pointer? And will it keep the data? Is that a good approach? Is there a better solution for this problem?

Hi OzoneBG,

This approach is expected given the large amount of OOP materials out there for programming. Here are some things to think about:

  • Why do I need inheritance?
  • Where should I put the common variables?
  • Is this easily extended for future objects?
  • Inheritance isn’t free and so what’s my performance impact?

From the way you are describing your ranged weapons, you really just need a structure that holds all the properties. For example, regardless what type of rifle you are making, they simply vary in:

  • Fire rate
  • Recoil
  • Ammo count

Obviously you might also vary in the actual 3D model which is stored in the Mesh component. Since your requirement is so simple, you should avoid using inheritance and have a single RangedWeapon class that holds those variables and have each children weapon override those values. You can do this by creating a configuration objects and each children will simply provide a different configuration.

Hope this helps.

Yes I’m aware of the OOP principles and I have set a structure to keep all the configuration variables and common variables inside the MasterWeapon, all other classes override those. My question was. If I have a pointer of MasterWeapon can it hold a memory reference to M4A1 or it will loose data?

Take a look at the C++ Shooter Example code for a nice approach we also use in our game.

To simply answer your question, yes it will hold it just find as long as your M4A1 is a child class of MasterWeapon. Basically you can:

TArray<MasterWeapon*> AllWeapons;
AllWeapons.Add(NewObject<M4A1>());
AllWeapons.Add(NewObject<M5>());
AllWeapons.Add(NewObject<M60>());

Calling method directly on MasterWeapon* will polymorphically resolve to the right child.

Thanks, it actually did work :smiley: