I do I add local to my game? [import]uid: 40990 topic_id: 8790 reply_id: 308790[/import]
Save an integer into a file by I/O functions into Document directory when a game ends and then restore it when your game starts next time.
– Advanced UI + Graph for Corona® | Website | Forum (https) | See:
[import]uid: 11385 topic_id: 8790 reply_id: 32054[/import]
If you just need to save a one-line value to a file, you can paste the following function into your code:
[blockcode]
local saveValue = function( strFilename, strValue )
– will save specified value to specified file
local theFile = strFilename
local theValue = strValue
local path = system.pathForFile( theFile, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “w+” )
if file then
– write game score to the text file
file:write( theValue )
io.close( file )
end
end
[/blockcode]
And this is how you would use it:
[blockcode]
local myScore = 500
saveValue( “score.txt”, myScore )
[/blockcode]
And then you can use this function to load a file (one-line file only, with this function):
[blockcode]
local loadValue = function( strFilename )
– will load specified file, or create new file if it doesn’t exist
local theFile = strFilename
local path = system.pathForFile( theFile, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
io.close( file )
return contents
else
– create file b/c it doesn’t exist yet
file = io.open( path, “w” )
file:write( “0” )
io.close( file )
return “0”
end
end
[/blockcode]
And here’s how you would use that function:
[blockcode]
local loadedScore = loadValue( “score.txt” )
[/blockcode]
Hope that helps! That function works for a most basic solution. If you need to read a file that consists of multiple lines of data, then things get a little more trickier.
If you simply need to load/save a score though, those functions should be just fine. [import]uid: 52430 topic_id: 8790 reply_id: 32637[/import]
Use JSON, pretty easy to set up and you can save/load a much you want. [import]uid: 5712 topic_id: 8790 reply_id: 32639[/import]
thanks for your reponses, but I need to add best scores, I have tried the sample code on the Code Exchange, but I am getting errors [import]uid: 40990 topic_id: 8790 reply_id: 34863[/import]