- Edited
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()