Godot Version 4.2.2
Why my code is not working?
Code:
extends Node
var save_path = “user://Save&Load./Save.save”
var NickName = Nickname.nickname
func _save():
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(Nickname.name)
func _load():
var file = FileAccess.open(save_path, FileAccess.READ)
NickName = file.get_var(save_path)
func _ready():
if FileAccess.file_exists(save_path):
if NickName != “Dev”:
get_tree().change_scene_to_file(“res://Menuuu/Menu.tscn”)
func _on_button_pressed():
_save()
get_tree().change_scene_to_file(“res://Menuuu/Menu”)
file.get_var(save_path)
this should be file.get_var()
as get_var does not require a parameter to get the value.
So opening the file fails
Change to
user://save_load/save.save
& Ampersands shouldn’t be used in file names. Also capital letters are not utilized in all operating systems and should be avoided in filenames.
Thanks, but this is not working
I think you have a syntax error. But it’s not really clear. Please correct the syntax, or share a picture of the path code.
It should look like this.
Line 11 doesn’t work, I checked through print
Unfortunately I don’t have line numbers.
file.store_var(Nickname.name)
It seems like fileaccess cannot create directories on its own.
func _create_folder():
DirAccess.make_dir_recursive(save_path.get_base_dir())
func _save():
if not DirAccess.dir_exists_absolute(save_path.get_base_dir()):
_create_folder()
...
In order to understand why file might be null you need to access the error code.
var file = FileAccess.open(...)
if file == null:
print(FileAccess.get_error())
The list of errors
Global scope constants and functions. Description: A list of global scope enumerated constants and built-in functions. This is all that resides in the globals, constants regarding error codes, keyc...