Page 1 of 1

Highscore trouble

Posted: Tue Sep 18, 2012 6:24 pm
by J@ckWhiteIII
Hey, I'm trying to code a little highscore. Player name and score are seperated with "|"; and example:

player1|39
...
player22|552
...

To insert the new player's score and name, I wanted to use this:

Code: Select all

*Mem = AllocateMemory (5000)
*Mem2 = AllocateMemory (5000)
Protected Lines.w
Protected Size.w
Protected ActualSize.w
Protected String.s
Protected PlayerScore.w
OpenFile (1,"Highscore.ini")
ReadData (1,*Mem,Lof(1))
Lines = CountString (PeekS (*Mem),"|")
FreeMemory (*Mem)
For i = 0 To Lines
  Size = ReadData (1,*Mem2,200)
  ActualSize + Size
  String = PeekS (*Mem2)
  PlayerScore = Val(StringField(String,2,"|"))
  If PlayerScore > X
    FileSeek (1,ActualSize)
    WriteStringN (1,Name + "|" + Str(X))
  EndIf
Next
FreeMemory (*Mem2)
CloseFile (1)
...But, nothing happens. Does anyone have an idea why?
Thanks in advance :)

P.s. I didn't use integers because I have to use my old computer right now; it only has PB 4.00 (and some kinda weird version of that!)

Re: Highscore trouble

Posted: Tue Sep 18, 2012 6:38 pm
by IdeasVacuum
Hi Jack


FreeMemory (*Mem)

You free the mem before you read it........ oh, no you dont, you have 2 allocations. So, why do you have two mem allocations? More over, why read into memory? Are you expecting to read thousands of lines? Edit: you have a mysterious X value.

I think it would be easier to use the ReadString function - read a line, chop into the two values with stringfield, next line.

Re: Highscore trouble

Posted: Tue Sep 18, 2012 6:47 pm
by J@ckWhiteIII
No, I wanted to limit the list to 10 ranks later. My problem is just, that I have no idea how to insert a new line at a special point. That's why i wanted to count the lines (only one "|" per line) and then find out the size of the file that's already read (to set the write pointer to that place). Any idea how that can be done more simple?

Re: Highscore trouble

Posted: Tue Sep 18, 2012 7:45 pm
by Sirius-2337
Maybe this code does what you want

Code: Select all

FileName$ = "D:\Highscore.ini"

; - - - Load Scores into List

OpenFile (1, FileName$)

Structure Score
  Name.s
  Score.l
EndStructure

NewList Highscore.Score()
Define Line$

For x = 1 To 10
  
  Line$ = ReadString(1)
  
  AddElement(Highscore())
  
  If FindString(Line$, "|")
    Highscore()\Name  =     StringField(Line$, 1, "|")
    Highscore()\Score = Val(StringField(Line$, 2, "|"))
  EndIf
  
Next

CloseFile(1)

; - - - Insert new Score

Define NewName$  = "P11B"
Define NewScore.l = Random(200)

ForEach Highscore()
  
  If NewScore > Highscore()\Score
    
    InsertElement(Highscore())
    Highscore()\Name  = NewName$
    Highscore()\Score = NewScore
    
    LastElement(Highscore())
    DeleteElement(Highscore())
    
    Break
  EndIf
  
Next

; - - - Save Scores

CreateFile(1, FileName$)

ForEach Highscore()
  WriteStringN(1, Highscore()\Name + "|" + Str(Highscore()\Score))
Next

CloseFile(1)

End

Re: Highscore trouble

Posted: Tue Sep 18, 2012 7:49 pm
by Demivec
I think you need to reset the file position before you start the loop.

You read the file examine the contents and then you are attempting to read the file again without resetting the file position to the start of the file.