Godot Version
4.1.2
Question
Hi. When I run my load function, nothing appears to happen from the player view, except for the part where it removes all the TextureButtons in the ImageObjects group. When I look at the remote view of the scenetree, the duplicated ImageObjects aren’t anywhere to be found. I am not getting any errors, none of the variables are coming out as null, everytime I tested this code the save was successful. Not sure what’s wrong here, though I’m sure it’s some huge problem that I accidentally overlooked.
extends Button
func save_game():
var file = FileAccess.open("user://savegame.json", FileAccess.WRITE)
var image_objects = get_tree().get_nodes_in_group("ImageObjects")
var saved_data = {}
saved_data["buttons"] = []
for child in image_objects:
if child is TextureButton:
var button_data = {}
button_data["positionX"] = child.global_position.x
button_data["positionY"] = child.global_position.y
button_data["texture"] = child.texture_normal.get_path()
saved_data["buttons"].append(button_data)
var json = JSON.stringify(saved_data)
file.store_string(json)
file.close()
print("save successful")
func load_data():
var CopyImage = $"../../../../Copys/CopyImage" #This is a TextureButton that I am copying.
var file = FileAccess.open("user://savegame.json", FileAccess.READ)
var image_objects = get_tree().get_nodes_in_group("ImageObjects")
var json = file.get_as_text()
var saved_data = JSON.parse_string(json)
var new_parent = $"../../../../OnSceneItems"
for child in image_objects:
if child is TextureButton:
child.queue_free()
for button_data in saved_data["buttons"]:
var button = CopyImage.duplicate()
button.add_to_group("ImageObjects")
button.reparent(new_parent)
button.texture_normal = ResourceLoader.load(button_data["texture"])
button.global_position.x = button_data["positionX"]
button.global_position.y = button_data["positionY"]
button.set_name("TheHardestPartOfCodingIsNamingThings")
file.close()
func _on_load_pressed():
load_data()
func _on_pressed():
save_game()
If you have any questions, feel free to ask. Thanks!