Feature Requeset: XML/INI Support

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.
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Feature Requeset: XML/INI Support

Post by Luiji »

It would be nice if there was native support for XML or INI for saves.
Good bye.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Feature Requeset: XML/INI Support

Post by bartbes »

There's Lua!

And, there's an issue tracker which has a feature tracker: here.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Feature Requeset: XML/INI Support

Post by Jasoco »

I find it's much better to use Lua for saves because all you need to do is write all the variables into it in the "variable = value" form, then load it or require it at game load and you don't have to do any parsing. Makes so much more sense to do it that way.

Some rudimentary code (Not actual code, just an idea of how the flow goes):

Code: Select all

if (file exists save_file.lua) then 
  love.filesystem.load("save_file.lua")()
else
  variable1 = value1
  variable2 = value2
  ...

  --Save the file for next time
  saveSettings()
end

function saveSettings()
  open "save_file.lua" for writing
  local tempVariable = ""
  tempVariable = tempVariable .. "variable1 = " .. variable1 .. "\n"
  tempVariable = tempVariable .. "variable2 = " .. variable2 .. "\n"
  ...
  print into file tempVariable
end
The saveSettings(function) could be called any time to instantly save all your settings. By printing it all so that it ends up looking like what the code would normally, you don't have to do any parsing or loading later. Remember to print everything as it should look as code. Like my example above, printing "variable1 = " as a string, then following it with the value of it (By actually printing the variable1) it will show up as the following, if say variable1 was "true"... variable1 = true So when you load the file, it will load variable1 into memory as true. See how flexible it is? And you won't need to use a third party XML parser or parse an INI. (Which most of the time I've seen consists of the same variable = value format)

Using love.filesystem.load(file)() will load and execute the lua file immediately which is what you want. Remember the extra () at the end. Otherwise it just loads but doesn't execute it.

Oh, and make sure to set the variables up before calling the saveSettings() function for the first time so it actually has values to write or else you'll get errors. It's all in that if/then/else part where it checks for existence of the file.

And remember, that's not actual code for the most part. I'm at Barnes & Noble so I can't code it up right, but if you're into Löve, I'm sure you can figure it out with the Documentation Wiki.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Feature Requeset: XML/INI Support

Post by bartbes »

And, as jasoco points out, inis aren't that complicated, writing a parser would be trivial.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Feature Requeset: XML/INI Support

Post by Jasoco »

Every INI I've ever seen consisted of the same thing..

Code: Select all

this = that
this = that
somethingelse = anotherthing
whatever = whenever
etc = andsoon
Which is basically the same thing a .lua file can do. Except you don't have to write a useless parser. So just print all the variables and values into a file in that same format and you can load it easily.

The difference is you can even write functions into it. As long as you print it into the file the same way it should be coded, you can do whatever. I don't think INI's or even XML could contain actual functions. And XML would be even more restrictive and take up more space with all its tags and elements. Just go the LUA route.

You could even do this:

Code: Select all

variables = {this, that, anotherthing, whenever, andsoon }
If your game happened to store all variables as one array I guess. I don't know how your game works. Or will work.
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: Feature Requeset: XML/INI Support

Post by Luiji »

Hm...this is an interesting way to do things. The problem is that if there is an error in the save for some reason, the entire game could crash. There should be some sort of exception handler for syntax errors etc.

Possible upside: if there is a function to compile Lua code, then you could make the saves harder to edit. Hm...

Thanks for the suggestion!
Good bye.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Feature Requeset: XML/INI Support

Post by bartbes »

To compile: string.dump()
To run with error checking: pcall()/xpcall()
All in standard lua.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Feature Requeset: XML/INI Support

Post by Robin »

Luiji wrote:Hm...this is an interesting way to do things. The problem is that if there is an error in the save for some reason, the entire game could crash. There should be some sort of exception handler for syntax errors etc.
introducing: pcall()
Luiji wrote:Possible upside: if there is a function to compile Lua code, then you could make the saves harder to edit. Hm...
Methinks that would be a bad idea.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Feature Requeset: XML/INI Support

Post by Jasoco »

What, compiling Lua? What if we make a release game that we actually want to sell, or give away. Is there no way to keep the code from prying eyes? At least certain parts of it like the engine. I mean you don't see code for Cave Story or Portal. It's compiled into binaries.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Feature Requeset: XML/INI Support

Post by bartbes »

LAME.
And, as I mentioned you can compile.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 232 guests