Hello and goodevening, can anyone help me on how to solve this problem? I've been using 2D game and i have a save and load Singleton (BackbuttosaveandLoad.gd), It was all okay, the save game is saved the global_position x and y, but the problem is when i click a continue button which is connected to the load_game is, resetting the position to a default, and it seems like it didnt retrieve the json file in it, here's the code
extends Node2D
const SAVE_PATH = "user://save_json.save"
var current_level = ""
func save_game():
var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
var save_dict = {}
# Save the spawnpoint value to the dictionary
save_dict.global_position = {}
save_dict.global_position.x = global_position.x
save_dict.global_position.y = global_position.y
save_dict.current_level = current_level
print("Saved file")
file.store_line(JSON.stringify(save_dict))
OS.shell_open(ProjectSettings.globalize_path("user://"))
file.close()
pass
func load_game(check_only=true):
if not FileAccess.file_exists(SAVE_PATH):
print("FIle not Found")
return false
else:
var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
var json = JSON.new()
json.parse(file.get_line())
var save_dict = json.get_data()
if typeof(save_dict) != TYPE_DICTIONARY:
# Return false if the data is not a dictionary
print("Dictionary Not Found")
return false
if not check_only:
print("restore data??")
_restore_data(save_dict)
func _restore_data(save_dict):
global_position.x = save_dict.global_position.x
global_position.y = save_dict.global_position.y
current_level = save_dict.current_level
print("Position: ", position.x, "%", position.y)
print("Position: ", position)
print("Current level: ", current_level)
pass