Mike: Save/Load should work now; TODO updated

This commit is contained in:
Logen Kain 2017-07-30 20:33:43 -07:00
parent 1d8777d42f
commit d75e61bba9
3 changed files with 21 additions and 10 deletions

View File

@ -9,4 +9,7 @@ Either find or write a better way to save table information, tserial fails when
Also, possibly find a way to use love.filesystem.write() instead of just doing it myself Also, possibly find a way to use love.filesystem.write() instead of just doing it myself
Game crashes if I try to save without running it from the dir code: "love ." But love "mike" will crash Game crashes if I try to save without running it from the dir code: "love ." But love "mike" will crash
-- Fixed, but the source code could use some cleanup.
-- I'd also like to make it so I can do "actors = GameState.load(SAVE_FILE)" instead of having the variable (actors) in the save file
-- Should be doable with love.filesystem.read(SAVE_FILE), just need to stop adding in the "VARIABLE =" at the beginning of my save file
-- Also remove the need to put in a string at all this way.

View File

@ -2,15 +2,15 @@ GameState = {}
-- Supply the table then the name of the table in string format -- Supply the table then the name of the table in string format
function GameState.save(tbl, table_name) function GameState.save(tbl, table_name)
file = io.open("save_game/savefile.lua", "w") saved_table = print_table(tbl, table_name)
io.output(file) return saved_table
print_table(tbl, table_name)
io.close(file)
end end
function GameState.load() function GameState.load(SAVE_FILE)
dofile("save_game/savefile.lua") -- Pretty sure doing this slash will break it on winblows
dofile(love.filesystem.getSaveDirectory().. "/" .. SAVE_FILE)
end end
function printf (s, ...) function printf (s, ...)
@ -20,24 +20,31 @@ end
function print_table(tbl, table_name, only_once) function print_table(tbl, table_name, only_once)
if only_once == nil then if only_once == nil then
printf("%s = { ", table_name) printf("%s = { ", table_name)
saved_table = table_name .. " = { "
end end
for key,value in pairs(tbl) do for key,value in pairs(tbl) do
if type(value) == "table" then if type(value) == "table" then
printf("%s = { ",key) printf("%s = { ",key)
saved_table = saved_table .. key .. " = { "
print_table(value, table_name, true) print_table(value, table_name, true)
printf("}, ") printf("}, ")
saved_table = saved_table .. "}, "
end end
if type (value) ~= "table" then if type (value) ~= "table" then
if type(value) == "string" then if type(value) == "string" then
printf("%s = \"%s\", ", key, value) printf("%s = \"%s\", ", key, value)
saved_table = saved_table .. key .. " = \"" .. value .. "\", "
else else
printf("%s = %d, ", key, value) printf("%s = %d, ", key, value)
saved_table = saved_table .. key .. " = " .. value .. ", "
end end
end end
end end
if only_once == nil then if only_once == nil then
printf("}") printf("}")
saved_table = saved_table .. "}"
end end
return saved_table
end end
--Counts contents of tables of tables as well --Counts contents of tables of tables as well

View File

@ -6,6 +6,7 @@ LOAD_KEY = "l"
HALF_SPEED = "lshift" HALF_SPEED = "lshift"
MOVE_RIGHT = "right" MOVE_RIGHT = "right"
MOVE_LEFT = "left" MOVE_LEFT = "left"
SAVE_FILE = "testing.lua"
function love.keyreleased(key) function love.keyreleased(key)
if key == QUIT_KEY then if key == QUIT_KEY then
@ -14,16 +15,16 @@ function love.keyreleased(key)
-- save -- save
elseif key == SAVE_KEY then elseif key == SAVE_KEY then
--love.filesystem.write(saveFile, Tserial.pack(player, false, true)) --love.filesystem.write(saveFile, Tserial.pack(player, false, true))
GameState.save(actors, "actors") love.filesystem.write(SAVE_FILE, GameState.save(actors, "actors"))
-- Load -- Load
elseif key == LOAD_KEY then elseif key == LOAD_KEY then
save_exists = love.filesystem.exists("save_game/savefile.lua") save_exists = love.filesystem.exists(SAVE_FILE)
if save_exists then if save_exists then
--player = Tserial.unpack( love.filesystem.read( saveFile ) ) --player = Tserial.unpack( love.filesystem.read( saveFile ) )
GameState.load() GameState.load(SAVE_FILE)
end end
end end
end end