I need help with Loading and saving scores from a file
i am going to have 2 arrays and two functions
names ={} -- it is going to hold the top ten names
scores ={} -- it is going to hold the top ten scores
loadscore()
savescore()
i want to be able to load the top ten names and scores from a file to the arrays. also i want to be able to save the arrays back to the same file
eg: file layout
Name1
Score1
Name2
Score2
:
:
:
Name10
Score10
Thanks
Loading and saving scores from a file
Moderators: Shine, Insert_witty_name
here you go all writen for you by me(small cred in game would be nice :))
dont worry about when writing the scores and names the table to string converts both name and score tables to one big string then writes it. so as long as both tables have something in them it will work perfectly
Code: Select all
white = Color.new(255,255,255)
scoresFile = "scores.txt"
topTenNames = {}
topTenScores = {}
--table to string function (OUTPUT Variable= "OutputString")
function tableToString(t)
local tempFile = "temp.table"
tableLength = table.getn(t)
file = io.open(tempFile, "w")
if file then
for i=1, tableLength do
file:write(t[i].." ")
end
file:close()
end
file = io.open(tempFile, "r")
if file then
OutputString = file:read("*a")
file:close()
end
return OutputString
end
function readScores()
file = io.open(scoresFile, "r")
if file then
FileContent = tostring(file:read("*a"))
end
---SEPARATE FILE
if FileContent==nil then return 0 end
local a=1
local fileString = {}
--split the file contents into words (file format = name name name name name name name name name name score score score ...)
for scores in string.gfind(FileContent, "%w+") do
screen:clear()
fileString[a] = scores
a = a+1
end
--place the first 10 strings (names) into the table
for i=1, 10 do
topTenNames[i] = tostring(fileString[i])
end
--place the 2nd 10 strings (scores) into the table
for i = 1, 10 do
topTenScores[i] = tostring(fileString[i+10])
end
end
function writeScores()
file = io.open(scoresFile, "w")
if file then
names = tableToString(topTenNames)
scores = tableToString(topTenScores)
file:write(names .. scores)
file:close()
end
end
readScores()
for i=1, 10 do
screen:print(10,(i*10),topTenNames[i],white)
screen:print(100,(i*10),topTenScores[i],white)
screen.flip()
end
while true do
screen:waitVblankStart()
end
Looks like you have written too much C or Java code :-)JorDy wrote:[lots of complicated code]
Code: Select all
function loadScore()
dofile("score.txt")
end
function saveScore()
file = io.open("score.txt", "w")
if file then
file:write("score={")
for i = 1, table.getn(score) do
file:write("{")
for key, value in score[i] do
file:write(key .. "=")
if type(value) == "number" then
file:write(tostring(value))
else
file:write(""" .. value .. """)
end
file:write(",")
end
file:write("},")
end
file:write("}")
file:close()
end
end
function printScore()
for i = 1, table.getn(score) do
for key, value in score[i] do
print(key .. ": " .. tostring(value))
end
print("")
end
end
-- some test scores
score = { { name="Frank", score=1234 }, { name="John", score=567} }
-- save it
saveScore()
-- delete local variable
score = {}
-- load it again
loadScore()
-- show it
printScore()
-- another example, with an additional "level" field
score = { { name="Frank", score=1234, level=42 }, { name="John", score=567, level=39 } }
saveScore()
score = {}
loadScore()
printScore()
name: Frank
score: 1234
name: John
score: 567
level: 42
name: Frank
score: 1234
level: 39
name: John
score: 567