[SOLVED] No file created and no errors

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
tjohnman
Prole
Posts: 16
Joined: Sat Aug 04, 2012 8:40 am

[SOLVED] No file created and no errors

Post by tjohnman » Mon Sep 30, 2013 9:50 am

EDIT: Turns out I had a non-related error in the data structure and I forgot to catch errors from the coroutine... :P

Hi there. I'm having some trouble saving data to the disk and I thought I may be approaching it the wrong way.
I have a function that is basically this:

Code: Select all

local data = [[This no more than 700 kb, written using string.char() concatenations and it includes zeroes]]
local f, e = love.filesystem.newFile("universe.bin")
if e or not f then error(e) end
if not f:open("w") then error("Could not open file for writing") end
if not f:write(data) then error("Could not write to file") end
f:close()
This code is called as a subroutine. It yields inside the loop that populates data.
The problem I have is that the code runs with no errors and I can't find universe.bin anywhere. I'm using Windows 7 64 bit and Löve 0.9.0. I have looked in %appdata% (Local and Roaming), the documents folder, the user folder and the source code folder. The fact that I'm getting no errors makes me think I did not understand what I read on the wiki. Is there anything obvious I'm missing? Is the coroutine a problem? Nightly Löve bug?

Thank you!

Full code involved:

Code: Select all

StateSaveUniverse = {}

function StateSaveUniverse.load(data)
	setmetatable(StateSaveUniverse, {__index = State})

	StateSaveUniverse.nextState = data or StateMenu
	StateSaveUniverse.coro = coroutine.create(StateSaveUniverse.doSave)
end

function StateSaveUniverse.update(dt)
	if coroutine.status(StateSaveUniverse.coro) == "suspended" then
		coroutine.resume(StateSaveUniverse.coro)
	elseif coroutine.status(StateSaveUniverse.coro) == "dead" then
		Game:ChangeState(StateSaveUniverse.nextState)
	end
end

function StateSaveUniverse.draw()
	love.graphics.setColor(255, 255, 255)
	love.graphics.printf("Saving game...", 0, love.graphics.getHeight()/4 - 12, love.graphics.getWidth()/2, "center")
end

function StateSaveUniverse.doSave()
	local data = ""
	for i,v in ipairs(StateGame.universe.sectors) do
		data = data.."S"
		for ii,vv in ipairs(v) do
			data = data.."s"
			local star = vv:serialize()
			data = data..string.char(star[1])
			data = data..string.char(star[2])
			data = data..string.char(star[3])
			for iii,vvv in ipairs(vv.planets) do
				data = data.."p"
				data = data..StateSaveUniverse.serializeShort(vvv[1])
				data = data..StateSaveUniverse.serializeShort(vvv[2])
				data = data..StateSaveUniverse.serializeShort(vvv[3])
				data = data..StateSaveUniverse.serializeShort(vvv[4])
				data = data..StateSaveUniverse.serializeFloat(vvv[5])
				data = data..StateSaveUniverse.serializeShort(vvv[6])
				data = data..StateSaveUniverse.serializeFloat(vvv[7])
				data = data..StateSaveUniverse.serializeFloat(vvv[8])
				data = data..StateSaveUniverse.serializeShort(vvv[9])
				data = data..StateSaveUniverse.serializeShort(vvv[10])
				for m=1,vvv[9] do
					data = data..StateSaveUniverse.serializeFloat(vvv[10+m])
				end
				for a=1,vvv[10] do
					data = data..StateSaveUniverse.serializeFloat(vvv[10+vvv[9]+a])
				end
			end
		end
	end

	local f, e = love.filesystem.newFile("universe.bin")
	if e or not f then error(e) end
	if not f:open("w") then error("Could not open file for writing") end
	if not f:write(data) then error("Could not write to file") end
	f:close()
end

function StateSaveUniverse.serializeShort(n)
	return string.char(math.floor((n+32767)/256))..string.char(n - math.floor((n+32767)/256))
end

function StateSaveUniverse.serializeFloat(n)
	return string.char(StateSaveUniverse.serializeShort(math.floor(n)))..string.char(StateSaveUniverse.serializeShort(math.floor(n*100000)))
end
Last edited by tjohnman on Mon Sep 30, 2013 10:04 am, edited 1 time in total.

User avatar
tjohnman
Prole
Posts: 16
Joined: Sat Aug 04, 2012 8:40 am

Re: No file created and no errors

Post by tjohnman » Mon Sep 30, 2013 10:00 am

OK, I have just realized I am not catching errors that may pop up within the coroutine... Be back in a minute.

EDIT: Yeah, that was the problem. The above code was riddled with both syntax and logic errors, as anyone who might have read it will have noticed... :crazy:

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 14 guests