Saving top points/highscore without TSearial

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
Charlie Gallie
Prole
Posts: 14
Joined: Sun Mar 18, 2018 2:53 pm

Saving top points/highscore without TSearial

Post by Charlie Gallie »

Hey, so I'd like to have my game automatically save my highscore into a file such as "data.sav" with a custom extension. I have already looked for other posts like this but they do not work in my circumstances.
Here's my code:

Code: Select all

if lost == false then
		if grow then
			love.graphics.setColor(0,90,45)
			text = "Larger"
		else
			love.graphics.setColor(0,40,145)
			text = "Smaller"
		end
		love.graphics.print(text,10,15)
	else
	
		--Shows the points when you die.
	    font = love.graphics.setNewFont("assets/STSYSTEM.ttf", 90)
		love.graphics.setColor(255,20,20)
		love.graphics.print("Your score was:",7,25)
		love.graphics.setColor(0,255,0)
		love.graphics.setNewFont("assets/ARBERKLEY.ttf", 90)
		love.graphics.print(points.. "!",7,45)
		font = love.graphics.setNewFont("assets/STSYSTEM.ttf", 38)
		love.graphics.setColor(4,142,255)
	end
Would there be a way of saving the variable "points" in a file and be able to load it back in and set the variable "points" to it without using TSearial? Thanks!

p.s. Please excuse my 'bodgy' coding, I'm sort of used to writing in java.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving top points/highscore without TSearial

Post by zorg »

Hi and welcome to the forums!

Assuming you have no qualms about the 'data.sav' being in your save folder:

Code: Select all

local file = love.filesystem.newFile("data.sav")
file:open("w")
file:write(points)
file:close()

-- ... ,and elsewhere:

local file = love.filesystem.newFile("data.sav")
file:open("r")
points = tonumber(file:read(points))
file:close()
Most of the code from the wiki article: http://love2d.org/wiki/love.filesystem.newFile

I did skip a few useful things, like error handling and assertions; :open may return false and an error message, for instance, if the game's identity isn't set in conf.lua or through a function.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Charlie Gallie
Prole
Posts: 14
Joined: Sun Mar 18, 2018 2:53 pm

Re: Saving top points/highscore without TSearial

Post by Charlie Gallie »

Hi, I'm incredibly sorry it took 11 months to get back to you, but this works amazingly and I appreciate your help.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving top points/highscore without TSearial

Post by zorg »

Hey, no worries :P
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Saving top points/highscore without TSearial

Post by ivan »

Zorg is correct, but it's easier to write:

Code: Select all

love.filesystem.write("data.sav", points)
...
points = love.filesystem.read("data.sav")
points = tonumber(points)
Post Reply

Who is online

Users browsing this forum: No registered users and 69 guests