Player Component across level changes / save/load

Hello everyone :slight_smile:

So i am fairly new to Unreal Engine, just like 2 weeks by now.

I built an actor component for a survival system (Thirst, Hunger, Sleep etc.), wrote some decrementing logic, HUD logic and all and attached the component to the player actor.

Now i came across an issue, how to make the data inside the component survive a level transition and, later on, also saving / loading.

However even after much reading i can only come up with two potential solutions:

  1. Basically throw the actor component away, make a GameInstance and throw all the variables and logic in there. That is my least preferred option, as this would cause quite an extensive rewrite of a lot of things right now. But i also think it might be better in the long run, especially seeing the saving and loading.

  2. A combination of the two. So my idea was to replace all current variables with a struct in the component and also make a “Global” struct in the game instance. The game instance would get updated when necessary (e.g. actual changes happened) and when the player character gets constructed i simply set the saved struct in the players component. I just don’t know about saving/loading but it would be a lot less rewriting code.

I tested option 2 a little and it seems to work. But i am unsure if this is the “correct” way and if it would work fine later down the line.

So my question to everyone here is, what option would you choose? Or do you potentially know a better way even?

Thanks in advance!

Hi @Drakon74

So heres the thing, theres not always a correct way for everything. And using the GameInstance was the first thing i thought when i saw the topic title.

So if youre happy with what you have chosen then go with that. As long as it doesnt seem to cause you any optimisation or other issues its all good.

On a side note is to Work Round as such.

Thought of using sub levels or level instancing? Rather than explicit level swapping.

Just a thought

1 Like

First of all thank you!

I was thinking to somehow omitt level changes too, but i doubt this will work out in the long run.

I now changed the stuff to use a struct (from what i further gathered this also makes things easier for saving games later on) and it works fine!

In any case, thanks for the help :slight_smile:

1 Like