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

in Engine by (21 points)
edited by

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"):

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

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.

by (2,064 points)
selected by

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.