Saving High Scores

ddeckert

Member
Licensed User
Longtime User
I'm a long time programmer but new to Basic4Android.

I am finishing a game and would like to get some recommendations on saving the High Scores to the local device.

All I will be saving is the Name and Score.

Thanks in advance for your recomendations.

Dave:D
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I am finishing a game and would like to get some recommendations on saving the High Scores to the local device.

All I will be saving is the Name and Score.

Here's code (just the essentials) I use for that:

B4X:
Sub Activity_Create
    ReadOptions
End Sub
Sub Activity_Pause
    SaveOptions
End Sub
Sub SaveOptions
    Dim TW As TextWriter
    TW.Initialize(File.OpenOutput(File.DirInternal, "[file name]", False))
    TW.WriteLine(player)
    TW.WriteLine(score)
    TW.Close
End Sub
Sub ReadOptions
    If File.Exists(File.DirInternal, "[file name]") = False Then Return   
    Dim TW As TextReader
    TW.Initialize(File.OpenInput(File.DirInternal, "[file name]"))
    player = TW.ReadLine
    score = TW.ReadLine
    TW.Close
End Sub
 
Upvote 0

ddeckert

Member
Licensed User
Longtime User
Thanks all.

I went with the StateManager and settings. I figured this will come in handy with other projects too.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Here's code (just the essentials) I use for that:

B4X:
Sub Activity_Create
    ReadOptions
End Sub
Sub Activity_Pause
    SaveOptions
End Sub
Sub SaveOptions
    Dim TW As TextWriter
    TW.Initialize(File.OpenOutput(File.DirInternal, "[file name]", False))
    TW.WriteLine(player)
    TW.WriteLine(score)
    TW.Close
End Sub
Sub ReadOptions
    If File.Exists(File.DirInternal, "[file name]") = False Then Return   
    Dim TW As TextReader
    TW.Initialize(File.OpenInput(File.DirInternal, "[file name]"))
    player = TW.ReadLine
    score = TW.ReadLine
    TW.Close
End Sub


How are you getting the HighScores into a ListView or making them visible to the user?
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Oh so it is literally a high score and not a high score list? I will mess around with it once I am back at my computer.

So just label.text = TW ?
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Yeah, I'm just keeping the highest score for the individual user.
FWIW, here's my complete routine:

B4X:
labelHiGameScore.Text = "Previous high game score: " & HiGameScore 
If labelHiGameScore.Height > 50 Then
   labelHiGameScore.Height = labelHiGameScore.Height / 2
End If
If Score(0, GameScore) > HiGameScore Then
   HiGameScore = Score(0, GameScore)
   labelHiGameScore.Text = labelHiGameScore.Text & CRLF & _
                     "New high game score: " & HiGameScore
   labelHiGameScore.Height = labelHiGameScore.Height * 2
End If
labelHiGameScore.Top = Activity.Height - labelHiGameScore.Height
labelHiGameScore.Visible = True
labelHiGameScore.BringToFront
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I can't seem to get the High Score into a label. I tried:

B4X:
lblHighScore.Text = TW.Readline

and

B4X:
lblHighScore.Text = Score

How do you extract the saved data into a label?
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I can't seem to get the High Score into a label. I tried:

B4X:
lblHighScore.Text = TW.Readline

and

B4X:
lblHighScore.Text = Score

How do you extract the saved data into a label?

Regarding the first attempt -- I don't know that you can read a line directly into a label. Try this:

str_HighScore = TW.Readline
lblHighScore.Text = "High score = " & str_HighScore
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
works in my test program, but when I try to apply it to my app it forces me to initialize my label even though I created it in Designer and dim'd it there. It then shows nothing...any idea of why this is happening?
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
works in my test program, but when I try to apply it to my app it forces me to initialize my label even though I created it in Designer and dim'd it there. It then shows nothing...any idea of why this is happening?

If you created it in Designer and you are sure that:
(1) you saved the layout/variant and that
(2) you are loading that same layout and that
(3) you are using the same label name as in Designer,
then there is no way B4A should be saying you need to initialize the label.

You say that you "created it in Designer and dim'd it there".
You can't really "dim" it in Designer.
You must dim it in the code.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
1. I am an idiot and I didn't save it to the layout I was loading.

2. You can right-click objects in designer and 'dim' them. It will automatically place the code in your globals.

3. Thanks for being patient with me! :)
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
1. I am an idiot and I didn't save it to the layout I was loading.

2. You can right-click objects in designer and 'dim' them. It will automatically place the code in your globals.

3. Thanks for being patient with me! :)
:D If you're an idiot, so are we all. The reason I knew that #1 was possible is that I've done the same thing.
 
Upvote 0
Top