Need help saving variables from destroyed actors

Hi guys and gals, i am in need of a little bit of help.

I do not know how to store variable values either in an array or structures or on another blueprint? and would like your help please. At the moment my weapon ammo is stored on the weapon itself, so that when i drop 1 weapon and pick another up of the same weapon type i am given the ammo that was stored on that weapon blueprint. if I drop the 2nd and pick up the first one again i am not given new ammo but only the current ammo that first weapon had.

BUT when i get into a turret system, when i get out of it i am spawned with a new character and default weapons with brand new ammo.

Can you guys please help me (preferably with pics) to get the ammo values from the old weapons stored and then when new character is spawned, the old ammo values get given back to the new spawned weapons. Also a way to store which weapons i had on in the first place also appreciated.

Please help me :slight_smile:

You can simply transfer the value of ammoes to the pickup class before destroying the weapon, or you can store the value of ammoes in a state class (PlayerState for example), it depends on your code.

store your ammo types and amounts on the player character or in the player controller. use the player character if you want the values to reset when the player dies and use the controller if you want the ammo amounts to persist through death (this is dependent on the type of game rpg vs action fps). now you will have to use a bit of casting in your weapon bp to decrement the ammo but thats pretty simple stuff.

now for the turret issue. the simplest solution to implement would be at the same time you posses the turret set the character your un-possesing to hidden in game. this will make them not visible but they will still exist. then just reverse the process. If however just hiding the character isnt an option due to enemies trying to attack it or something then you will need to copy the characteristics (variables, equipped weapon, etc) of that character to a non-volatile blueprint. you could copy the variables to the controller if they dont need to be stored between levels or something like the game instance if you do want to store the values between levels.

WARNING!!! WALL OF TEXT, Sorry!

At the moment the ammo is stored on the weapon and the weapon has ALOT of get current ammo and get current ammo in mag and Ammo per mag and MaxAmmo.

Here’s the issue with storing ammo on the character.

Every gun is a child of the parent weapon blueprint and all ammo calculation is done on the parent blueprint. So if I change the ammo to the character, then every child of the parent blueprint would have the same amount of ammo. That means rocket launchers, grenade launchers, pistols, all rifles, will have 30 in the mag and 120 max ammo. That’s 30 rockets and 30 grenades. That just wouldnt work.

I kinda need help getting the variables inside the parent weapon changed over to a structure where i CAN put an enum in there and give the enum different weapon types that go into the structure and depending on what weapon I have on depends on what ammo is given where.

Is it possible to put a structure variable onto my character blueprint, have that structure set other variables and then on the weapon have an enum variable that gets set to Rifle/Pistol/RocketLauncher/GrenadeLauncher and when either one of those is equipped, the weapon chooses which values to use from the structure on the character blueprint all depending on what weapon type they are set to?

and then from there on the ammo is stored on the character and instead of destroying the character just make it hidden. Hidden means things like line tracers and stuff will go through them right so they wont get hit if they are hidden. BUT then i want their mesh put onto the turret so if they get hit on the turret then they can still die whilst on the turret.

It’s all good me trying to think of better ways of storing ammo and making weapons use ammo, but I still need help implementing all of this xD I havent quite worked out how to use certain nodes to my advantage yet. still new to UE4 and coding/blueprints in general.

Thanks for any help/feedback too

what i was proposing was having a variable for each type of ammo on the character so that the total ammo of each type isnt decided by the equipped weapon. for example lets look at the real world if your max ammo capacity for a rifle is 120 rounds you dont carry that in the rifle you carry that on your person. so for a in game character the logic should mirror this.

so to implement what im saying you would create a variable for rockets, rifle, pistol, grenade and bfg or whatever other weapons you like. you could use an array or struct here but i went with multiple variables for simplicity sake. on your weapon whenever you fire you would cast to the character get the current value of the variable type it shoots and decrement (-1) it.

now to address a few of your points in the post more directly. im not sure if your looking to put a struct in your weapon or your character according to your post you mention both. i would suggest in the character though as pointed out above. next setting the character to hidden would hide the entire character including the mesh/ this however could be a good thing as trying to put the character into the turret with its collision and all wouldnt be an easy task. instead you may want to hide the actual character then spawn in another version of just the mesh with the getting into the turret animation. then to damage the player while in the turret you could just take any damage the turret receives and redirect it to the player via casting.