can get loading to work

This commit is contained in:
James 2023-12-06 12:52:54 +00:00
parent cbcda09e08
commit 262173bee8
6 changed files with 40 additions and 19 deletions

View File

@ -2,26 +2,26 @@ extends ConfirmationDialog
func _on_about_to_popup():
$ItemList.clear()
#var dir = DirAccess.open("user://saves/")
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():
print("Found directory: " + file_name)
else:
print("Found file: " + file_name)
$ItemList.add_item(file_name)
file_name = dir.get_next()
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():
# Get the Map node
var sim = get_tree().root.get_node("Sim")
var old_map = get_tree().root.get_node("Sim/Map")
var map = get_tree().root.get_node("Sim/Map")
# Load and instance the selected item
for item in $ItemList.get_selected_items():
@ -30,7 +30,22 @@ func _on_confirmed():
if res and res is PackedScene:
var instance = res.instantiate()
sim.remove_child(old_map)
sim.add_child(instance)
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")

4
Map.gd
View File

@ -12,7 +12,7 @@ func _unhandled_input(event):
# Handle mouse button release
var pos = get_global_mouse_position()
var vol = pos - mouse_down_position
spawn(pos,vol)
spawn(mouse_down_position,vol)
func spawn(pos,vol):
var r=int($"../ScreenOverlay/HUD/HBoxContainer/Colors/Red".text)
@ -59,6 +59,7 @@ func spawn_player(pos,vol):
for collision in collision_results:
#print
if collision.collider.get_parent() is Pool:
new_player.queue_free()
collision.collider.get_parent()._on_decay_timer_timeout()
return
var marker = Sprite2D.new()
@ -66,3 +67,4 @@ func spawn_player(pos,vol):
marker.scale=Vector2(0.1,0.1)
marker.texture = load("res://images/crosshair.png")
add_child(marker)
new_player.queue_free()

View File

@ -48,7 +48,6 @@ func update():
func decay():
var new_player = load("res://player.tscn").instantiate()
new_player.get_node("CharacterBody2D").velocity = Vector2.from_angle(randf_range(0, 2 * PI)) * 100
new_player.get_node("CharacterBody2D").position = position
new_player.get_node("CharacterBody2D").exchange_with(self)

View File

@ -24,10 +24,11 @@ func _on_about_to_popup():
func _on_confirmed():
var filename=base_path+$LineEdit.text+".tscn"
var map=get_tree().root.get_node("Sim/Map")
print("Save: "+filename)
var packed_scene = PackedScene.new()
set_owner_recursive($"../../Map",$"../../Map")
if packed_scene.pack($"../../Map") == OK:
set_owner_recursive(map,map)
if packed_scene.pack(map) == OK:
var result = ResourceSaver.save(packed_scene,filename)
if result != OK:
print("Failed to save scene. Error #",result)
@ -37,5 +38,6 @@ func _on_confirmed():
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)

View File

@ -67,14 +67,14 @@ text = "Save..."
[node name="LoadDialog" type="ConfirmationDialog" parent="ScreenOverlay"]
title = "Load"
position = Vector2i(0, 36)
size = Vector2i(200, 200)
size = Vector2i(200, 500)
script = ExtResource("6_0mayr")
[node name="ItemList" type="ItemList" parent="ScreenOverlay/LoadDialog"]
offset_left = 8.0
offset_top = 8.0
offset_right = 192.0
offset_bottom = 151.0
offset_bottom = 451.0
[node name="SaveDialog" type="ConfirmationDialog" parent="ScreenOverlay"]
mode = 1
@ -90,6 +90,8 @@ offset_right = 192.0
offset_bottom = 51.0
placeholder_text = "Filename"
[node name="NewDialog" type="ConfirmationDialog" parent="ScreenOverlay"]
[node name="ScreenShots" type="Timer" parent="."]
wait_time = 60.0
autostart = true
@ -101,4 +103,5 @@ script = ExtResource("3_7b3mn")
[connection signal="confirmed" from="ScreenOverlay/LoadDialog" to="ScreenOverlay/LoadDialog" method="_on_confirmed"]
[connection signal="about_to_popup" from="ScreenOverlay/SaveDialog" to="ScreenOverlay/SaveDialog" method="_on_about_to_popup"]
[connection signal="confirmed" from="ScreenOverlay/SaveDialog" to="ScreenOverlay/SaveDialog" method="_on_confirmed"]
[connection signal="confirmed" from="ScreenOverlay/NewDialog" to="Map" method="_on_new_dialog_confirmed"]
[connection signal="timeout" from="ScreenShots" to="ScreenShots" method="_on_timeout"]