I have coins that disappear and add to a counter at the top (I found a way for the counter to save over scenes with a global script) but I’m having trouble having them stay gone when I change scenes. I’d also like if when you go to the second scene and back to the first the player didn’t start at the beginning of the first scene.
The way I’d do this with the kinds of systems I use, is have a CoinPosition scene or just a Marker2D to represent the position of a coin. Then, the scene responsible for loading my level gets the positions of the coins and check each one against some storage of retrieved coin positions.
If you set the global_position of the player, the player will be there. Just put the player wherever you want.
I mean, you’re giving no info at all on what kinds of systems you have, how levels are loaded etc., so probably no one can give you any better advice than abstracts.
You need some way to track and save the state of your scene so it can be loaded back to that state when you switch back to it. There are multiple ways this can be accomplished. I would approach this from the mental perspective of a default_state
and saved_state
, where the saved_state
only tracks the deltas from the default_state
.
Assume you are saving the state as a Dictionary, this would be the default state:
{}
When you enter the scene for the first time, you pass in a default_state
and create a saved_state
to start tracking changes as your player moves around and collects coins. When you exit the scene you can save the player position. This could result in a saved_state
that looks like:
{
"CoinsCollected": ["Coin1", "Coin2", "Coin3", ...etc ],
"PlayerPosition": Vector2(500, 500),
}
Now when loading the scene you pass in the saved_state
and have the scene iterate through your Dictionary and start removing collected coins and update the player’s position.