Checkpoint does not load correctly in Godot 4

I am developing a 2D game in Godot 4, and I am trying to create a “save and load (checkpoint)” system. Each scene’s name follows a pattern like “world_01”, “world_02”, etc. The character starts in the “world_01” scene, and upon reaching the end, advances to “world_02”. The location where the player starts is the location of the checkpoint, meaning that after passing scene 01, closing the application, and returning, the player should start in scene 02 (world_02). The code is successfully saving the checkpoint/scene, and upon closing and reopening, it also seems to load the checkpoint/scene, but when starting the game, it does not open and returns the error:

Invalid get index ‘global_position’ (on base: ‘Nil’)

Message returned when saving:

Checkpoint saved: res://levels/world_02.tscn(331, 270)
--- Debugging process stopped ---

Message returned when closing and reopening the game:

Globals script ready
Checkpoint loaded: res://levels/world_02.tscn(331, 270)

Archives:

checkpoint.gd:

extends Area2D

var is_active = false

func _on_body_entered(body):
    if body.name == "player" and not is_active:
        activate_checkpoint()

func activate_checkpoint():
    Globals.current_checkpoint = self
    Globals.current_checkpoint_scene = get_tree().current_scene.scene_file_path
    is_active = true
    Globals.save_checkpoint()

globals.gd (global):

extends Node

var player = null
var current_checkpoint = null
var current_checkpoint_scene = ""
var checkpoint_save_path = "user://checkpoint.sav"

func _ready():
    print("Globals script ready")  # Adicione uma mensagem para depuração
    load_checkpoint_from_file()

func _exit_tree():
    print("Tree exiting, saving checkpoint")  # Adicione uma mensagem para depuração
    save_checkpoint()

func respawn_player():
    if current_checkpoint_position != null:
        player.position = current_checkpoint_position


func load_checkpoint():
    if load_checkpoint_from_file():
        return {
            "position": current_checkpoint.global_position,
            "scene": current_checkpoint_scene
        }
    else:
        return null

func save_checkpoint():
    if current_checkpoint != null:
        var file = FileAccess.open(checkpoint_save_path, FileAccess.WRITE)
        if file:
            file.store_pascal_string(current_checkpoint_scene)
            file.store_var(current_checkpoint.global_position)
            file.close()
            print("Checkpoint saved: ", current_checkpoint_scene, current_checkpoint.global_position)
        else:
            print("Error opening file for writing")
    else:
        print("No current checkpoint to save")

var current_checkpoint_position = null

func load_checkpoint_from_file() -> bool:
    if not FileAccess.file_exists(checkpoint_save_path):
        print("Checkpoint file does not exist")
        return false # file not found
    
    var file = FileAccess.open(checkpoint_save_path, FileAccess.READ)
    if file:
        current_checkpoint_scene = file.get_pascal_string()
        var pos := file.get_var() as Vector2
        file.close()
        
        current_checkpoint_position = pos
        print("Checkpoint loaded: ", current_checkpoint_scene, pos)
        return true # got all data!
    else:
        print("Error opening file for reading")
        return false # failure to load

title_screen.gd (main scene):

extends Control

func _ready():
    pass # Não é necessário implementar nada aqui

func _on_start_btn_pressed():
    var checkpoint_data = Globals.load_checkpoint()
    if checkpoint_data != null:
        Globals.player.position = checkpoint_data.position
        get_tree().change_scene_to_file(checkpoint_data.scene)
    else:
        get_tree().change_scene_to_file("res://levels/world_01.tscn")

func _on_new_btn_pressed():
    get_tree().change_scene_to_file("res://levels/world_01.tscn")

func _on_credits_btn_pressed():
    # Implemente conforme necessário
    pass

func _on_quit_btn_pressed():
    Globals.save_checkpoint()
    get_tree().quit()

world.gd (script base/general):

extends Node2D

@onready var player := $player as CharacterBody2D
@onready var player_scene = preload("res://levels/player.tscn")

@onready var camera := $camera as Camera2D
# Called when the node enters the scene tree for the first time.
func _ready():
    Globals.player = player
    Globals.player.follow_camera(camera)
    Globals.player.player_has_died.connect(reload_game)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    pass

func reload_game():
    await get_tree().create_timer(1.0).timeout
    var player = player_scene.instantiate()
    add_child(player)
    Globals.player = player
    Globals.player.follow_camera(camera)
    Globals.player.player_has_died.connect(reload_game)
    Globals.respawn_player()
    #get_tree().reload_current_scene()

The player begins in the first scene and not in the second scene where the checkpoint is located. What could be the reason for this?

New contributor

Neitan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật