0 votes

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.

enter image description here

followed this guy's tutorial for that, but in his example, he was using 2d:
https://www.youtube.com/watch?v=YraYIe-q0uo&list=PLHMAyNnpn6fFhM5n6b-y1PiOp1dD9AYYx&index=5&t

Here is where I call the func update spawn in the area node
enter image description here

Also followed this guy's tutorial:
https://www.youtube.com/watch?v=jvhWbEIZWjo&list=PLHMAyNnpn6fFhM5n6b-y1PiOp1dD9AYYx&index=2
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):
enter image description here

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.
enter image description here

The problem is when I run it and restarts it, it says the code below:
enter image description here

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

Godot version Godot 3.4.4
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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.

by (7,742 points)
selected by

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

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 ?

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?

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 loadgame(), inside while loop, under nodedata 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 ;)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.