Reputation: 1329
I tried to set up a highscore tracker for an app by using a JSON file and the json storage language in Kivy.
I imported JSONstore and in my main game class I did
class Game(FloatLayout):
highscorejson = JsonStore('highscore.json')
highscore = NumericProperty(highscorejson.get('highscore')['best'])
and after I init the class, I have an end game function that ends the game and checks to see if the new score beats the old highscore.
def end_game(self):
if self.score > self.highscore:
self.highscorejson.put('highscore', best = self.score)
self.highscore = self.highscorejson.get('highscore')['best']
This runs perfectly when I run it through Kivy, but when I run it through XCode using my iphone as a test device, it crashes when you score above the highscore and the game ends. The error message is as below.
File "/usr/local/lib/python2.7/site-packages/kivy/storage/__init__.py", line 174, in put
File "/usr/local/lib/python2.7/site-packages/kivy/storage/jsonstore.py", line 39, in store_sync
IOError: [Errno 1] Operation not permitted: 'highscore.json'
2014-06-24 21:59:34.385 cookie[2320:60b] Application quit abnormally!
2014-06-24 21:59:34.457 cookie[2320:60b] Leaving
Full Error: http://pastebin.com/Zy0DtysW
Upvotes: 0
Views: 699
Reputation: 26
I was stuck on this problem too. So, finally, I was able to solve this problem. The next code helped me a lot.
from os.path import join
class MyApp(App):
def build(self):
data_dir = getattr(self, 'user_data_dir')
store = JsonStore(join(data_dir, 'storage_file.json'))
As I understand, user_data_dir
stores an unique for each app and OS path, where current app's code is stored.
Upvotes: 1
Reputation: 14794
You're probably trying to save the file in an invalid location. Try including a full path to the file you want to write out - you can use kivy_home_dir
to help with this.
from kivy import kivy_home_dir
from os.path import join
highscore = JsonStore(join(kivy_home_dir, 'highscore.json'))
Upvotes: 1