It looks like you're new here. If you want to get involved, click one of these buttons!
curLevel = 2 hp = 10 function save() local file = io.open("|D|save.bin", "w+b") file:write(curLevel) file:write(hp) file:close() end function load() local file = io.open("|D|save.bin", "rb") curLevel = file:read("*n") hp = file:read("*n") file:close() end The problem is when reading i obtain curLevel==210, hp==nil instead of expected curLevel==2, hp==10 i also examined the resulting file (on Windows) and discovered it is saved as text instead of binary as i specified, i suppose the first problem is caused by the second one, any suggestion about how to fix it? |
Comments
Instead, I can suggest implementing your save/load of your game as Lua tables:
1. Download table.save-0.94.lua from http://lua-users.org/wiki/SaveTableToFile and add it your project.
2. Create a table about your progress and save it:
I tried table.save, work fine, for now i will use it.
for the future, if needed, i consider to implement a more compact text mode save.
(now that i understand why my attempt not worked i am confident to be capable of doing it right)
Just for information: table.load function miss a "local tables,err" at begin, to be usable with strict.lua
Edit: I've committed table.save to https://github.com/gideros/table.save and make variables local at table.load after your suggestion.