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