Getting an error when calling get_var() in saving test

Godot Version

4.2x

Question

Im getting an error when trying to call the function get_var(). I have no idea why this is happening. im looking for someone who can help me fix that error "


"
I got the code fortutorial link

Blockquote

the code I’m currently using

extends Node2D

var save_path = “user//:variable.save”

var var1 = 0
var var2 = 0
var var3 = 0

var t1
var t2
var t3

Called when the node enters the scene tree for the first time.

func _ready():
#node path to labal
t1 = $‘c0/z’
t2 = $‘c1/o’
t3 = $‘c2/t’

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
t1.text = str(var1)
t2.text = str(var2)
t3.text = str(var3)

func _on_save_pressed():
save()

func _on_load_pressed():
load_data()

func _on_c_0_plus_pressed():
var1 += 1
func _on_c_0_minus_pressed():
var1 -= 1
func _on_c_1_plus_pressed():
var2 += 1
func _on_c_1_minus_pressed():
var2 -= 1
func _on_c_2_plus_pressed():
var3 += 1
func _on_c_2_minus_pressed():
var3 -= 1

func save():
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(var1)
file.store_var(var2)
file.store_var(var3)

func load_data():
if FileAccess.file_exists(save_path):
var file = FileAccess.open(save_path, FileAccess.READ)
var1 = file.get_var(var1)
var2 = file.get_var(var2)
var3 = file.get_var(var3)
else:
print(“nodatasaved”)
var1 = 0
var2 = 0
var3 = 0

It looks like FileAccess.open returned null.

I would delete the variable.save and save a new one. See if the issue continues.

If it does get_open_error will tell you why.

1 Like

what how do i use get_open_error
is it get_open_error()?

1 Like

Your path should be “user://variable.save”

To detect file errors use FileAccess.get_open_error()

var file := FileAccess.open(save_path, FileAccess.WRITE)
if file == null:
    print("Couldn't save because: ", FileAccess.get_open_error())
    return

file.store_var(var1)
file.store_var(var2)
file.store_var(var3)
2 Likes

@gertkeno good catch. Although i thought this would catch that?

if FileAccess.file_exists(save_path):
 ...

@sliverofstraw0_0

Since the documentation says it’s a static function you can call it like this, and get the error.

var error = FileAccess.get_open_error()

if error != OK:
	printerr("Failure! ", str(error))

Link to a list of error numbers and descriptions.

1 Like

The actual error they are recieving in the screenshot is null on store_var, I assume the main post is a little ahead of itself, the file won’t be created to be loaded eitherway.

@gertkeno , Right, but that would be because the file path was wrong, and the file didn’t exist and wasn’t opened? Or is it the fault of the : character being interpreted/filtered by Godot or the operating system?

If saving the file makes file exist return true why can’t open work?

The file path is the same wrong path for both saving and loading, it’s an impossible location on Windows (disallows : characters), and neither a res:// or user:// path so I think Godot doesn’t allow it without some special flags.

Since the file path is wrong no file is ever created, file is null, trying to use store_var on null produces this error

When loading, this line probably works as intended. The file path is illegal thus it cannot exist, so the rest of the load_data function loads zeros (assuming correct indentation)

if FileAccess.file_exists(save_path):
1 Like

Oh I’m dumb it is store var. Not get var. That makes more sense.

thank you so much

I got this error (number 12)
Error ERR_FILE_CANT_OPEN = 12
File: Can’t open error.

Did you see this part of my post?

tysm its working.
I think I skipped that part sorry

1 Like