Posted: 18th Sep 2011 22:27
I'm having a bit of trouble saving higscore to a file. I'll give some code

Code:
+ Code Snippet
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
+ Code Snippet
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?
Posted: 18th Sep 2011 22:54
FileW = OpenToWrite (file$, 0)


Change the ",0" to ",1". 0 will make it overwrite the file and 1 will append it.

Quote from documentation:
ppend - Set to 1 to append to the file, 0 to overwrite all data (optional, default 1).
Posted: 18th Sep 2011 23:02
It needs to overwrite the whole file.

It reads the code in a dim then checks for that dim if the score is bigger then one of the dimmed scores if it is it puts all current scores that are lower than the current score one place lower so the 10th best score dissapears 9th best score becomes 10th, 8th best score becomes 9th and so on. Then when its at the part where your score needs to come it puts it right there in the dim.

Then it OVERWRITES the file with the whole new dim which is what it needs to do.
Posted: 18th Sep 2011 23:25
I do it like this anyway

This gets wath position the user have entered!

+ Code Snippet
function HighScore_Check()
    User_Position = 0
	for t = 1 to 5
        if Player.Score > HighScoreNR [ t ]
           User_Position = t
           exit
        endif
    next t
endfunction


This shuffles the others down and puts the players entry at the right place!
+ Code Snippet
function HighScore_Setup()
     if User_Position = 0 then goto exit_HS_Setup
     temp = 5
     for b = User_Position to 5
         HighScoreNR [ temp ] = HighScoreNR [ temp - 1 ]
         HighScoreNAME$ [ temp ] = HighScoreNAME$ [ temp - 1 ]
         dec temp
     next b

     HighScoreNR [ User_Position ]= Player.Score
     HighScoreNAME$ [ User_Position ] = UserName$
     Input_State = 0
     HighScore_Write()
     exit_HS_Setup:
endfunction



I have made an complete file to be used in all my games

Simply paste in the functions and voila an high score
Posted: 18th Sep 2011 23:46
Thanks cliff, i'll look into this tomorrow as it's time to say good night for me.

Its lesser lines than my code

I'm sure it works with some editing from my end.
Posted: 19th Sep 2011 1:06
Yes but this is only parts of my high score code

You only wanted help with getting the player entry in the right place and to shuffle down the lower scores?

If it dont work so could you try my complete file !

This is still work in progress for me but it works!

+ Code Snippet
Initiate_HighScore_Table:
    global UserName$=""
    global Input_State=0
    global User_Position = 0
    global HG_1
    global HG_2
    global HG_3
    global HG_4
    global HG_5

    global dim HighScoreNR [ 5 ]
    global dim HighScoreNAME$ [ 5 ]
    score = 1600
    for t = 1 to 5
       HighScoreNR [ t ] = score
       score = score - 200
    next t
    HighScoreNAME$ [ 1 ] = "CLIFF"
    HighScoreNAME$ [ 2 ] = "STINA"
    HighScoreNAME$ [ 3 ] = "ANN"
    HighScoreNAME$ [ 4 ] = "STOFFE"
    HighScoreNAME$ [ 5 ] = "CLAES"
    HighScore_Read()
return

function HighScore_Check()
    User_Position = 0
	for t = 1 to 5
        if Player.Score > HighScoreNR [ t ]
           User_Position = t
           exit
        endif
    next t
endfunction

function HighScore_Get_Name()
    if User_Position = 0 then goto exit_HS_get_name
    if Input_State = 0
       Input_State = 1
       StartTextInput ( )
    endif

    if GetTextInputCompleted ( ) = 1
       UserName$ = GetTextInput ( )
    endif
    exit_HS_get_name:
endfunction

function HighScore_Setup()
     if User_Position = 0 then goto exit_HS_Setup
     temp = 5
     for b = User_Position to 5
         HighScoreNR [ temp ] = HighScoreNR [ temp - 1 ]
         HighScoreNAME$ [ temp ] = HighScoreNAME$ [ temp - 1 ]
         dec temp
     next b

     HighScoreNR [ User_Position ]= Player.Score
     HighScoreNAME$ [ User_Position ] = UserName$
     Input_State = 0
     HighScore_Write()
     exit_HS_Setup:
endfunction

function HighScore_Show( Task )
     temp = HG_1-1
     for t =1 to 5
        SetTextVisible ( temp + t , task )
        SetTextString (temp + t ,Str(t) + " . "+ HighScoreNAME$ [ t ] + " . "+Str(HighScoreNR [ t ]))
     next t
endfunction

function HighScore_Write()
     OpenToWrite ( 1, "BoinkaDroid.Score", 0 )
     for t = 1 to 5
        WriteInteger ( 1,HighScoreNR [ t ])
        WriteString ( 1,HighScoreNAME$ [ t ])
     next t
     CloseFile ( 1 )
endfunction

function HighScore_Read()
     if GetFileExists( "BoinkaDroid.Score" )=1
        OpenToRead ( 1, "BoinkaDroid.Score" )
        for t = 1 to 5
           HighScoreNR [ t ] = ReadInteger ( 1 )
           HighScoreNAME$ [ t ] = ReadString ( 1 )
        next t
        CloseFile ( 1 )
     endif
endfunction




Cheers......

Ps...... if you scroll the view offset so must you reset it back to o,o or the agk input window will not always show up!
Posted: 19th Sep 2011 19:59
The first part was exactly what i was looking for. In your second part i have a tip for ya.

Type HScore
name$
nr
Endtype

dim HighScore[5] as Hscore

and then change every HighScoreNR to Highscore.nr and HighScoreName$ to Highscore.name$

Its not that your part is wrong but i think it's neater code.

Gonna add your first part to my code now and test it out.
Posted: 19th Sep 2011 20:36
Just tested it and made it so that it works with my code.

Again thanks alot Cliff
Posted: 19th Sep 2011 23:49
onna add your first part to my code now and test it out.

Thanks for the input
I whas still working on it but it works !
Will probably add that later on

gain thanks alot Cliff

We are here to help each other

Updated the read and write function in to one today also!
Saves some lines of bytecode

+ Code Snippet
function HighScore_FileIO(state,Name$)
     if GetFileExists( Name$ )=0 and state=0 then goto skipReadHS
     if state = 1
        OpenToWrite ( 1, Name$, 0 )
     else
        OpenToRead ( 1, Name$ )
     endif
     for t = 1 to 5
        if state = 1
           WriteInteger ( 1,HighScoreNR [ t ])
           WriteString ( 1,HighScoreNAME$ [ t ])
        else
           HighScoreNR [ t ] = ReadInteger ( 1 )
           HighScoreNAME$ [ t ] = ReadString ( 1 )
        endif
     next t
     CloseFile ( 1 )
     skipReadHS:
endfunction
Posted: 20th Sep 2011 10:30
There is no need to keep putting stuff like this in your code:
+ Code Snippet
Function wotsit
  If this = that Then Goto somewhere
  (blah)

  somewhere:
EndFunction


Just do this:
+ Code Snippet
Function wotsit
  If this = that Then ExitFunction
  (blah)
EndFunction
Posted: 20th Sep 2011 22:07
Does the exit function command work in the agk ?

I tryed to use it in the beginning but it read the entire function anyway

But that whas in the first release

Thanks bursar are going to check if it works now ?

I actually hate the goto command

Edited................
I knew there whas a reason i used it

exit function uses more lines of bytecode then the 2 lines neaded for the goto function
Posted: 20th Sep 2011 22:10
I do it even different still.. May not be useful to you now, but it's how I handle it.

+ Code Snippet
Function Data_LoadHighScores()
    FileState = GetFileExists("HighScore.dat")

    Select FileState

        Case 0: `Does not exist
            // Uh - Oh! We'll need to make the highscores and save them first.
            // This sets the highest score currently on the list.
            Score = MaxHighScores * 750

            For i = 1 to MaxHighScores
                HighScore[i].Name = "System"
                HighScore[i].Score = Score

                // Decrease the value, so that the score below this will be slightly easier to overtake.
                Score = Score - 700
            Next i

            // Save the scores for future reference.
            Data_SaveHighScores()
        EndCase

        Case 1: `Does Exist
            // Open the file, and read the data from it.
            TempFile = OpenToRead("HighScore.dat")

            For i = 1 to MaxHighScores
                HighScore[i].Name = ReadLine(TempFile)
                HighScore[i].Score = ReadInteger(TempFile)
            Next i
            CloseFile(TempFile)
        EndCase
    EndSelect
EndFunction

Function Data_SaveHighScores()
    // Open the file and save the data to it.
    TempFile = OpenToWrite("HighScore.dat", 0)
    For i = 1 to MaxHighScores
        WriteLine(TempFile, HighScore[i].Name)
        WriteInteger(TempFile, HighScore[i].Score)
    Next i
    CloseFile(TempFile)
EndFunction
Posted: 20th Sep 2011 22:18
I belive your version is slightly faster ?

Thanks for the input