x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.17
Tags:
more ▼

asked Nov 20 '17 at 04:15 PM in Blueprint Scripting

avatar image

josh98012
8 1 3

avatar image Grot13 Nov 20 '17 at 06:33 PM

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=5w594D3qtLs

https://www.youtube.com/watch?v=XjPdyoy7ySo&t=3s

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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

more ▼

answered Nov 20 '17 at 11:28 PM

avatar image

Shadowriver
37.8k 937 172 1123

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question