Best way for associating Weapon and Ammo?


Currently, my Weapon system is like this:

each weapon-derived class has its own field called AMMO and its own enumeration called WeaponType used to identify that weapon in the game.
This way, if I want to pickup some ammo for that weapon, I just need to do something like:

→ AddAmmo(20);

That will cycle through my

AWeapon* Inventory

until it finds the weapon with that enumeration and, once found, gives it some ammo

I don’t like this system. I find it unflexible.

I want AMMUNITIONS to be independent and for each weapon that I will introduce in the game, I should define which kinds of ammo it uses for its PrimaryFire and which ones for its SecondaryFire

Let’s pretend I want to make a Lightning gun in Unreal Tournament style.
Well, there will be ammunitions called, for example, “Electricity”

Now let’s say I want to make another weapon, which uses Bullets for its PrimaryFire, but as secondary fire it uses Electricity ammunitions.

From this example, you see that Electricity and Bullet are two kinds of Ammunitions that don’t really belong to a specific weapon.
They’re independent

How could I achieve this?

Enumerate ammo and specify primary and secondary types of compatible ammo for each weapon as fields. Add type field to ammo. Check compatible types of ammo and weapon before pickup.

But where do I store information for each type of ammo?

Electricity 50

Bullets 130

Rockets 15

Each ammo type has its own ammount of projectiles and whatever weapon wants to use those, it just decreases that ammount.

You’re creating the Ammo class but not using it

Why you use CountPrimary and CountSecondary when you have the subclasses?

class AAmmo
EAmmo Ammo;
int32 Count;

class AWeapon
    EAmmo AmmoPrimary;
    int32 CountPrimary;

    EAmmo AmmoSecondary;
    int32 CountSecondary;

Perhaps the rational use of StaticClass or DynamicClass (new feature) in this case as alternative to enumeraion of the ammo types.

 class AWeapon
     TSubClassOf<AAmmo> AmmoPrimary;
     int32 CountPrimary;
     TSubClassOf<AAmmo> AmmoSecondary;
     int32 CountSecondary;

bool Compatible = *(Weapon->AmmoPrimary) == Ammo->GetClass();

This is just an example of the actual number of ammo. Ammunition can contain its own parameters.

Weapon->CountPrimary = Ammo->Count;

My idea is to give the players an Inventory of Ammunitions, each one with its own EAmmoType (enumeration) and CurrentAmmo (int32) properties.

So the Player has an inventory of Ammo and he can add ammo to it through an Inventory search by EAmmoType.

Is this a good idea?

EAmmoType is an enum, so you don’t have to search through an array of structs, you can convert the enum to a byte, and use that as an index into an array of integers. just set up the array in the same order as the Enum.

What if I have a particular weapon where it’s secondary fire uses a kind of ammo which requires the weapon to change something like the way it traces?Should I make a C++ class for each of these ‘particular’ weapons?

And now for blueprint form pls?

this is C++ buddy. You can create a wrapper for this C++ to call it from BP pretty easily. Its called google.