#Save and loading the position of player in 2D game

3 messages · Page 1 of 1 (latest)

fiery pawn
#

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
    

#

and also in the main_menu.gd, the load_game() that was connected in continue button(continue pressed) is it was always disable the output in it.

extends Control
##@onready var player = get_node("/root/Main/Player")
@onready var about_us = $aboutUs
@onready var settings = $Settings
@onready var menu = $TextureRect/Menu
@onready var start = $TextureRect/Menu/NewGame
@onready var color_rect = $ColorRect
var username = ""
var music_bus_index
var SFX_bus_index
@export var initial_level = ""
#var maingame = preload("res://main.tscn")
func _on_continue_pressed():
    if Backbuttonsaveandload.current_level !="":
        if get_tree().change_scene_to_file(Backbuttonsaveandload.current_level) != OK:
            push_error("Error changing scenes")
            print("Error changing scenes")
        else:
             print("Changing scene success!!")   
    else:
        push_error("Error: current_level shouldn't be empty")
        print("Error: current_level shouldn't be empty")
    Backbuttonsaveandload.load_game()
    
    
func _ready():
    grab_focus()
    
    if Backbuttonsaveandload.load_game(true):
        $TextureRect/Menu/continue.disabled = false
        print("Button Enabled")
    else:
        $TextureRect/Menu/continue.disabled = true
        print("Button Disabled")
    # Assuming username is retrieved from a scene variable set in login.gd
    #username = get_node("%username")  # Replace "%username" with actual scene variable name
    # Set username scene variable
    #var username_var = get_node("username_var")  # Replace with actual scene variable node name
    #username_var.set_var("username", username)
    ```
carmine wyvern