practice/love/mike/lib/GameState.lua

63 lines
1.5 KiB
Lua

GameState = {}
-- Supply the table then the name of the table in string format
function GameState.save(tbl)
saved_table = print_table(tbl)
return saved_table
end
function GameState.load(SAVE_FILE)
-- placeholder is the name of our placeholder variable
-- so we can do things like "actors = GameState.load"
love.filesystem.load(SAVE_FILE)() -- Load and run the file
return placeholder
end
function printf (s, ...)
return io.write(s:format(...))
end
function print_table(tbl, table_name, only_once)
if only_once == nil then
table_name = "placeholder"
printf("local %s = { ", table_name)
saved_table = table_name .. " = { "
end
for key,value in pairs(tbl) do
if type(value) == "table" then
printf("%s = { ",key)
saved_table = saved_table .. key .. " = { "
print_table(value, table_name, true)
printf("}, ")
saved_table = saved_table .. "}, "
end
if type (value) ~= "table" then
if type(value) == "string" then
printf("%s = \"%s\", ", key, value)
saved_table = saved_table .. key .. " = \"" .. value .. "\", "
else
printf("%s = %d, ", key, value)
saved_table = saved_table .. key .. " = " .. value .. ", "
end
end
end
if only_once == nil then
printf("}")
saved_table = saved_table .. "}"
end
return saved_table
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