The reason you are getting a unexpected token error is because the code you have for opening and loading data from a file needs to be in a function, not globally defined. You can put it in something like _ready
or make your own function and then call it when you need (I would suggest going this, as it makes it easier to refactor)
Something like this should work:
func load_highscores():
var f = File.new()
f.open(easy_high_path, File.READ)
easy_high_score = f.get_var()
f.close()
f.open(hard_high_path, File.READ)
hard_high_score = f.get_var()
f.close()
That should fix the issue.
@MungMoong said:
And I do not know when to call high scores.
For a infinite runner project I made I just saved scores before Godot closes, and load scores when Godot is first started. This seemed to work well enough most of the time.
Another option is to save high scores when a level is completed. That way you can always have the most up to date scores stored on file, and so long as you are not writing a ton of data, it shouldn't be too slow and cause any noticeable lag.
Ultimately it really depends on how your game is setup and how you want to set it up. There are lots of way to go about it, so I'd just find one that works for you and stick to it.
Also, if you did not create a file in that path, does the godot engine create the file yourself?
Godot will not make a file itself. You will need to create any files you need before you try to read them, otherwise it will not work.
Thankfully, creating files is really easy. I'd check out this page from the documentation on saving/loading files in Godot. It shows how to save and load data using JSON.
Looking at how you are loading scores, if you want to put data into the files so it can be loaded using the code above, then you will need something like this:
func save_highscores():
var f = File.new()
f.open(easy_high_path, File.WRITE)
f.store_var(easy_high_score)
f.close()
f.open(hard_high_path, File.WRITE)
f.store_var(hard_high_score)
f.close()
Hopefully this helps :smile: