Godot Version
4.4.1 stable
Question
i have a problem with loading my save file using fileaccess, well i did read godot docs and watch
some tutorials and i did come up with this code, and it works, but only with saving, as when i save my game then print room path it works and when i opened save file in notepad it showed correctly but when i exit and play again and load game it doesnt load, next_room_path eventually changes to room_2 path and it saves but it cant load. here is my code.
var next_room_path = "res://Scenes/Rooms/room_1.tscn"
var save_path = "user://file.lol2"
func save_room_to_file(next_room_path)
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_string(next_room_path)
func load_room_from_file():
var file = FileAccess.open(save_path, FileAccess.READ)
var next_room_path = file.get_as_text()
return next_room_path
func _process(delta: float) -> void:
if Input.is_action_just_pressed("save"):
save_room_to_file(next_room_path)
print("game saved")
if Input.is_action_just_pressed("load"):
load_room_from_file()
print("game loaded")
if Input.is_action_just_pressed("print_room"):
print("next room is " + next_room_path)
when i press the load key the output tab prints game loaded but when i press print room key its all wrong.
i also did another method
func save_room_to_file(next_room_path)
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(next_room_path)
func load_room_from_file():
var file = FileAccess.open(save_path, FileAccess.READ)
var next_room_path = file.get_var()
return next_room_path
help me guys i badly want to be able to save/load player progress
You’re calling load_room_from_file()
but not using the returned value from the function.
This should fix the issue:
if Input.is_action_just_pressed("load"):
next_room_path = load_room_from_file()
print("game loaded")
Alternatively, change the function itself to return nothing and set the variable directly:
func load_room_from_file():
var file = FileAccess.open(save_path, FileAccess.READ)
next_room_path = file.get_as_text()
2 Likes
@sixrobin is right, you may get a warning for “shadowing” your original variable. By using var
within the function you are making a new variable with the same name as next_room_path
, this new variable is edited then destroyed within the function, totally unused outside it.
var next_room_path = "res://Scenes/Rooms/room_1.tscn"
func load_room_from_file():
var file = FileAccess.open(save_path, FileAccess.READ)
# Creates a new next_room_path and deletes it when then function ends
var next_room_path = file.get_as_text()
return next_room_path
1 Like
thanks bud i solved it using first solution, idk why i did the return option i just saw it in the documentation and did it. i shouldve read about return option because i dont know what does it do.
1 Like
Actually, you’ve used this in your code before: when calling FileAccess.open(save_path, FileAccess.WRITE)
, you store the returned value of the function inside the file
variable. Your load_room_from_file()
function works the exact same way.
The tricky thing is, even though a function does return a value, it’s not mandatory to store it in a variable. Which is why, even though your function returns next_room_path
, you can call the function so that it does its stuff without caring of what it returns.
Anyway, glad I could help 