I have a continue button in the menu how do i tell Godot that i want to load the "parent" from the json file or how do i save the level that im on ?

in the player node i have this:

func save():
	var save_dict = {
		"filename" : get_filename(),
		"parent" : get_parent().get_path(),
		"pos_x" : position.x, 
		"pos_y" : position.y,
		"speed" : SPEED,
		"direction_x" : motion.x,
		"direction_y" : motion.y
	}
	return save_dict

and in the global script(save system) i have this:

func load_level():

	var level = "parent"


	var save_game = File.new()
	if not save_game.file_exists("user://" + name + ".save"):
		return 

	var save_nodes = get_tree().get_nodes_in_group("save")
	for i in save_nodes:
		i.queue_free()

	save_game.open("user://" + name + ".save", File.READ)
   
	while not save_game.eof_reached():
		var current_line = parse_json(save_game.get_line())
		
		if current_line == null:
			continue
		
		var new_object = load(current_line["filename"]).instance()
		get_node(current_line["parent"]).add_child(new_object)
		new_object.position = Vector2(current_line["pos_x"], current_line["pos_y"])
		new_object.motion = Vector2(current_line["direction_x"], current_line["direction_y"])
		

		for i in current_line.keys():
			if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y" or i == "direction_x" or i == "direction_y":
				continue
			new_object.set(i, current_line[i])
	
	save_game.close()

	func save_level():
		var save_game = File.new()
		save_game.open("user://" + name + ".save", File.WRITE)
	
		var save_nodes = get_tree().get_nodes_in_group("save")
	
		for i in save_nodes:
			var node_data = i.call("save");
			save_game.store_line(to_json(node_data))
	
	save_game.close()

	func delete_save_data():

	
		var dir = Directory.new()
	
		dir.open("user://")
	
		dir.list_dir_begin()

		while true:
			var file = dir.get_next()
			if file == "":
				break
			elif not file.begins_with("."):
				dir.remove(file)
	
		dir.list_dir_end()

I have not tried saving the level I am in dynamically before, but it should be possible. If you have not seen it already, I would recommend checking the Saving Games page on the Godot documentation.

To get resource path of the currently opened scene, which then you can use in a change_scene function call, you should be able to use the following:

# assuming you are storing this in a dictionary
"current_scene" : get_tree().current_scene.filename

Then when loading, you can use the following:

# We'll assume the loaded dictionary is a variable called loaded_data
var saved_scene_file = loaded_data["current_scene"]
get_tree.change_scene(saved_scene_file)

And that should change the scene to whatever scene was loaded.

One thing to note is that loading the scene will not keep the entities (like the player) in the same position they were in when saving. It will just load the scene like normal, as if you entered it for the first time. You may need to perform additional steps depending on how you want to handle loading back into the game.

Also, I moved this topic from the tutorials category to the programming category, since it is a programming question.

@TwistedTwigleg said: I have not tried saving the level I am in dynamically before, but it should be possible. If you have not seen it already, I would recommend checking the Saving Games page on the Godot documentation.

To get resource path of the currently opened scene, which then you can use in a change_scene function call, you should be able to use the following:

# assuming you are storing this in a dictionary
"current_scene" : get_tree().current_scene.filename

Then when loading, you can use the following:

# We'll assume the loaded dictionary is a variable called loaded_data
var saved_scene_file = loaded_data["current_scene"]
get_tree.change_scene(saved_scene_file)

And that should change the scene to whatever scene was loaded.

One thing to note is that loading the scene will not keep the entities (like the player) in the same position they were in when saving. It will just load the scene like normal, as if you entered it for the first time. You may need to perform additional steps depending on how you want to handle loading back into the game.

Also, I moved this topic from the tutorials category to the programming category, since it is a programming question.

Is there a better way to make checkpoints and respawning the player where he left off? How would you do it?

Well, it depends on how you want to do it. You have the right idea with storing the scene, you just likely will need to do additional processing to get the player (and potentially other entities) into the correct position after loading.

If you are okay with the idea of fixed save points, then I would have each level have a dictionary of save points and then store the scene and the key/name of the fixed save point. Then, when loading, you can spawn the player, find the correct spawn point using the saved key/name, and then move the player into position. To do this, I generally have a "spawn point manager" class that, when the scene is loaded, is passed information on where to spawn the player, and what data to use (generally from an autoload script). I've done this before for a few games and it works decently, though it has some limitations. The biggest limitation is that it forces the player to save at specified points, they will not be able to just save anywhere.

If you want to allow the player to save anywhere in the scene, without a specific save point, then you will need to store the global position of the player, along with which scene they are in. Then, when loading, you can spawn the player and manually set the position to the stored global position in the save file. Just keep in mind, with this situation, you will likely need to make some minor limitations in where and when the player can save, so the player cannot save during transitions, cut-scenes/cinematics/in-battle, etc. If you want other entities to be in the same position, like NPC characters, you will need to store their positions (and any relevant data) as well, and on load spawn/move them in the correct positions. The biggest limitation with this method is just getting it working right and fixing the little limitations that pop up as you progress. I personally have not employed this method in any of my projects, but I know from other experience it is a fairly common approach.

All in all, it really just depends on how you want to do it. I would suggest implementing whatever seems like the best for you and/or what you know how to implement. While overhauling the save/load system later in development can be tricky, the difference between fixed-save points and the ability to save anywhere is mostly just how the data is stored and how the player (and other entities) are spawned, so in theory it should be possible to swap them out later in development if needed.

Hopefully this helps a bit! :smile:

3 years later