Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | samjmiller |
Hi friends!
My game saves previous scores to a JSON file, and upon user request provides a read-out of recent scores.
On my computer, it does this perfectly. When I export to Android and load it on my phone, the game works fine EXCEPT it does not save any new scores. I know that it is correctly accessing the two JSON files (one of which saves each new day the user plays, the other scores each individual score for each day) because it can pull data from both of them to display the recent scores that were already saved in the JSON files, but it is not saving new scores.
Here’s the code I use for reading FROM the two JSON files:
var file = File.new()
file.open("res://files/scores.json", File.READ)
logged_times = parse_json(file.get_as_text())
file.close()
var file2 = File.new()
file2.open("res://files/scores_DAYS.json", File.READ)
days = parse_json(file2.get_as_text())
file.close()
And here’s how I write TO the files:
var file = File.new()
file.open("res://files/scores.json", File.WRITE)
file.store_line(JSON.print(logged_times))
file.close()
file.open("res://files/scores_DAYS.json", File.WRITE)
file.store_line(JSON.print(days))
file.close()
Any thoughts on what might be happening here? Happy to provide additional code or context if needed.
As always, I so appreciate this community!
File.open()
returns an error code that will most probably give you the exact reason it’s not working.
zhyrin | 2023-03-22 17:33
Oh! Okay. Now I’m going to sound stupid. How do I find out what the error code is?
Thank you!!
samjmiller | 2023-03-22 17:56
If you follow the documentation of File.open()
, you’ll see that the error code is of an Error
enum defined in global scope.
@GlobalScope — Godot Engine (3.5) documentation in English
zhyrin | 2023-03-22 19:19
If I print…
print(file.open("res://files/scores.json", File.READ))
…I get a 0. Which from the documentation you linked to, looks like it means “OK” or “No error occurred.” And then if I print
print(file)
I get [_File:1517]
, which makes me think that it is finding the file. But when I try to access the data in it with logged_times = parse_json(file.get_as_text())
, logged_times
comes up Null
.
samjmiller | 2023-03-22 20:18