So I’m trying to implement a high score list that persists from game to game and I’m having trouble with how to keep adding data elements over separate games.
Here’s what I have so far:
local function packData()
dataTable = {}
– get game score
dataTable.score = score
end
local function loadData()
local file = io.open( filePath, “r” )
if file then
– Read file contents into a string
local dataStr = file:read( “*a” )
– Break string into separate variables and construct new table from resulting data
local datavars = str.split(dataStr, “,”)
dataTableNew = {}
for i = 1, #datavars do
– split each name/value pair
local onevalue = str.split(datavars[i], “=”)
dataTableNew[onevalue[1]] = onevalue[2]
end
io.close( file )
– Note: all values arrive as strings; cast to numbers where numbers are expected
dataTableNew[“score”] = tonumber(dataTableNew[“score”])
else
print (“no file found”)
end
end
local function saveData()
if (dataTable) then
file = io.open( filePath, “w” )
for k,v in pairs( dataTable ) do
file:write( k … “=” … v … “,” )
end
io.close( file )
– HOW WOULD I ITERATE THROUGH THE LIST OF SCORES, SORT THEM HIGH TO LOW, AND DISPLAY THEM?
local msg = display.newText( “Score:” … score, 0, 0, nil, 12 )
msg:setTextColor( 255, 255, 136, 255 )
msggroup:insert( msg, true )
else
print (“no dataTable”)
end
end [import]uid: 1560 topic_id: 1886 reply_id: 301886[/import]