Last active
August 9, 2018 21:53
-
-
Save tobiasvl/a5cb8b980e8275681231c392f097df82 to your computer and use it in GitHub Desktop.
Save data in LÖVE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- initialize a save file holding a `savedata` sequence | |
| function init_savedata() | |
| local info=love.filesystem.getInfo("savedata.lua", "file") | |
| if info then | |
| local data=love.filesystem.read("savedata.lua") | |
| savedata={love.data.unpack("f",data,1)} | |
| else | |
| local file=love.filesystem.newFile("savedata.lua") | |
| local ok,err=file:open("w") | |
| if not ok then | |
| error(err) | |
| end | |
| local data=love.data.pack("string","f",unpack(savedata)) | |
| ok,err=love.filesystem.write("savedata.lua", data) | |
| if not ok then | |
| error(err) | |
| end | |
| end | |
| end | |
| -- load a value from the save file (entails loading all values, as they are serialized) | |
| function load_value(index) | |
| local data=love.filesystem.read("savedata.lua") | |
| savedata={love.data.unpack("f",data,1)} | |
| return savedata[index] | |
| end | |
| -- save a value to the save file (entails saving all values, as they are serialized) | |
| function save_value(index, value) | |
| local old_value=savedata[index] | |
| savedata[index]=value | |
| local data=love.data.pack("string","f",unpack(savedata)) | |
| local ok,err=love.filesystem.write("savedata.lua", data) | |
| if not ok then | |
| savedata[index]=old_value | |
| end | |
| return ok | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment