diff --git a/love/mike/TODO b/love/mike/TODO index e9de775..75bf2b0 100644 --- a/love/mike/TODO +++ b/love/mike/TODO @@ -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 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. diff --git a/love/mike/lib/GameState.lua b/love/mike/lib/GameState.lua index bf0670b..3ebc8e1 100644 --- a/love/mike/lib/GameState.lua +++ b/love/mike/lib/GameState.lua @@ -2,15 +2,15 @@ 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) + saved_table = print_table(tbl, table_name) + return saved_table + end -function GameState.load() - dofile("save_game/savefile.lua") +function GameState.load(SAVE_FILE) + -- Pretty sure doing this slash will break it on winblows + dofile(love.filesystem.getSaveDirectory().. "/" .. SAVE_FILE) end function printf (s, ...) @@ -20,24 +20,31 @@ end function print_table(tbl, table_name, only_once) if only_once == nil then printf("%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 diff --git a/love/mike/lib/keys.lua b/love/mike/lib/keys.lua index aee7342..6c36421 100644 --- a/love/mike/lib/keys.lua +++ b/love/mike/lib/keys.lua @@ -6,6 +6,7 @@ LOAD_KEY = "l" HALF_SPEED = "lshift" MOVE_RIGHT = "right" MOVE_LEFT = "left" +SAVE_FILE = "testing.lua" function love.keyreleased(key) if key == QUIT_KEY then @@ -14,16 +15,16 @@ function love.keyreleased(key) -- save elseif key == SAVE_KEY then --love.filesystem.write(saveFile, Tserial.pack(player, false, true)) - GameState.save(actors, "actors") + love.filesystem.write(SAVE_FILE, GameState.save(actors, "actors")) -- Load 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 --player = Tserial.unpack( love.filesystem.read( saveFile ) ) - GameState.load() + GameState.load(SAVE_FILE) end end end