I'm having a bit of trouble saving higscore to a file. I'll give some code
Code:
global file$ as string = "hiscore.dat"
// this part of code is in my initilize part of the program
// i use this to see if the hiscore file exists if not
// this part creates a "fake" code file for you to beat
if GetFileExists(file$) = 0
fileID = OpenToWrite(file$,0)
fakescore = 1000
for FakeIT = 1 to 10
WriteString (FileID, "Jeffrey")
writeString (FileID, str(FakeScore))
dec Fakescore, 100
next FakeIt
closefile(fileID)
endif
// between here there is some code for my game
// when you do certain things you get score
// in this purpose lets assume that we have made a highscore
function CheckHighScore()
// open up the file and put all the scores inside the dat file
// into a dimension (i already made it a type and dimed it in my
// code!
FileN = OpenToRead (file$)
for GetHS = 1 to 10
hscore[GetHS].name$ = readstring(FileN)
hscore[GetHS].score = val(readstring(FileN))
next GetHS
closefile(FileN)
for CheckScore = 1 to 10
// start checking of the current score is higher than in the
// dim.
if hscore[CheckScore].score < score
for New = 9 to CheckStore step -1
// if it is we need to put 9 in 10, 8 in 9, 7 in 8, ...
hscore[new + 1].score = hscore[new].score
hscore[new + 1].name$ = hscore[new].name$
next new
retry:
while gettextinputcompleted() = false
starttextinput()
sync()
endwhile
IName$ = GetTextInput()
IName$ = left(IName$, 14)
// get the name from the user and cut down to length of 14
hscore[CheckScore].name$ = IName$
hscore[CheckScore].score = score
goto WritePart
// put it in the dim and go to the write part of this function
endif
next CheckScore
WritePart:
FileW = OpenToWrite (file$, 0)
// write all to the file and done
for WriteIt = 1 to 10
WriteString(FileW, hscore[WriteIt].name$)
WriteString(FileW, str(hscore[WriteIt].score))
next WriteIt
Closefile(FileW)
endfunction
hiscore.dat
Jeffrey 1000 Jeffrey 900 Jeffrey 800 Jeffrey 700 Jeffrey 600 Jeffrey 500 Jeffrey 400 Jeffrey 300 Jeffrey 200 Jeffrey 100
I have explained a bit in the code, i really cant seem to find whats wrong with my code but it seems that he does not move 9 to 10 and just overwrites the highscore. It's hard to explain its behavior. Anyone can help?
Only heroes live forever