My save creator keeps erroring

Godot Version

4.4.1

Question

` I’m trying to make a save and it keeps going “Attempt to call store_string on null instance”. How do I… not have it do that? My code for the control is:
extends Control

func _on_font_pressed():
$ProgressBar.value = 6
$fontquest.hide()
$font.hide()
$fontyes.hide()
$SetupText2.hide()
$SetupText.show()
$tosquest.show()
$tos.show()
$tosyes.show()
$tosno.show()

func _on_tosyes_pressed():
$ProgressBar.value = 14
$tosquest.hide()
$tos.hide()
$tosyes.hide()
$tosno.hide()
$toswhy.hide()
$namequest.show()
$name.show()
$nameyes.show()

func on_name_pressed():
$ProgressBar.value = 26
global.username = $name.text
$namequest.hide()
$name.hide()
$nameyes.hide()
$passquest.show()
$pass.show()
$passyes.show()

func on_passyes_pressed():
$ProgressBar.value = 40
global.password = $pass.text
$passquest.hide()
$pass.hide()
$passyes.hide()
$payquest.show()
$pay.show()
$payyes.show()

func on_payyes_pressed():
$ProgressBar.value = 50
$payquest.hide()
$pay.hide()
$payyes.hide()
$funny1.show()
await get_tree().create_timer(1.7).timeout
$ProgressBar.value = 55
$funny1.hide()
$funny2.show()
await get_tree().create_timer(0.6).timeout
$ProgressBar.value = 67
$funny2.hide()
$funny3.show()
var usr1 = DirAccess.open(“user://”)
usr1.make_dir(“Glass 10”)
var usr = DirAccess.open(“user://Glass 10”)
global.userdat = FileAccess.open(“user://Glass 10/user_data.dat”, FileAccess.READ_WRITE)
global.userdat.store_string(global.username+“\n”+global.password+“\n”) # it errors here
usr.make_dir(“apps”)

func _on_tos_close_requested():
$toswhy.show()

func _process(_delta):
if “\n” in $name.text:
$name.text = $name.text.replace(“\n”, “”)
on_name_pressed()
elif “\n” in $pass.text:
$pass.text = $pass.text.replace(“\n”, “”)
on_passyes_pressed()
elif “\n” in $pay.text:
$pay.text = $pay.text.replace(“\n”, “”)
on_payyes_pressed()
var original_text = $pay.text
var filtered_text = “”
for char in original_text:
if char in “0123456789”:
filtered_text += char
$pay.text = filtered_text
and my global code is
extends Node

var username = “defaultname”
var password = “defaultpass”
var grade = “a”
var ran = “0”
var ads = “0”
var fight = “0”
var userdat = FileAccess.open(“user://Glass 10/user_data.dat”, FileAccess.READ_WRITE)
`

Posting your code in backticks makes it easier to read:

```gdscript
code
code
code
```

Looks like:

code
code
code
global.userdat = FileAccess.open(“user://Glass 10/user_data.dat”, FileAccess.READ_WRITE)
global.userdat.store_string(global.username+“\n”+global.password+“\n”) # it errors here

I imagine what’s happening is that FIleAccess.open() is returning null and so attempts to store_string() with the result are blowing up.

I’d advise something like:

    var usr1 = DirAccess.open(“user://”)

    if !usr1:
        print("Drat, couldn't open user dir: " + DirAccess.get_open_error())
        return

    var err = usr1.make_dir(“Glass 10”)

    if err != Ok:
        print("Curses, couldn't make dir: " + str(err))

    # You don't need to open the directory to read files, btw...

    var usr = DirAccess.open(“user://Glass 10”)

    if usr == null:
        print("Fiddlesticks, couldn't open Glass 10: " + DirAccess.get_open_error())
        return

    var path = “user://Glass 10/user_data.dat”

    global.userdat = FileAccess.open(path, FileAccess.READ_WRITE)

    if !global.userdat:
        print("Bother, couldn't open " + path + " -> " + FileAccess.get_open_error())
        return

    if !global.userdat.store_string(global.username+“\n”+global.password+“\n”):
        print("Vexing, couldn't store a string: " + str(global.userdat.get_error()))

In general, you need to check every filesystem access for errors. Even if you’re absolutely certain the code is bug-free, you can run into exceptional circumstances like “the computer is out of disk space” which mean that your filesystem request fails.

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