Issue with loading levels [solved]
Posted: Fri Jun 22, 2018 6:24 pm
so I made a small level editor which I've been working on for quite some time, it does everything you'd expect one to do, lets you save/load has commands to edit things, etc. However, when trying to make a demo for my game so others could play it, I ran into an issue, when exporting it into a zip and then renaming it a .love, when I launched it, it didn't load a level like it normally does, aka it couldn't find the level. I save/load levels by using the lua file functions with tserial (lets me save tables so I don't have to manually save everything), I then load it by opening the file of levels, finding the level with the matching string/name and having it spawn all of the tiles how they were when I saved. it worked fine, however it seems that when in a .love file, it forgets how to load a level. is there a way I could save/load levels without having to have the levels file be external from the .love?
source for my save:
and source for my load:
source for my save:
Code: Select all
function Editor:savelevel(filename)
local drop = {}
if filename == nil then
filename = self.roomname
else
self.roomname = filename
end
filename = self.savePath.."Levels/"..filename..".lvl"
local tiles = {}
local doors = {}
for i=1,#Tiles do
if Tiles[i].args.door then
table.insert(doors,Tiles[i])
else
table.insert(tiles,Tiles[i])
end
end
local f = io.open(filename,"w")
local saveString = "{"..self.roomw..","..self.roomh.."}\n"..Tserial.pack(tiles,drop,false).."\n"..Tserial.pack(doors,drop,false)
f:write(filename,saveString)
f:close()
self.console_errtxt = "level \""..filename.."\" saved!"
end
Code: Select all
function Editor:loadlevel(filename)
filename = string.lower(filename)
self.roomname = filename
filename = self.savePath.."Levels/"..filename..".lvl"
local line = {}
if file_check(filename) then
self:clearlevel()
local f = io.open(filename)
for lines in f:lines() do
table.insert(line,lines)
end
f:close()
self.roomw,self.roomh = string.match(tostring(line[1]),'{(%d+),(%d+)}')
self.roomw,self.roomh = tonumber(self.roomw),tonumber(self.roomh)
Tiles = Tserial.unpack(tostring(line[2]),true)
if line[3] ~= nil then
local doors = Tserial.unpack(tostring(line[3]),true)
for d=1,#doors do
table.insert(Tiles,doors[d])
end
end
cam:setWorld(-32,-32,self.roomw+64,self.roomh+64)
self.console_errtxt = "level \""..filename.."\" loaded!"
for i,v in ipairs(Tiles) do
if v.args.playerSpawn then
player.spawnx = v.x+8
player.spawny = v.y+8
player.x = player.spawnx
player.y = player.spawny
end
end
else
self.console_errtxt = "level \""..filename.."\" doesn't exist!"
end
end