Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Avoiding parsing

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content