0 votes

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!!!

asked Jul 9 in Engine by Nautic (21 points)
edited Jul 9 by Nautic

1 Answer

+1 vote
Best answer

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.

answered Jul 9 by Ertain (1,257 points)
selected Jul 11 by Nautic

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.