How to save that the coin i've collected is dissapear when i change scene and come come back

how would i save variables between scenes tho? like, if i grab a coin and queue_free it, then change scenes and then come back to the scene with the coin. The coin will be there again! how do i prevent that?

You need to implement a save/load system and record the fact that a certain coin has been collected.

When you reload the scene, check the saved data and dynamically remove the coin if the save data says that it has been already collected.

i have the autorun script but i don’t know how to dynamically remove the coin :roll_eyes:

and the collect, too

you need to save to json where the scene with coin now without any coin when you grabbed the coin. so it wont appearing again on second load.
queue_free() only just removing the coin from the root tree. it doesnt remove it from your json save data.

can you show me how to save scene to json?

you kind of not do that, saving a whole scene is large. i recommend using plugin for an easy save and load with encryption

with this, you can use SaveSystem.set_var(“scene1_has_coin”,false) when you collected the coin
meaning you add a dictionary key of scene1_has_coin with false value
so every now an then you first open the game, you read the SaveSystem by
SaveSystem.get_var(“scene1_has_coin”, true)

if the SaveSystem.get_var(“scene1_has_coin”, true) return false, then no coin spawned.

i don’t understand much :roll_eyes:, i know i will save the variable dictionary, but i don’t know what kind of that var,
example, i collect the coin on the ground, so what’s the kind of var it will save (texture of the coin, position, description, res file,…)
i think of the choice that, when i grab the coin, it will save something to the save file, and when i reload or reopen the game, it will compare something in the save file vs something of that coin in game, if it == , it will queue_free() that coin in the func _ready() first.
is it possible? what is something i may save

it’s back to your game design, all i know you were saying about having the scene has a coin, so i assume just make a save data key of that scene “has coin” with false or true to determine its existence. About the position either randomly generated or texture or description, you might no need to put it in save data, because it’s always the same or randomly generated/unrelated

In my experience, the easiest way to do this is with a simple variable, like

var is_coin_collected = false

Which is saved in an autoload singleton (I have one called “Global” where I store variables that are needed across multiple scenes - singletons are pretty simple and there’s lots of good tutorials on them out there, if you need to look that up).

So then when the user acquires the coin, the coin is removed from the scene tree, and the variable is changed from false to true

Global.is_coin_collected = true

And then the next time the scene containing the coin loads, you can use the _ready function to check whether the coin has been collected, and if it has already been gotten, you can delete it from the scene, like so:

    if Global.is_coin_collected == true: 

(replace $Coin with whatever the relevant node’s name is, in the scene tree)

thanks, it’s very simple and works well.

1 Like

awesome! glad it helps!

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