Trying to assign value of type 'Nil' to a variable of type 'int'

Code is not working how do I set the text from fileaccess variable?

Trying to assign value of type ‘Nil’ to a variable of type ‘int’.

func _on_play_pressed() -> void: 

	var _file = FileAccess.open("user://.Chucho_01", FileAccess.READ)
	var score: int = 0
	score = _file.get_var(score)
	$Value.text = str(Essencia)
	print(_slotito)

get_var() function does not require any argument,

just change it

score = _file.get_var(score)

to this:

score = _file.get_var()
1 Like

help

E 0:00:19:092 Menu3D.tscn::GDScript_jmkmv:52 @ _on_play_pressed(): Condition “len < 4” is true. Returning: ERR_INVALID_DATA
<C++ Fuente> core/io/marshalls.cpp:190 @ decode_variant()
Menu3D.tscn::GDScript_jmkmv:52 @ _on_play_pressed()

E 0:00:19:093 Menu3D.tscn::GDScript_jmkmv:52 @ _on_play_pressed(): Error when trying to encode Variant.
<Error C++> Condition “err != OK” is true. Returning: Variant()
<C++ Fuente> core/io/file_access.cpp:388 @ get_var()
Menu3D.tscn::GDScript_jmkmv:52 @ _on_play_pressed()

Trying to assign value of type ‘Nil’ to a variable of type ‘int’.

You aren’t doing any error checking there. More than one thing could be going wrong:

  • the file you’re trying to read could be missing, or inaccessable due to permissions problems – this could be as simple as the path being wrong or the name spelled incorrectly (potentially including capitalization on some platforms)
  • there might not be a variable in the file to read, so the file could be open and readable, but you’re trying to read something that’s not there
2 Likes

It seems like _file.get_var() is still returning null. Make sure the file .Chucho_01 actually exists in the user directory, and that both the file name and its contents are correct. Sometimes the file can get corrupted if it was saved incorrectly.

To check the file manually, go to:
AppData > Roaming > Godot > app_userdata > [your project name] in file manger

Or, a quicker way [recommend]: In Godot “Editor” tab, select “take screenshot”, it will open the project’s directory automatically where you can see if the saved file, .Chucho_01 exists and its data/value isnt empty .

1 Like

If the file contains more than just the int and the int isn’t stored at the beginning of the file, you’d also have to move the file cursor to the int’s position before you can read it.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.