Godot Version
4.4.1.
Question
`I made a save system trough a tutorial, sadly the tutorial didn’t show how to save and load the scene, added to the player position. A nice person helped me get the scene saving and loading to work, but that made the loading of the global position not work anymore.
I was told that it’s probably because everything get’s loaded before the scene is ready, and it can’t load the global position of a node that isn’t ready yet. I was also told there are mistakes in the rest of my code, and I tried to fix them a bit, but didn’t know how to do that.
So my question is, how would I make my save system wait, after loading the scene, and before loading the global position. I’m a complete noob so explaining it in a simple way would be very kind.
I added my save and load script here
func save():
print(get_path())
var file = FileAccess.open("user://savegame.json",FileAccess.WRITE)
var saved_data = {}
saved_data["scene_file_path"] = get_tree().get_current_scene().scene_file_path
saved_data["player_global_position:x"] = player.global_position.x
saved_data["player_global_position:y"] = player.global_position.y
saved_data["Zombie_Health_1"] = Globals.ZombieHealth1
var json = JSON.stringify(saved_data)
match FileAccess.get_open_error():
OK:
file.store_string(json)
file.close()
print("Save successful.")
ERR_CANT_CREATE:
print("Error: Cannot create the save file.")
ERR_CANT_OPEN:
print("Error: Cannot open the save file.")
ERR_FILE_NOT_FOUND:
print("Error: Save file not found, and could not be created.")
_:
print("An unknown error occurred while opening the save file.")
func load_game():
var file = FileAccess.open(“user://savegame.json”, FileAccess.READ)
match FileAccess.get_open_error():
OK:
var json = file.get_as_text()
file.close()
var saved_data = JSON.parse_string(json)
if typeof(saved_data) == TYPE_DICTIONARY:
if saved_data.has("scene_file_path"):
get_tree().change_scene_to_file(saved_data["scene_file_path"])
print ("got scene")
else:
print ("Scene not found")
if saved_data.has("player_global_position:x") and saved_data.has("player_global_position:y"):
player.global_position.x = saved_data["player_global_position:x"]
player.global_position.y = saved_data["player_global_position:y"]
print("Loaded player position: ", player.global_position)
else:
print("Position data not found in save file.")
if saved_data.has("Zombie_Health_1"):
Globals.ZombieHealth1 = saved_data["Zombie_Health_1"]
print("Loaded ZombieHealth1: ", Globals.ZombieHealth1)
else:
print("Zombie_Health_1 not found in save file.")
else:
print("Error parsing save file JSON.")
ERR_FILE_NOT_FOUND:
print("Save file not found.")
_:
print("Error opening save file: ", FileAccess.get_open_error())
`