JHRGB/LoadDialog.gd
2023-12-06 12:52:54 +00:00

52 lines
1.4 KiB
GDScript

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():
print("load:", $ItemList.get_item_text(item))
var res = ResourceLoader.load("user://saves/" + $ItemList.get_item_text(item))
if res and res is PackedScene:
var instance = res.instantiate()
print("Loading:",instance.name)
# Delete all children of 'scene'
for child in map.get_children():
child.queue_free()
# Copy all children of 'new_scene' to 'scene'
for child in instance.get_children():
#var child_copy = child.duplicate()
#map.add_child(child_copy,false,Node.INTERNAL_MODE_BACK)
instance.remove_child(child)
map.add_child(child)
print("adding ",child.name)
instance.queue_free()
res=null
$"../SaveDialog/LineEdit".text=$ItemList.get_item_text(item).get_basename()
else:
print("Error loading")