Highscore trouble

Advanced game related topics
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Highscore trouble

Post 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!)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Highscore trouble

Post 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.
Last edited by IdeasVacuum on Tue Sep 18, 2012 6:48 pm, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Highscore trouble

Post 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?
Sirius-2337
User
User
Posts: 59
Joined: Sat May 14, 2011 10:39 am

Re: Highscore trouble

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Highscore trouble

Post 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.
Post Reply