I am trying to save a score and coin amount from a game in python. The format looks like this:
{"score": score, "coins": coins}
where the variables score and coins represent a different number. I save this data by doing this:
with open(os.path.join(save_dir, 's.bin'), 'w') as save_file:
json.dump(data, save_file)
And the output is correct, as it is this (for example):
{"score": 34.060000000000166, "coins": 0}
in the file. But when the game is closed and reopened, the score is loaded in with this:
try:
with open(os.path.join(save_dir, 's.bin')) as save_file:
try:
print("attempting to load file...")
json.load(save_file)
return json.load(save_file)
except JSONDecodeError:
print("JSON can't decode...")
with open(os.path.join(save_dir, 's.bin'), 'w') as save_file_2:
json.dump(data_layout, save_file_2)
return data_layout
except FileNotFoundError:
print("file not found...")
with open(os.path.join(save_dir, 's.bin'), 'w') as save_file_3:
json.dump(data_layout, save_file_3)
return data_layout
Where data_layout
is: {“score”: 0, “coins”: 0}
.But the JSONDecodeError is always called on startup, so it always resets the score and coins on startup. It saves correctly, but it doesn’t load correctly. All help is appreciated!