How to make a checkpoint system?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By parataposna

Hello, I am trying to make a checkpoint system, that if the player has entered an area. The player’s spawn point would change to that area’s location.

My goal:
That I want to restart the scene or reload it in some way. so that everything resets when the player dies or clicks the restart button. BUT, it should retain the player’s spawn point to that area’s location.

I have tried using a singleton named “Global”. It has a vector3 and a function that updates the vector3 when the function is called.

followed this guy’s tutorial for that, but in his example, he was using 2d:

Here is where I call the func update spawn in the area node

Also followed this guy’s tutorial:

I tried to make it so that the area node’s vector3 would be saved and could be loaded back when the player restarts.

here is the Save script (basically copied the Tiny Legions’s code):

When the player dies it shows a ui where there would be a button named “Restart”.
And this is the code on what happens when the button is clicked.

The problem is when I run it and restarts it, it says the code below:

I am unsure on what to do and I am open for other alternative methods on making a checkpoint system that meets my goal.

:bust_in_silhouette: Reply From: Inces

Did You by any chance add your Global to group “Saved” ?
Because what error says is You have just queued free your global script :). I assume it happened during loading.

Yep, I added it to a group “Saved”. I am unsure tho of what to do. What can I do to fix it?

parataposna | 2022-08-03 01:17

Look what happens :
When You load a game You attempt to delete current existing nodes and replace it with new ones. Their older ID is deleted, and new nodes get new ID.
But Your autoloaded script, referred to as Global, is supposed to have original ID, because the rest of the script depends on it.
So You can’t queue it free. Remove it from “Saved” groups, and save/load its contents manually. What kind of data do You want Global to save and load ?

Inces | 2022-08-03 13:07

Oh, I get it now. Thanks for the explanation. For the data, I want the coordinates of a checkpoint that a player has been set to. So might it be possible to save only the translation of the checkpoint and make it not depend on the old node’s ID?

parataposna | 2022-08-04 14:40

In your save system You draw chosen data from your nodes using get_save_stats function. This function translates nodes properties to Strings. Strings are lined one below the other for every saved node in one file. Every node has the same things saved - parent and position. Your Global script doesn’t have parent nor position, so it would throw different error later anyway.
You want the Global to save only last checkpoint position.
So You need to store it manually. In save_game() function, before it closes file, add line :

save_file.store_line(to_json({"lastcheckpoint" : Global.#your_variable_name_here#))

and in load_game(), inside while loop, under node_data introduction:

if node_data.has("checkpoint"):
        Global.#yourvariable# = node_data.checkpoint
else:
       # the rest of the code must be here, indented

Now You can remove Global from Saved group, its ID will be safe, and only checkpoint data will be stored/loaded

I could rework your code and post it, but You pasted print_screens, so I can’t edit them :wink:

Inces | 2022-08-04 15:13