How to properly transfer data between levels using Game Instance and Open Level?

I trying to build a save system where I store level name, player inventory and transform etc., in a save object through the game instance, when I press a certain key.
Now, after I save in a certain level and then travel to a different level (open level), I then try to ‘load’ my save through an event in the game instance like so: open level (level name being the one I saved before), and then get player character and try to apply all data saved before to it, like inventory, transform etc.

Now the problem is that the saved data doesn’t get applied to the player, unless I put a delay immediately after the open level node.
Thing is, as soon as I hit that ‘load’ key, I can actually see, for a split second, that the data is applied to the player (I’m getting transported to the place I was when I saved), but it then resets immediately to the default player spawn point in that level, with no inventory etc.
I don’t know if it’s because the saved data gets applied and then overwritten once the open level reinitializes everything, or if the player character doesn’t load fast enough, hence the game instance has no player character to apply that data to?

If someone can explain what’s going on and what’s the proper way of doing this. Thanks.

1 Like

Are you calling ‘open level’ from the GI?

It does sound like a timing problem.

By default, the game mode spawns the player at the player start. If you want to take control, you’ll need to set the pawn type to ‘none’ in the game mode for that level, then you can spawn and position the player at your leisure.

1 Like

Yes, I’m calling open level in GI, and then the rest of the functionality after that, as well in the GI, like changing the player transform.
Are you saying that the gamemode resets the player character?
Is there any documentation on what the game mode does exactly, in depth, anywhere? The official documentation is basic.

1 Like

In the level, world settings tab

image

If you leave that as is, the system will spawn that player and put them at the player start.

If you want to control it, you have a couple of choices

  1. Wait until that’s happened, and reposition the player

  2. Change the default pawn to none, and spawn your own player where you want them

:slight_smile:

In a nutshell, the game mode is basically this combination of objects. You can put code in the game mode, just like the GI, that’s up to you.

1 Like

Thanks. I’m not sure which of the 2 options is better. If I were to wait until the player is spawned, and then reposition it, where would I go about doing that, and how would I know when the player has finished spawning/initializing? Because right now I’m doing it in the GI, immediately after open level, and it doesn’t work. I chose the GI because I have the rest of the save functionality in there, not wanting to spread that over multiple blueprints too much.

1 Like

It’s really up to you, it’s a design decision.

Waiting should work, if you wait until the player is at the player start, then run your code. But you will always have that ‘jump’.

You can keep your code in the GI, no need to put it in GM.

1 Like

Well, it still doesn’t work. When I ‘load’, I run all the saved data through the player character blueprint to make sure it’s loaded so it can run. So far so good.
As soon as I introduce that open level node, it doesn’t work anymore. I tried putting it in the player character, in the player controller, and in the GI, all by itself with no other code to follow. No go.
I cannot believe there isn’t an easy way of applying your saved data after opening a level. What game doesn’t have that? Surely, there should be a simple way of doing this.

Wrong order :slight_smile:

You have to load after open level. Open level trashes everything, so you have to do the loading of the player after the call.

Maybe I read your post wrong, but it should go like this

  1. Open level

  2. Either the player is in the level, or you spawn them and possess

  3. Read everything from the save game and position the player.

1 Like

Yes, that’s what I’m doing, but doesn’t work. Don’t know if I should use level streaming instead. I heard some people saying lighting can spill in from another level when baking. So I’m a bit reluctant to do that.

1 Like

Well, you can do it either way.

Can you show your code?

1 Like

This is in the player controller:

And this is in the player character:

It opens the level fine, but the player data, like inventory and transform, are not applied.

You can have stuff after ‘open level’ if you do it in the game instance, but not the player controller.

Most blueprints, including the player controller, don’t make it past the ‘open level’ node. So this never gets run

1 Like

Yes, I did that too, but still didn’t work. I think the code after open level runs, but somehow there’s no player character object at that time?
If only there would be an in depth source on the order that things run in and how.

It you want to be sure the player runs the code, just put the code in the player.

On begin play, the player reads the save game and then set it’s position.

I also tested what I described earlier on, so I know it works. Not at a machine rn, but can paste it here tomorrow.

It was literally

open level → delay 1 sec → set player position

1 Like

Yeah, having the player do all the work, aside from opening the level, does work. The only problem is that as soon as the player spawns in the level it applies the saved data regardless if you want it or not. But I guess I can just put a condition in the GI for that.
I know delay in the GI or controller also works, but delay is a bad way of doing it, because you can never be sure the delay will be long enough in all cases, so that’s why I wanted to avoid that.
Thanks a lot for the help.

1 Like

The delay is only to get the thing working at all.

In any event, you can save a flag ( also in the save game ) to tell the player whether to load or not. But, you still get a ‘jump’ on repositioning.

The smoothest way, is to just spawn the player in the correct location ( from the save game ). For that, just make sure you have no pawn specified, and the GI can just do the spawn and possess.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.