extends ConfirmationDialog func _on_about_to_popup(): $ItemList.clear() var dir = DirAccess.open("user://saves/") var file_names = [] if dir: dir.list_dir_begin() var file_name = dir.get_next() while file_name != "": if !dir.current_is_dir(): file_names.append(file_name) file_name = dir.get_next() dir.list_dir_end() file_names.sort() # Sort the file names for f in file_names: $ItemList.add_item(f) else: print("An error occurred when trying to access the path.") func _on_confirmed(): var map = get_tree().root.get_node("Sim/Map") # Load and instance the selected item for item in $ItemList.get_selected_items(): var filename = "user://saves/" + $ItemList.get_item_text(item) print("load:", filename) if not FileAccess.file_exists(filename): return # Error! We don't have a save to load. # We need to revert the game state so we're not cloning objects # during loading. This will vary wildly depending on the needs of a # project, so take care with this step. # For our example, we will accomplish this by deleting saveable objects. var save_nodes = get_tree().get_nodes_in_group("Persist") for i in save_nodes: i.queue_free() # Load the file line by line and process that dictionary to restore # the object it represents. var save_game = FileAccess.open(filename, FileAccess.READ) while save_game.get_position() < save_game.get_length(): var json_string = save_game.get_line() # Creates the helper class to interact with JSON var json = JSON.new() # Check if there is any error while parsing the JSON string, skip in case of failure var parse_result = json.parse(json_string) if not parse_result == OK: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) continue # Get the data from the JSON object var node_data = json.get_data() # Firstly, we need to create the object and add it to the tree and set its position. var new_object = load(node_data["filename"]).instantiate() # Check the node has a load function. if !new_object.has_method("load_data"): print("persistent node '%s' is missing a load_data() function, skipped" % new_object.name) new_object.queue_free() continue new_object.load_data(node_data) map.add_child(new_object) $"../SaveDialog/LineEdit".text=$ItemList.get_item_text(item).get_basename()