Serialization/Deserialization and loading

Godot Version

4.2.1

Question

Hey everyone,

seeking help yet again. I have a random pet generator, that spits out pets with different stats. The issue I’m having is in saving/loading said pets. I generate a pet, save the game, generate a second pet and load the save file. Both pets appear on my list, despite me saving when there was only one pet available. I’ve set up some debugging processes and it’s telling me that when it’s saving/loading the data it doesn’t have a single pet in the array, but the issue is I can’t for the life of me figure out why or what’s wrong with the code. I’ve been going at it all morning, I could really use an insight from someone not banging their heads against the wall for it, haha.
If you need me to post more sections of code I can do that as well, but here is what I think is important in this situation:

func serialize_pets() -> Array:
	var serialized_pets = []
	for pet in pets:
		var pet_data = {
			"name": pet.name,
			"type": pet.type,
			"breed": pet.breed,
			"image": pet.image.resource_path,
			"beauty": pet.beauty,
			"energy": pet.energy,
			"intelligence": pet.intelligence
		}
		serialized_pets.append(pet_data)
	print("Serialized pets: ", serialized_pets)  # Debug print
	return serialized_pets

# Function to deserialize pets data
func deserialize_pets(serialized_pets: Array):
	for pet_data in serialized_pets:
		var pet = Animal.new()
		pet.name = pet_data["name"]
		pet.type = pet_data["type"]
		pet.breed = pet_data["breed"]
		pet.image = load(pet_data["image"])
		pet.beauty = pet_data["beauty"]
		pet.energy = pet_data["energy"]
		pet.intelligence = pet_data["intelligence"]
		pets.append(pet)
		emit_signal("pet_generated", pet)
	print("Deserialized pets: ", pets)
func save_game(slot: int):
	var file_path = "user://savegame_%d.save" % slot
	print("Saving game to file: ", file_path)
	var save_game = FileAccess.open(file_path, FileAccess.WRITE)

	# Serialize the pets array
	var serialized_pets = pet_generator.serialize_pets()

	var save_data = {
		"pets": serialized_pets,
		"hours": hours,
		"minutes": minutes,
		"date": GlobalResources.date,
	}
	
	print("Save data: ", save_data)  # Debug print
	
	var json_string = JSON.stringify(save_data)
	save_game.store_line(json_string)
	save_game.close()
	print("Game saved successfully to slot %d." % slot)

func load_game(slot: int):
	var file_path = "user://savegame_%d.save" % slot
	print("Loading game from file: ", file_path)
	if not FileAccess.file_exists(file_path):
		print("No save file found in slot %d." % slot)
		return
	
	var save_game = FileAccess.open(file_path, FileAccess.READ)
	var json_string = save_game.get_line()
	save_game.close()
	
	var json = JSON.new()
	var parse_result = json.parse(json_string)
	if parse_result != OK:
		print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
		return
	
	var save_data = json.get_data()
	print("Loaded save data: ", save_data)  # Debug print
	
	if save_data.has("pets"):
		pet_generator.deserialize_pets(save_data["pets"])
	else:
		print("Save data does not contain pets.")
	
	if save_data.has("hours"):
		hours = save_data["hours"]
	else:
		print("Save data does not contain hours.")
	
	if save_data.has("minutes"):
		minutes = save_data["minutes"]
	else:
		print("Save data does not contain minutes.")
	
	if save_data.has("date"):
		GlobalResources.date = save_data["date"]
	else:
		print("Save data does not contain date.")
	
	update_time_label()
	pet_generator.display_pet_list()  # Update the display with the loaded pets
	print("Game loaded successfully from slot %d." % slot)

And the debug text after doing these steps: 1. Generate a pet (one is already pre-generated), 2. Save in slot 1. 3. Generate another pet. 4. Load slot 1.

Godot Engine v4.2.1.stable.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.277 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon RX 6600
 
Loaded animal type: Images with breeds: { "Dogs": <Resource#-9223372001656175407> }
Loaded animal type: Scenes with breeds: {  }
Loaded animal type: Scripts with breeds: {  }
Loaded animal type: Images with breeds: { "Dogs": <Resource#-9223372001505180238> }
Loaded animal type: Scenes with breeds: {  }
Loaded animal type: Scripts with breeds: {  }
Generated pet: Ali
Current pets array: [<Resource#-9223372001370962709>]
New pet generated: Ali
Displaying info for pet: Ali
Updating save slots...
Found file: logs
Found file: savegame.save
Found file: savegame_1.save
Found file: savegame_2.save
Found file: savegame_3.save
Found file: savegame_4.save
Found file: save_game.dat
Found file: shader_cache
Found file: vulkan
Save files: ["savegame_1.save", "savegame_2.save", "savegame_3.save", "savegame_4.save"]
Slot 1 - Saved
Slot 2 - Saved
Slot 3 - Saved
Slot 4 - Saved
Slot 5 - Empty
Loaded animal type: Images with breeds: { "Dogs": <Resource#-9223371959847352426> }
Loaded animal type: Scenes with breeds: {  }
Loaded animal type: Scripts with breeds: {  }
Generated pet: Mai
Current pets array: [<Resource#-9223372001370962709>, <Resource#-9223371959713135368>]
New pet generated: Mai
Displaying info for pet: Mai
Save button pressed for slot 1
Saving game to file: user://savegame_1.save
**Serialized pets: []**
**Save data: { "pets": [], "hours": 0, "minutes": 5, "date": "1.1.2024"" }**
Game saved successfully to slot 1.
Updating save slots...
Found file: logs
Found file: savegame.save
Found file: savegame_1.save
Found file: savegame_2.save
Found file: savegame_3.save
Found file: savegame_4.save
Found file: save_game.dat
Found file: shader_cache
Found file: vulkan
Save files: ["savegame_1.save", "savegame_2.save", "savegame_3.save", "savegame_4.save"]
Slot 1 - Saved
Slot 2 - Saved
Slot 3 - Saved
Slot 4 - Saved
Slot 5 - Empty
Loaded animal type: Images with breeds: { "Dogs": <Resource#-9223371928692062371> }
Loaded animal type: Scenes with breeds: {  }
Loaded animal type: Scripts with breeds: {  }
Generated pet: Kai
Current pets array: [<Resource#-9223372001370962709>, <Resource#-9223371959713135368>, <Resource#-9223371928557844624>]
New pet generated: Kai
Displaying info for pet: Kai
Load button pressed for slot 1
Loading game from file: user://savegame_1.save
**Loaded save data: { "date": "1.1.2024", "hours": 0, "minutes": 5, "pets": [ ] }**
**Deserialized pets: [ ]**
Game loaded successfully from slot 1.
Updating save slots...
Found file: logs
Found file: savegame.save
Found file: savegame_1.save
Found file: savegame_2.save
Found file: savegame_3.save
Found file: savegame_4.save
Found file: save_game.dat
Found file: shader_cache
Found file: vulkan
Save files: ["savegame_1.save", "savegame_2.save", "savegame_3.save", "savegame_4.save"]
Slot 1 - Saved
Slot 2 - Saved
Slot 3 - Saved
Slot 4 - Saved
Slot 5 - Empty
--- Debugging process stopped ---