Skip to content

Commit

Permalink
Only save if storage is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed May 7, 2019
1 parent b617ee0 commit d5c01bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Changelog
* New: screen.fill() now takes ``gcolor``, creating a vertical-linear gradient
* New: a :doc:`REPL <repl>` has been added, which allows exploring a game's
state while it is running.
* New: Added a :ref:`storage API <data-storage>`, which preserves data across
game runs.


1.2 - 2018-02-24
Expand Down
4 changes: 4 additions & 0 deletions pgzero/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Storage(dict):

def __init__(self, filename=None):
super().__init__()
self.loaded = False
self._save_file = filename
self.storages.append(self)

Expand Down Expand Up @@ -113,9 +114,12 @@ def load(self):
)
else:
self.update(data)
self.loaded = True

def save(self):
"""Save data to disk."""
if not self and not self.loaded:
return
try:
data = json.dumps(self)
except TypeError:
Expand Down

0 comments on commit d5c01bd

Please sign in to comment.