Need help making a save file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
MikeCraftian
Prole
Posts: 2
Joined: Sat Feb 22, 2014 9:19 am

Re: Need help making a save file

Post by MikeCraftian »

Hexenhammer wrote:One of the things I love about Lua(JIT) is that I do not have to have the separation of core engine (e.g. C or C++), scripting (e.g. Lua or Python), and serialized data (e.g. XML, JSON, something custom) anymore. I used to develop in that traditional way, and it was so ugly. Translating between the different "worlds" adds so much unnecessary complexity and code. LuaJIT is fast enough that I can do everything in Lua... including save files.

You could serialize everything to Lua tables but I do not like that. Instead my save files are simply code which rebuilds the world state. My situation is more complex, I need to restore an entire game world, which means my saved game code is mostly calls to functions which do things like creating actors, setting their attributes etc. And of course I use locals. I also use delta encoding to keep the save files small without compression. However ignore all that stuff. If you just want to save some values in a simple way using that basic method, here is how you can do it:

Code: Select all

var = {}
var.number = 0 -- Initial value

var.number = 43 -- The value when the user pressed "save" (or whatever)

-- Saving said value to a file
file = io.open("savedGame.lua", "w")
file:write("var.number = ", var.number)
file:close()

-- Resetting to initial value, simulating the effect of a restart
var.number = 0

-- Loading the saved value from a file
dofile("savedGame.lua") -- Yes, that's it!

print(var.number) -- Will print 43!
Ive been on vacation now looking at this its perfect <3333 :cry:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests