Best way to create default high score file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nautic

Hopefully not a double post as when i did this before it just disappeared. Anyway, I have a high score file that i am currently doing a simple check for and reading it in if its found. What I’d like to do is to create it if it’s not found. Is it possible for me to include a default in my project and just extract it if need be, or do i have to manually create it and then file.store_line and write it myself?

Thanks All!!!

:bust_in_silhouette: Reply From: Ertain

If the file isn’t found, you should be able to create it from within your Godot game. So check for the file and, if it’s not found, use the File class to create it. If you haven’t seen the sample code to write to a file, it would go like this:

if not File.file_exists("user://path/to/highscore.txt"):
       save_highscore(highscore)

func save_highscore(content):
       var file = File.new()
       file.open("user://path/to/highscore.txt", File.WRITE)
       file.store_string(content)
       file.close()

I haven’t tested this code, but it should serve as a starting point. For further reading on why a file may not be created, check out Wombat Stampede’s answer to this question.

Note that is a file that’s created at runtime. For files that you included from the development of the game (e.g. assets and config file), use the command ResourceLoader.exists() instead.

Thank you. This is what I ended up doing and it works great.

Nautic | 2020-07-11 03:09