Store PlayerName variable in PlayerStateBP on OpenLevel/EditorClose


I’m having trouble in getting the hang of store/save variables upon OpenLevel/EditorClose.

I’ve learned that the best way to store information between levels is to store the variables on GameInstance - but I’ve also learned that things like Player Name and Current Score are better to store inside PlayerState for online gameplay.

As the default PlayerState exposes PlayerName only as Readable, I created a BP derived from the default PlayerState and created a PlayerName (String and Replicated) variable on it which gets the default PlayerName on BeginPlay:

As it is, it will obviously reset the variable on OpenLevel to the default PlayerName, but even if I don’t declare nothing on BeginPlay, it will always reset when entering a new level.

I know this because I have a ChangePlayerName inside my menu, which resets everytime I enter a new level:

So, what is the best approach to store variables on Editor Close/Open Level when they’re stored inside a PlayerState?

I’ve heard about the CopyProperties inside PlayerState but I couldn’t get it to work, although it prints as it should (with different before and after values):

I also been reading about Create Save Game Object and Load Game From Slot but still didn’t give it a try.

I think it all comes to the same question, in GameInstance my stored variables don’t reset to default when opening a level, while the ones stored in PlayerState do.

When I close the editor, both GameInstance and PlayerState variables reset to their default values, which is something I’m trying to solve too - but for now, I would be appreciated if I could get my PlayerName variable to not reset on opening level.

Thanks for reading.

If you want to save variable values in between editor sessions (keep the value even after you close the editor), you’re going have to use the save game, which is quite easy and simple: HTF do I? Use the SaveGame Object in Unreal Engine 4 - YouTube. And to keep values on opening new levels, you’ve to use the game instance. There’s not much other way around it, if you really want these values on player state you should probably transfer them to game instance when you want to change from one level to another, and then transfer back to player state.

Player name should be on save game, and you should get it from there, it will keep it’s value on level change and in editor close.