Added scraps.

This commit is contained in:
Logen Kain 2017-07-30 20:30:07 -07:00
parent b47de49830
commit 1d8777d42f

54
scraps/GameState.lua Normal file
View File

@ -0,0 +1,54 @@
GameState = {}
-- Supply the table then the name of the table in string format
function GameState.save(tbl, table_name)
file = io.open("save_game/savefile.lua", "w")
io.output(file)
print_table(tbl, table_name)
io.close(file)
end
function GameState.load()
dofile("save_game/savefile.lua")
end
function printf (s, ...)
return io.write(s:format(...))
end
function print_table(tbl, table_name, only_once)
if only_once == nil then
printf("%s = { ", table_name)
end
for key,value in pairs(tbl) do
if type(value) == "table" then
printf("%s = { ",key)
print_table(value, table_name, true)
printf("}, ")
end
if type (value) ~= "table" then
if type(value) == "string" then
printf("%s = \"%s\", ", key, value)
else
printf("%s = %d, ", key, value)
end
end
end
if only_once == nil then
printf("}")
end
end
--Counts contents of tables of tables as well
function tablelength(tbl, count)
local count = count or 0
for key, value in pairs(tbl) do
if type(value) == "table" then
count = tablelength(value, count)
end
count = count + 1
end
return count
end