[Solved]Loading variables isn't working with filesystem

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
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

[Solved]Loading variables isn't working with filesystem

Post by BoopDeePoop »

I'm trying to save a highscore variable using love.filesystem and when I do this, I can save the file and the information inside it. I just can't figure out how to load the data properly?

This is the error I get:

Code: Select all

saving.lua:6: Syntax error: save.lua:1: '<name>' expected near '0'
This gets written into the save.lua file my game creates:

Code: Select all

highscore: 0
This function is in love.load()

Code: Select all

function save()
	saveisthere = love.filesystem.exists("save.lua")
	if not saveisthere then
		savefile = love.filesystem.newFile("save.lua")
		love.filesystem.newFile("save.lua")
		print("File Creation Successful!")
		highscore = 0
		print("Data Cleared.")
		savefile:open("w")
		savefile:write("highscore: " ..highscore)
		savefile:close()
		print("New Data Saved.")
	end
	if saving then
		savefile = love.filesystem.newFile("save.lua")
		savefile:open("w")
		savefile:write("highscore: " ..highscore)
		savefile:close()
		print("New Data Saved.")
	end
end
I then have this function called when I click the 'highscores' button on my main menu

Code: Select all

function load()
	saveisthere = love.filesystem.exists("save.lua")
	if saveisthere then
		love.filesystem.load("save.lua")()
	end
	if not saveisthere then
		print("No Save File Detected.")
	end
end
I'm not really too sure what the problem seems to be.
Last edited by BoopDeePoop on Mon Jul 21, 2014 9:47 pm, edited 1 time in total.
Joemag
Prole
Posts: 24
Joined: Sun Apr 14, 2013 5:42 pm

Re: Loading variables isn't working with filesystem

Post by Joemag »

Just replace ":" with "=".
It should be saved as:

Code: Select all

highscore=0
.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Loading variables isn't working with filesystem

Post by BoopDeePoop »

Joemag wrote:Just replace ":" with "=".
It should be saved as:

Code: Select all

highscore=0
.
That shouldn't change anything should it? I was printing a string to the file with a variable?

edit:
wtf it worked. why don't colons work?
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Loading variables isn't working with filesystem

Post by DaedalusYoung »

Because colons is not proper syntax to define variables.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Loading variables isn't working with filesystem

Post by BoopDeePoop »

DaedalusYoung wrote:Because colons is not proper syntax to define variables.
That's strange then. I've seen other save files like this using colons and they worked. Idk. Maybe they did it a different way. At least I think I saw that. Idk, well anyway thanks for the help!
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Loading variables isn't working with filesystem

Post by Jasoco »

BoopDeePoop wrote:
DaedalusYoung wrote:Because colons is not proper syntax to define variables.
That's strange then. I've seen other save files like this using colons and they worked. Idk. Maybe they did it a different way. At least I think I saw that. Idk, well anyway thanks for the help!
Because those programs are designed to load the file line by line and parse it. You can do the same if you really want to. But it's so much easier to do it this way because it's Lua and can be loaded and executed. Whereas a .ini file isn't executed, rather parsed line by line and split into variable / value pairs at runtime.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests