Trouble with load function

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()

thingofdeath

If you have any questions, feel free to ask. Thanks!

first thing to check if there’s saved data in the json is to check it directly if there’s any data inside it after you click save
if there’s then try to print something inside

if there are prints,
then
probably the way you setting up the buttons from save data is not right

I know for a fact the save is working (I tried your suggestion to double check), I just don’t know what’s wrong with the loading. Was hoping the internet could spot something wrong.

have you tried these?

Not sure what you mean, but yes, I have tried printing out the data from button_data and it did print everything I saved.

then the problem lies at how you add the buttons

try change this to:

var button = CopyImage.duplicate()
button.add_to_group("ImageObjects")
add_child(button)
button.reparent(new_parent)

Ok that worked, thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.