Modular Weapon Ammo Storage?

Hopefully I can make this clear, but Ive run into an issue with trying to figure out how I can create a system for my weapons that ammo is treated as a separate blueprint, as well as the total ammo for that type of ammo. If you play any modern FPS game, you see that different guns use different ammo, or sometimes the same, as in the case with several kinds of hand guns using the same 9mm ammo. Im interested in making an ammo system that you can define the type of ammo, so say 5.56, and then that ammo includes its own ammo pool, so lets say you have a total of 100 rounds of 5.56, and two rifles that use that ammo. Both guns will take ammo from the 5.56 ammo pool and use that when reloading, so effectively it is split between the two guns. How could I make something like this? Im having a hard time trying to figure it out and any help would be appreciated.

Store the ammo on the character or somewhere that is unrelated to the weapons themselves.

You could create an “Ammo” struct that includes an Enum (“EAmmoType” or whatever you wanna call it) for your type of ammo, and an int for the ammo count. Then you’d have an AmmoInventory array (of the type of your struct) on your Character that has an entry for each type of ammo.

Have an EAmmoType variable on your weapons that defines what type of ammo they use, and when you consume ammo just find the corresponding entry in your AmmoInventory array and modify the ammo count.

Thanks for the reply, Ive tried using arrays and structs but there seems to be a bug in UE4 since 4.4 where you cant properly edit a struct or an array.

Fixed in 4.13. Structs can be wonky at times but setting their elements using loops works fine.

I’ve successfully used struct arrays in many projects even back in 4.8.

Weird, everytime I would use the set members node, the information inside would no update no matter what.

In my game, I have a Weapon class (derived from a Base Item class).

The Weapon interface provides a function called “get Ammo Types” which returns an array of Base Item classes.
(Ammo and Weapon both derive from Base Item class!)
The Ammo Interfaces provides a struct called “Ammo Properties” that has all the information what the firing of a weapon does:
Number of Projectiles, Inaccuracy, The Projectile Type, etc. etc.

Here’s a video of my inventory. Everything in there is actually a “Base Item”. The Functionality (whether the Item is a weapon or in the same category) lies within interfaces.

Not entirely sure where the issue lies but you may want to investigate the way you handle structs. I’ll give you an example:

Here, I’m looping through an array, modifying the string var and it works fine when I print it. Once out of the loop, I access it again, and… my original array was not modified, the value of string was not set.

If you look at the loop macro (or peek inside), you’ll notice that it actually returns a copy of the struct rather than a reference. So yeah, that’s something to remember. Can it be that one of your interfaces creates copies when “The Ammo Interfaces provides a struct”?

btw, that’s a very early TombRaiderish looking menu!

edit: the right way to do it

1 Like

Thanks for the reply! What youve done is what I originally wanted to do, but I cannot for the life of me edit any variables in a struct and set array elem without it adding a new element to the array. Im also not sure how you were able to add the POI Common input to the Set Members In Cluster node, as I only have Struct Ref and Struct Out.

Select the *SetMembersInStruct * node and tick the elements you want to modify in the details panel. The ones left unticked will be left unmodified.

Thanks, I figured that out not long after I posted. Although Ive seem to run into the original issue I was talking about before, where I cant edit a value in the element, because if I do it ends up creating a new index which defeats the purpose of editing it in the first place.

Outside of the Box Post (ignore if beyond your scope): You could have the various ammo types stored on your character that possesses the ammo and have the weapon itself feed its own ammo variable from the character, thus storing the ammo in the weapon itself.

Example: You find a gun that uses 9mm ammo. You pick up the gun, it does not have any ammunition in it (not loaded). You load 10 of your 100 rounds of ammo into the gun. You now have 90 rounds of 9mm ammo in your storage box (backpack, pocket, whatever). The gun has the other 10 rounds in it. You shoot once, expending 1 9mm ammo from the gun. The gun now has 9 rounds. You drop the gun. The other character, we shall call them Susan, walks past the gun and decided to pick it up. The gun still has 9 rounds of 9mm ammo in it. Susan removes the ammunition from the gun and drops the gun to the ground.

The gun now has no ammunition, Susan has 9 more rounds of 9mm ammo than she did before she found the gun and you are still short the 10 rounds you put into the gun.

My only issue with a system like this is that it requires me to manually add each new ammo type as a variable in the player which is not quite what I want, I am trying to figure out an automated system for dealing with the ammo.

Not necessarily, you can still use enums and structs for the variable.