Saving settings/scores

BlitzMax Forums/BlitzMax Beginners Area/Saving settings/scores

I was afraid that you were going to ask that question.
Same concept as above but instead of using an array, we use a type and a list as follows:-


Strict

Local HiScoreList:TList=CreateList()
Local MaxItems=10
Local h:THiScore 'HiScore Type, see definition below

Print "==============="
Print "Unsorted"

For Local i:Int=0 To MaxItems
	h:THiScore=New THiscore
	h.score=Rnd(0,10000)
	h.PlayerName="Bill Gates"
	ListAddLast HiScoreList,h
	Print "hiscore["+i+"]="+h.PlayerName+":"+h.Score
Next


HiScoreList.Sort(True,THiscore.MySort) 'Note that we defined the sort function below
Print "==============="
Print "Sorted"
Local i:Int=0
For Local h:THiScore=EachIn HiScoreList
	Print "hiscore["+i+"]="+h.PlayerName+":"+h.Score
	i :+ 1
Next

'write hi-score
Local out:TStream=WriteStream("c:\Hiscore.dat")

For Local h:THiScore=EachIn HiScoreList
	WriteInt(Out,Len(h.PlayerName))
	WriteString(Out,h.PlayerName)
	WriteInt(Out,h.Score)
Next

CloseStream(Out)

ClearList HiScoreList
'Read hi-score
Local In:TStream=ReadStream("c:\Hiscore.dat")

Local s:Int=0
While Not Eof(In)
	h:THiScore=New THiscore
	s=ReadInt(In)
	h.PlayerName=ReadString(In,s)
	h.Score=ReadInt(In)
	ListAddLast HiScoreList,h
Wend

CloseStream(In)

Print "==============="
Print "Read from File"
i:Int=0
For Local h:THiScore=EachIn HiScoreList
	Print "hiscore["+i+"]="+h.PlayerName+":"+h.Score
	i :+ 1
Next

'-----Define Type Here
Type THiscore

 Field PlayerName:String
 Field score:Int

'--Need to write our own sort function, sort by score not name
 Function MySort(m1:Object,m2:Object)
  If THiscore(m1).score > THiscore(m2).score Then Return 1
  If THiscore(m1).score > THiscore(m2).score Then Return -1
  Return 0
 End Function

EndType