JHRGB/SaveDialog.gd
2023-12-07 01:05:50 +00:00

63 lines
1.9 KiB
GDScript

extends ConfirmationDialog
var base_path = "user://saves/"
func _ready():
print("check for saves folder...")
if DirAccess.dir_exists_absolute(base_path):
print("...exists")
return
print("...not exist, create...")
DirAccess.make_dir_recursive_absolute(base_path)
var dir = DirAccess.open("res://saves/")
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
print("skip directory: " + file_name)
else:
print("copy file: " + file_name)
DirAccess.copy_absolute("res://saves/"+file_name,"user://saves/"+file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
func _on_about_to_popup():
pass # Replace with function body.
func _on_confirmed():
var filename=base_path+$LineEdit.text
print("Save: "+filename)
var save_game = FileAccess.open(filename, FileAccess.WRITE)
var save_nodes = get_tree().get_nodes_in_group("Persist")
for node in save_nodes:
# Check the node is an instanced scene so it can be instanced again during load.
if node.scene_file_path.is_empty():
print("persistent node '%s' is not an instanced scene, skipped" % node.name)
continue
# Check the node has a save function.
if !node.has_method("save_data"):
print("persistent node '%s' is missing a save_data() function, skipped" % node.name)
continue
# Call the node's save function.
var node_data = node.call("save_data")
# JSON provides a static method to serialized JSON string.
var json_string = JSON.stringify(node_data)
# Store the save dictionary as a new line in the save file.
save_game.store_line(json_string)
$"../HUD/HBoxContainer/ToolSelect".populate_paste()
func set_owner_recursive(node: Node, new_owner: Node):
if node != new_owner:
node.owner = new_owner
print("chown ",node.name)
for child in node.get_children():
set_owner_recursive(child, new_owner)