 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
tmaster
Joined: 21 Oct 2005 Posts: 12 Location: Ireland
|
Posted: Mon Apr 03, 2006 12:47 am Post subject: Loading and saving scores from a file |
|
|
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 |
|
| Back to top |
|
 |
JorDy
Joined: 11 Dec 2005 Posts: 121
|
Posted: Mon Apr 03, 2006 2:49 am Post subject: |
|
|
here you go all writen for you by me(small cred in game would be nice :))
| Code: | 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 |
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 |
|
| Back to top |
|
 |
tmaster
Joined: 21 Oct 2005 Posts: 12 Location: Ireland
|
Posted: Mon Apr 03, 2006 2:56 am Post subject: |
|
|
it is very well detailed.
Thanks
will do. |
|
| Back to top |
|
 |
JorDy
Joined: 11 Dec 2005 Posts: 121
|
Posted: Mon Apr 03, 2006 2:58 am Post subject: |
|
|
| no problem glad i could help |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Mon Apr 03, 2006 5:56 am Post subject: |
|
|
| JorDy wrote: | | [lots of complicated code] |
Looks like you have written too much C or Java code :-)
| Code: |
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()
|
output:
| Quote: |
name: Frank
score: 1234
name: John
score: 567
level: 42
name: Frank
score: 1234
level: 39
name: John
score: 567
|
|
|
| Back to top |
|
 |
glynnder
Joined: 04 Sep 2005 Posts: 35
|
Posted: Mon Apr 03, 2006 10:10 pm Post subject: |
|
|
| lol i wondered why that looked so much like java |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|