Safe save system

Godot Version 4.3

I’m about to release a game with a score saving system, is this a safe way of doing it, with no potential problems?

extends Node

const SAVEFILE = "user://savefile.save"

static var high_score = 0

func _ready():
	load_highscore()

func save_highscore():
	var file = FileAccess.open(SAVEFILE, FileAccess.WRITE_READ)
	file.store_32(high_score)
	file = null

func load_highscore():
	var file = FileAccess.open(SAVEFILE, FileAccess.READ)
	if FileAccess.file_exists(SAVEFILE):
		high_score = file.get_32()

It looks functional enough but there are some potential issues.

Just looking at the save_highscore function for instance: Your code doesn’t check if the file was successfully opened. It’s a good practice to add error handling. While setting file = null will work, it’s better to be more explicit with file.close(). The FileAccess.WRITE_READ mode is used, but if you’re only writing, FileAccess.WRITE could be more appropriate.

func save_highscore():
    var file = FileAccess.open(SAVEFILE, FileAccess.WRITE)
    if file:
        file.store_32(high_score)
        file.close()
    else:
        print("Failed to open file for writing: ", SAVEFILE)

Hope that helps.

PS It is also a good idea to check the file exists before trying to open it too. Something like:

func save_highscore():
    if FileAccess.file_exists(SAVEFILE):
        var file = FileAccess.open(SAVEFILE, FileAccess.WRITE)
        if file:
            file.store_32(high_score)
            file.close()
        else:
            # Handle failed opening however you want
            print("Failed to open file for writing: ", SAVEFILE)
    else:
        # Handle file not found however you want too
        print("File does not exist: ", SAVEFILE)

What potential errors could pop up without this?

You program will throw an error and stop working? I would need to check the docs to be precise though.

Well actually it seems that if the file is not found in WRITE_READ mode a file is created for you anyway. So I suppose your code is perfectly functional but to me it feels like bad practice. But in reality it would work fine. I wonder what would happen if it failed to write when trying to correct automatically for your missing file?

Edit:
I believe it would throw a runtime error in that circumstance. So potentially without this and in the circumstance that the users device could not create a file then your program would crash. The file variable would be null and the store_32 would be a fatal error.

1 Like

Alright thanks, I’m a little nervous to change anything now, and I haven’t had any problems (yet), but if something comes up I might have to change it. I just wanted to make sure nobody could like hack the program or something, because I think I heard that could happen with some save systems.

1 Like

Oh yes you can. You access the file and change the value externally of course. It is not encrypted and although you might need a hex editor, it is not ‘secure’. Not that anyone would go to the effort for just a local game high score (presuming that is what you are using it for).

The only circumstance I can reasonably see your code having issues is if the users system memory is full! This is highly unlikely TBH.

PS In my own game, I have the file checks but generally I just ignore it if it does not save. The user would be upset coming back later to see their game didn’t actually save, but TBH and like you, I have never had this problem reported.

But it’s not going to compromise the security of my computer or information outside the game?

1 Like

No, not at all. This has no security issues whatsoever as far as I understand it.

1 Like