Page 1 of 1

[Solved]Loading variables isn't working with filesystem

Posted: Mon Jul 21, 2014 8:27 pm
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.

Re: Loading variables isn't working with filesystem

Posted: Mon Jul 21, 2014 8:58 pm
by Joemag
Just replace ":" with "=".
It should be saved as:

Code: Select all

highscore=0
.

Re: Loading variables isn't working with filesystem

Posted: Mon Jul 21, 2014 9:28 pm
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?

Re: Loading variables isn't working with filesystem

Posted: Mon Jul 21, 2014 9:38 pm
by DaedalusYoung
Because colons is not proper syntax to define variables.

Re: Loading variables isn't working with filesystem

Posted: Mon Jul 21, 2014 9:46 pm
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!

Re: Loading variables isn't working with filesystem

Posted: Tue Jul 22, 2014 7:11 am
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.