I’m having issues setting up a persistent ammo system for the weapons in my game. The way I want it to work is that the ammo is stored in the weapon when the player drops it so that when they go to pick up the weapon they just dropped the ammo that they left in there is still there. What’s happening currently is that the ammo resets every time the player picks up a weapon, basically like the weapon MASTER variables are overriding the child blueprint variables, which I want to preserve.
I’ve tried many things to solve this, so far none of them have worked and I’m admittedly kind of at wits end over this. It’s a considerably simple system, but my knowledge in programming and blueprints is novice enough where this is proving to be quite challenging.
I’m not sure what kinds of screen shots would be helpful to anyone willing to go over this with me, so just let me know what you need to see and I’d be happy to provide them.
Hey there @DannyMacFinn1! Welcome to the community! It sounds like you might be creating an instance of a weapon in your hand when interacting the one on the ground instead of taking the actual item.
Could we see the BPs where you pick up the weapon?
Your drop weapon needs to have a replicated inventory. I use a struct in mine do to all the attachments. grip, optics, cosmetics, mag type, ammo, muzzle, stock, canted sights etc.
When I drop it I copy the struct to the simple world pickup item. Just need to remember to check the weapons inventory on pickup and configure the one you spawn and attached.
Ammo reduction and reload has to go through the weapon class inventory as well.
This is basically where my problem lies, I can’t seem to get any variables to pass through from my weapon to my pick up then back to my weapon. It’s a skeletal mesh component blueprint, which I think is where the issue lies, because I can’t seem to cast to it correctly in the player character.
When you Drop Weapon you get the spawn class and spawn your pickup object. You only set Dropped by Player bool. Where are you supposed to set Stored Ammo in Clip and Stored Spare Ammo?
The ammo is set in the Get Current Weapon Function, it pulls the Ammo In Clip and Spare Ammo variables from the weapon master class that is in the equip index and sets that to the stored ammo variables on the player character.
It seems when you’re spawning the weapon, you’re creating a new instance, and using the Class Defaults which is why you’ll always get the default values. Your two easier options would be to:
A: Set the variable on the pickup item instance itself, then keep the actual weapon and attach that. This isn’t ideal, but it does mean you have to modify the script less.
B: As Rev mentioned, the cleaner way would be to have a struct that houses the weapon data attached to it, collecting that data on pickup and applying that to the created instance in your hand.
I’ve modified it to do just that, at least I think it’s supposed to be doing that. I have a PC Stored Ammo In Clip and PC Stored Spare Ammo as well as a Stored Ammo In Clip and Stored Spare Ammo, the PC stands for Player Character and those are empty integers that live in the player character blue print. The non PC integers live in the pick up class.
When I drop the weapon the pick up class stored ammo variables are supposed to be set based on what the current weapons ammo is at the time of dropping it, this is what that code looks like.
Then, when I pick up a weapon it should check the pick up class bool of DroppedByPlayer?, and if true, then it should pull from the PC Stored ammo variables. If false it’ll resort to the class defaults as seen in this code here.