Storing Health Variable between Characters?

Hey,

Basically, I have 2 characters that the player can switch between using Tab. The characters both have 100 health to start with, and can obviously take damage. However, if I switch to character 2, and take 20 damage, my health goes down to 80, but if I switch to Character 1 and then back to character 2, the health of character 2 resets to 100.

Is there anyway to make sure that the character’s health always stays the same? Possibly using Managers?

Thanks!

Hi, you should take a look at the documentation about game instance. Basically it’s a blueprint that is persistent and stores variables across maps.
In this game instance object, create one float variable (or int or byte) for the first charcter health and one for the second.
Here is nice tutorials:

https://www.youtube.com/watch?v=XjPdyoy7ySo3s

Blueprint are just class of object, like stamp matrix to make a object. When you make a object on the level is has it own individual state and set of varables. So each Pawn/Character you gonna make will have it own health state, thats why when you switch character it changes health of current character. You have few options how you want do :

-Easiest way, simply transfer health state of one character to another on character switch ;] if your game design allows, first character will maintain old health state whatever you do to him after switch, so if health is effected when he not controlled, then use different solution. You also need to trasfer health back ons switch back.

-Read health only from one character if your game design allows you to do it. For example you got main host character and other are just subordinates. this will require for you to keep reference of main character in subordinate character to apply any changes. But note that when object is destroyed all data is also destroyed and removed from memoery, so host character need to be alive all the time, or else you will get health reset on respowning him.

-Keep health is PlayerController, each player has PlayerController object that controls possessed pawn, data in it will survive death of pawn/character as this object only resets when world resets (change level or restart level) or player leaves and reenters the game in multiplayer. Alternatively you can also use GameMode class, but there only one GameMode per game so this will work only for single player, it also resets on world reset

-If you need even more persistent location of your data use GameInstance (as Grot13 suggests ;p) which is persistent version of GameMode class and it stays for entire game session and don’t reset on world reset. Same as GameMode there also single GameInstance, so you can’t use it in multiplayer or else you will keep track of players coming in and storing there health in array

I recommend you to real about principles of object oriented programming, since UE4 heavily use it and you seem to lack basic knowledge about it