Using XML for High Score Table

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1663 Views - Last Post: 18 January 2013 - 10:28 AM Rate Topic: -----

#16 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 04:10 PM

Yes, you won't be able to Remove the item until it has been Added.

Go to the Project ..Properties, click into the Value cell and type 1234;Pete (no quotes). I think you will probably need to click the build-button (...) on the right to do this, because the semi-colon confuses it!

This post has been edited by andrewsw: 17 January 2013 - 04:12 PM

Was This Post Helpful? 1
  • +
  • -

#17 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 17 January 2013 - 04:24 PM

Works perfectly! Thank you sir.

So at the end of the game if i put

My.Settings.HighScores.Add("UserScore;Username")

UserScore and Username are the variable names, They will be added to the list?

Also how do i then order them, and then make them appear on a different form.

Can't thank you enough for all this help, its real good.
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 04:34 PM

My.Settings.HighScores.Add("UserScore;Username")


Quote

UserScore and Username are the variable names, They will be added to the list?


If these are variables then you would need to do this:

My.Settings.HighScores.Add(UserScore & ";" & UserName)
'or
My.Settings.HighScores.Add(UserScore.ToString() & ";" & UserName)


My earlier code showed how they could be sorted.

You would need a reference to the other form..
.. get a reference to a control on that form, such as a ListBox..
.. use the code I gave earlier to loop through the scores..
.. add new items to the listbox.

Andy.

This post has been edited by andrewsw: 17 January 2013 - 04:35 PM

Was This Post Helpful? 0
  • +
  • -

#19 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 05:21 PM

A few comments...

I see you've discovered the read-only nature of Appication scope settings.

Now as to what to store, and how to store it. I would probably figure out how many characters I want to allow for a player name, then how many to allow for a score. Add these together, add 1 to the result, and your string shouled be that long.

Then, make the score string "<player><space(s)><score>". Assume a player length of 10 characters maximum, then you would extract the score later by using Cint(varname.SubString(10)). This can be compared with the integer value of the score being checked. When you add a new score to the array/list/whatever, you can use Format() to place the player name and to right-align the score, all ready for showing in a ListBox/TextBox. "Abigail 65540"

This post has been edited by lar3ry: 17 January 2013 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#20 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 05:33 PM

View PostMayzie12, on 17 January 2013 - 05:24 PM, said:

Works perfectly! Thank you sir.

So at the end of the game if i put

My.Settings.HighScores.Add("UserScore;Username")

UserScore and Username are the variable names, They will be added to the list?

Also how do i then order them, and then make them appear on a different form.

Can't thank you enough for all this help, its real good.

You'll want to traverse the array, from high-to-low scores, and when you find a score that is equal to or lower than the score you're adding, insert it before the one you found. If you use a function, you can also return a boolean indicating whether or not the score is in the high score list, and use that return to congratulate the player, if it made the high score list.

The easiest way to do that is to first get all your Settings into a List(Of String), then traverse it as above. The reason it's best to use a list is that after you insert the new score, you can place the list back into the Settings, leaving off the one that you just bumped into last place (say, the 11th in a list of 10 scores). The case where the score was equal to another score on the list is also automatically taken care of; the new score ending up higher in the list.
Was This Post Helpful? 0
  • +
  • -

#21 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 18 January 2013 - 04:24 AM

View Postandrewsw, on 17 January 2013 - 04:34 PM, said:

You would need a reference to the other form..
.. get a reference to a control on that form, such as a ListBox..
.. use the code I gave earlier to loop through the scores..
.. add new items to the listbox.


Right so ive put a listbox on the other form. How do i reference to it?

Then use this code that you posted earlier

        For Each score As String In My.Settings.HighScores
            Dim values() As String = score.Split(";")
            'MsgBox(score.ToString())
            theScores.Add(New Scores With {.Score = Int32.Parse(values(0)), .Name = values(1)})
        Next

        For Each score As Scores In theScores.OrderBy(Function(aScore) aScore.Score)
            MsgBox(score.Score & score.Name)
            'add these to a form control



And then change it so instead of printing it in av message box, get it to print in the listbox?

Cheers
Craig
Was This Post Helpful? 0
  • +
  • -

#22 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 04:40 AM

I would test it on the current form firstly. Just add a ListBox and the following line:

        For Each score As Scores In theScores.OrderByDescending(Function(aScore) aScore.Score)
            MsgBox(score.Score & score.Name)
            'add these to a form control
            ListBox1.Items.Add(score.Score & " " & score.Name)
        Next

(assuming the listbox is named ListBox1).

To reference another form is a little more complicated. How was the form started? Was it from a button of the first form? If so then you might have code like this:

Dim frm2 As New Form2
frm2.Show()
'so frm2 references the other form


So the line in the above code would be

frm2.ListBox1.Items.Add(score.Score & " " & score.Name)

Was This Post Helpful? 0
  • +
  • -

#23 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 18 January 2013 - 05:09 AM

Right so its working in Form 1 with the listbox, all good.

Outputs e.g. 5andrew
4craig

and so on...

Form 2 was started as you wrote above, i click a button on the main form and that one appears.

So if i put

frm2.ListBox1.Items.Add(score.Score & " " & score.Name)



in the form load or in the button sub, it will load up the scores into a listbox on form 2?

Thanks
Craig
Was This Post Helpful? 0
  • +
  • -

#24 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 05:23 AM

The form2 reference needs to be available to the whole class:

Public Class Form1
    Dim frm2 As Form2   'available anywhere within the class

'button code that displays the form..
    frm2 = New Form2

'code as previous post..
    frm2.ListBox1.Items.Add(score.Score & " " & score.Name)

Was This Post Helpful? 0
  • +
  • -

#25 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 05:29 AM

Of course, form-2 should really be executing the code to display the high-scores; it shouldn't be necessary to generate these scores from one form and then display them from code in that other form.

So, as you say, this code should really be in (for example) the load-event of the second form.

But, I suppose, you could get it working for now and re-think it later :whistling:/>

This post has been edited by andrewsw: 18 January 2013 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#26 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 18 January 2013 - 05:57 AM

Thats working perfectly thanks.

So if i just put

my.settings.save() after the code for adding the names and scores, it will save them permanantly?

All that is needed now is to put a wee vbTab in between the printing on the scores and names and it will be perfect.

Thanks a lot for your help, its been great.

Craig
Was This Post Helpful? 0
  • +
  • -

#27 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 06:06 AM

I mentioned previously that it wasn't saving for me straight-away after calling Save(). I think it needs to be in Release mode rather than Debug - there's a button on the main toolbar - or it might need to run as an executable file; I'm not certain.
Was This Post Helpful? 0
  • +
  • -

#28 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 06:13 AM

If you want to align it neatly in the ListBox, look into String.Format()
Was This Post Helpful? 0
  • +
  • -

#29 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 18 January 2013 - 06:20 AM

I gave it a shot, published the program and installed it, with the saving like this

        My.Settings.HighScores.Add(Userscore1.ToString() & ";" & Username1)
        My.Settings.HighScores.Add(Userscore2.ToString() & ";" & Username2)
        My.Settings.Save()



Closed the program, re-opened it and they weren't there :/

I really need to find the edit button......

Just also found out.

If i add a name to the table its fine,
But then if i try to add another name it does not work and highlights

frm2.show()

Says it cannot handle a disposed object~?
Was This Post Helpful? 0
  • +
  • -

#30 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 18 January 2013 - 06:34 AM

It worked for me if I saved them in the FormClosed event:

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        My.Settings.Save()
    End Sub


They don't show in the Project Properties, Settings area, but I can tell that the have persisted from the code.

"Cannot handle a disposed object": Did you declare frm2 at the top of the class, and is the form still open/available?

This post has been edited by andrewsw: 18 January 2013 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3