Saving player character values for respawn

I’m planning on making a rogue-like game, I’ve placed the player character’s stats on a parent character class like HP, XP etc. (I really don’t know if that’s the right place to put it but youtubers I’ve watched put it there and I’ve already webbed too many commands to just put it somewhere else). The game I’m planning on making has a respawn system, I’ve managed to make the character respawn on death(destroy actor) but the problem is that it respawns with 0 stats.

So how can I save the pawn’s current game stats so when it can respawn, it will respawn with current stats and then reset it to default after a new game? Also, can you also specify which BP, Player State BP or Player Controller BP or somewhere else, is best to put these commands?

Edit : I forgot to post my screenshot

First of all, what happens when a player dies? Does the level restart? Like, do you use the “open level” node and just re-open the same level they died on so they can try again (Mario, for example)?
Or does the player just respawn back into the same exact copy of the level they died in?

It sounds like it is the latter from your OP, but I’m just checking.

Also, it may be difficult to use destroy actor here if the actor you are destroying has information you still want to use. You might have to plug all the variables into counterpart variables in a game instance and then give them back to the player when they respawn.
Just finding a way to not destroy the actor in the first place is probably easier though. It depends on the rest of your code on if you can do that or not.

You need a save game

The character respawn to that same level but only if it has another life

Okay, so not destroying the actor is an option then. Do you have a way to just make it LOOK like the player died and then move the actor to the respawn spot? That would save a lot of headache making a bunch of variables in another asset.

If you are unable to do that, then you will have to save the variables somewhere else and retrieve them on respawn. I think @ClockworkOcean 's suggestion of a save game for your deaths would work well.

You could also use stuff like the game mode (if you aren’t planning to keep the variables more than that level) or game instance (if you want to keep the variables the same for next level as well). I’m assuming this is a single player game.

1 Like

@Detach789 Yes, it’s a single player game. What did you mean by look like it died? like a death animation? What’s the 1st option you’re talking about? I’d like to try that 1st if it will work

Yeah, you pretty much have the idea right there.

You say you have all the variables just like you like them in the character already, right? The only “problem” is the player got killed by a mob or fell in the pit or something right?

So, put a death animation just like you would if the player died, do all the particle effects or “YOU DIED” text or whatever you would normally do, and then instead of destroying the actor and making another one, just set the actor’s location to wherever they were going to respawn.
You might have to set their healthbar back to 100% if you are using a healthbar in your game though.

You mean just use the Teleport command and set the health back to 100%?

Yeah, pretty much. And 100% mana if you are using that, set your camera there if you are using a separate camera, etc. and such.

EDIT: I realized I’m a bit biased here because I mostly make extremely simple games where simply teleporting the pawn somewhere without any extra effort is enough to “restart” my game.
If you are playing with a whole bunch of variables like your characters pick up weapons that you need to remove when they die, they have a bunch of buffs you have to clear, etc. Then maybe just setting all the variables you want to keep in another asset and then giving them to a new copy of the actor really is the correct choice for you.

The problem with re-using the same Pawn is that you might forget to clear some affliction or effect or equipment or whatever.
I personally prefer to just make everything from scratch again.

There are a few places you can put this information in the Unreal gameplay framework.
GameMode is one such place – it stays loaded forever for the level, and has server authority, so it’s convenient from that point of view. If you load another level, you may get another GameMode, though.
Another is PlayerState – this might actually be the most optimal in this case, as it persists with a particular player. If you need it to persist across game sessions, you’ll use SaveGame for this.
PlayerState is also server-authoritative (where created) and as a bonus is replicated to clients if you end up doing networking. Even for single-player, it’s probably the optimal choice.

1 Like

@jwatte This is how my game would work. I choose a character, let’s say Mage, and play a level as that character. That Mage has Max HP, Level etc. that will increase in that game session only and if it has an extra life, it will respawn with the current stats. Once that game session ends, the Mage’s stats will return to it’s default stats, like Level 0, XP 0 etc. But there’s going to be a shop where I can buy permanent upgrade stats across all characters like Plus Max HP, Movement Speed etc. that will be added before a game session begins. For example, if the Mage’s default Max HP is 100, after I buy a Max HP+10 to an upgrade shop, the Mage will start a game session with a Max HP of 110.

How should I approach this?

hi,
after you bought it give it to GameInstance and make a SaveGame Object… if you close your game and come back later your purchase will still be there…

or you could make a shop widget and transfer the values to GameInstance and later load a new level…

hope this helps you :slight_smile:

cheers :vulcan_salute:

1 Like

If it’s permanent, then you need the save game.

I understand the concept now. Thank you to everyone who helped

1 Like