July 2018
Beginner
202 pages
5h 42m
English
Being able to simply load Lua tables is a powerful mechanism. If you format save data as a valid Lua file, it can be loaded back as a module. Using this feature of Lua, you can avoid having to write code that parses a text file.
Let's explore this concept by first saving some data to disk. The following function saves a simple object containing a level, health, and number of lives to a file named save.lua:
function Save(level, health, lives) file = io.open("save.lua", "w") file:write("return { \n") file:write("\tlevel = ", level, ",", "\n") file:write("\thealth = ", health, ",", "\n") file:write("\tlives = ", lives, "\n") file:write("}") file:close()endSave(20, 10, 2)
Because save.lua is a module, you can load the save data ...