I've been searching the forum and Ultimate Guide for information on saving multiple variables into a single settings file.
I used this from the Ultimate Guide first:
local playerName = "Awesome Player"
local file = io.open ( "|D|settings.save" , "w+" )
file:write( "Playername:" , playerName )
file:close()
local playerName = nil
local file = io.open ( "|D|settings.save", "r" )
playerName = file:read()
file:close()
print ( "The playername saved is: ", playerName) |
Unfortunately, the output is:
The playername saved is: Playername:Awesome Player |
which means the variable playerName isn't "Awesome Player" but "Playername:Awesome Player" which is not what I want.
Also, if I try multiple file:writes with different variables, I can only read into the first variable and it's almost all the values combined:
local playerOne = "Jason"
local score = 21450
local hiscore = 300000
local filesave = io.open("|D|settings.save", "w+")
filesave:write("playerOne", playerOne)
filesave:write("score", score)
filesave:write("hiscore", hiscore)
filesave:close()
playerOne = nil
score = nil
hiscore = nil
local fileload = io.open("|D|settings.save", "r")
playerOne = fileload:read()
score = fileload:read()
hiscore = fileload:read()
fileload:close()
print ("Player One: ", playerOne)
print ("Score: ", score)
print ("Hi Score: ", hiscore) |
Player One: playerOneJasonscore0
Score: nil
Hi Score: nil |
Help!
Comments
http://www.giderosmobile.com/forum/discussion/377/writing-to-file
...but it seems to me that:
a) You don't want to be writing strings like "playerOne" to the file, just the data.
b) You're reading back the contents of the whole file into a variable. After that you'll need to process it to put it into a structured form. So I'd be thinking in terms of saving the data in the format of something like a CSV file, with comma delimiters or whatever, then writing a function that could read this in, process it and stick it in a table.
(But there's a probably a better or standard way that someone will explain...)
Or use something from ready to use methods as dataSaver with json:
http://appcodingeasy.com/Gideros-Mobile/Save-and-load-data-module-for-Gideros-Mobile
Or saving lua tables to file
http://www.giderosmobile.com/forum/discussion/367/save-load-a-table
Likes: BJG
From the above result it means the example given in the Ultimate Guide is wrong.
http://BlueBilby.com/
This has just saved a file called settings.save with the values "Playername: Awesome Player"
And that is what it did, isn't it?
My main point is that it is an example of how to handle files (reading/writing) and not how to save and retrieve scores
Why would you save a value of "Awesome Player" but want to retrieve it as "Playername:Awesome Player"? A totally useless example. Makes no sense whatsover.
Also, you cannot use playerName=file:read( "Playername"). THAT would be useful.
No sense at all!
http://BlueBilby.com/
http://BlueBilby.com/
Here's the code:
I hope this helps
best regards
Attached is the library I currently use.
And here's some code to use it
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
On OS X, I need to use:
I only noticed when I worked on the code on Windows and it was working, but testing the same code on OS X would crash the Gideros Player.
The Ultimate Guide says to use the method above with a comma. I guess it wasn't tested on all platforms.
http://BlueBilby.com/
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
In fact. If I save a score on Windows, eg. 2750. It works fine with a comma.
If I use the comma in OSX the value saved is either of these:
0.
0.0
5.
Or something else totally different. No tab but a dot added for no reason and the wrong value.
Also, I read in a tutorial somewhere that the comma is the preferred way to do it. Will have to try and find it.
http://BlueBilby.com/
That's what the Lua manual seems to indicate. http://www.lua.org/pil/21.1.html
When you introduce numbers, eg:
If you use "tostring" to do the conversion explicitly...
Likes: WauloK
http://BlueBilby.com/