write to file

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

write to file

Post by pielago »

okay I have a question
when i do
love.filesystem.write('score.lua','score: ')
It doesn't write it to score.lua in side the folder already made where i have my main.lua pictures maps etc..
instead it makes a folder under user/roaming/love/ etc.. ( windows)
is it possible to write it to the one you made already?
i have try but nothing it keeps on making it where i don't want too or that's the right way?
and if that's the right way when i do the .exe will it work?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: write to file

Post by Plu »

It always writes to the save directory, which is indeed a different one from the game directory. This will continue to work when you make a .exe; Löve always writes to whatever save directory is said and will read from it as well if you try to open files.
User avatar
Daniel Eakins
Citizen
Posts: 99
Joined: Thu Jul 18, 2013 9:14 pm
Location: France

Re: write to file

Post by Daniel Eakins »

You can write elsewhere on the disk using Lua's native commands, but in that case you'd have to make sure that you pick a directory which will still work when the game is a .exe file (for example, using if-then statements).
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: write to file

Post by pielago »

okay let me try to understand.... regardless it will save it there right?
so when i make my (a.exe) my save score and save game will work as well? once the game is install in any other pc?
that doesnt have a love.exe install?

I just need to understand this 100% wiki its to difficult to get for beginners like me...
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: write to file

Post by pielago »

Can someone help me fix this save score i don't know what am i mising :( i still trying to learn the write file
if someone so kind to help me with this code or give me a better way to save the score ?

Code: Select all

function love.load()
	g=love.graphics
	k={}
	k.x=50
	k.y=100
	k.w=50
	k.h=50
	k.speed=200
	k.highscore=0
	k.sc=0
	
	u={}
	u.x=200
	u.y=100
	u.w=50
	u.h=50	
	Over_LAP=false	
	TT = false
	--save score
	file = love.filesystem.newFile("score.lua")	
	love.filesystem.write("score.lua", 'k.highscore\n='..k.highscore)
	
	hs={}
	for lines in love.filesystem.lines("score.lua") do
		table.insert(hs,lines)
	end
	k.highscore = hs[2]
end


function love.draw()
	g.setColor(255,255,255,255)
	g.rectangle('line',k.x,k.y,k.w,k.h)
	g.rectangle('line',u.x,u.y,u.w,u.h)
	
	g.print('COLLISION: '..k.highscore,10,10)
	g.print('scores: '..k.sc,10,25)
end


function love.update(dt)
	if love.keyboard.isDown("d") then
		k.x=k.x+k.speed*dt
	end
	if love.keyboard.isDown("a") then
		k.x=k.x-k.speed*dt
	end
	--collision--
	if  (k.y + k.h  >  u.y )  and ( k.y  < u.y + u.h )  and
		(k.x + k.w  >  u.x )  and ( k.x  < u.x + u.w )  then
		Over_LAP=true
	else
		Over_LAP=false
	end	
	
	if Over_LAP then
        if not TT then 
            k.highscore = k.highscore + 1 
            TT = true  
        end
    else
        TT = false  
    end	
	
	--save game
	if k.sc > tonumber (k.highscore)then
		k.sc =k.highscore
		love.filesystem.write("score.lua", 'k.highscore\n=\n'..k.highscore)
	end
end

function love.quit()
	love.event.quit()
	love.filesystem.write("score.lua", 'k.highscore\n=\n ' ..k.highscore)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests