4 Replies - 15107 Views - Last Post: 26 July 2011 - 02:10 AM Rate Topic: -----

#1 Jester94   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-July 11

High Score system

Posted 24 July 2011 - 10:43 PM

I know this is a common topic and I have read many different topics of people asking for help. The most common reply is to use an XML File but all there aren't any helpful tutorials for the absolute beginner.

What I want to do is create a High Score system that takes the score at the end of a game, (Which is either Pong or Memory and is a form with forms), fits it into a list of the top 10 scores. These scores will need to be saved outside of the program so they can be called up later. The scores also need to be read during the application as well.

Is xml the easiest way to do this for a beginner, or is there a better way?

Also, if xml is the easiest way, could you please link to me a tutorial or give me an explanation that an absolute beginner would understand.

Is This A Good Question/Topic? 0
  • +

Replies To: High Score system

#2 Shiggsy   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 14-July 11

Re: High Score system

Posted 25 July 2011 - 03:17 AM

Hi there,

I don't know anything about XML, so I can't help you with that one. However, when I need to store all sorts of data I'll just use an array and a .txt file, with a 'Total' counter so I can grab everything in a loop. For example:

Dim I, Tot as Integer
Dim Scores(999,2) as String 'Here we will store the Name and Score for the highscores

Public Sub ReadFile()
' Here we will get the contents of our Highscore file
Input(1, Tot) ' This lets us know how many sets of data is in the file
For I = 1 to Tot
Input(1,Scores(I,1)) 'Name
Input(1,Scores(I,2)) 'Score
Next I
End Sub

Public Sub SaveFile()
' Here we will write our contents to a file
Writeline(1,Tot) 'So we always know how much data is in the file
For I = 1 to Tot
Next I
End Sub

Public Sub NewScore()
' Here we will add a New Score
Tot += 1
Score(Tot,1) = Textbox1.Text 'New Name
Score(Tot,2) = Textbox2.Text 'New Score
' Now either keep the info stored in the array until Form_Close event, or save it to the file
End Sub

Public Sub SortScores()
' Here you could add a way to sort the scores by highest value
End Sub

I know it's a few lines long, but its simple and it works.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: High Score system

Posted 25 July 2011 - 08:23 AM


I personally think that XML serialization is your easiest, cleanest option if your a beginner (or, indeed, if your not!). .NET has classes to help make serialization to XML extremely simple. These classes handle the saving to XML for you, so you don't have to worry about looping through your array of scores and saving/loading them one by one yourself.

I shall show you a quick, explained example of how to serialize an array of names to XML, and then load the values back again.

First things first, make sure you have these two namespaces imported (so that you have access to the required classes):

Imports System.IO
Imports System.Xml.Serialization

Right, we need to write the methods that can save/load objects to/from XML. My methods are going to be generic, meaning that you can reuse them again to save many different kinds of objects.

Let's start with the method to save objects to XML:

Private Sub SaveToXML(Of T)(ByVal objectToSave As T, ByVal filePath As String)

        Using textWriter As TextWriter = New StreamWriter(filePath)

            Dim serializer As New XmlSerializer(GetType(T))

            serializer.Serialize(textWriter, objectToSave)

        End Using

End Sub

First, we create a new StreamWriter object, passing to it the file path of the file we wish to create. Think about that as setting up a stream between our program, and the file we want to write to. We can write data to that stream, and it is then written to the file.

The 'using' stuff just means that once we are done with the stream, it will be closed for us automatically, i.e. we don't have to worry about closing it ourselves using Close() or Dispose() method (you should always make sure your streams get closed. A StreamWriter object represents a stream).

We then create a new XmlSerializer object, passing to it the type of the object we want to serialize. We use the GetType() method to get the Type of that object, that tells the XmlSerializer object what type of object we are trying to serialize.

Now, you may be wondering what 'T' is. T represents the type that the method is operating on. We tell the method what type we are operating on when we call the method. This is known as a generic method, and allows the method to operate on any type we choose, thus making it reusable in many different scenarios.

Then, all we do is call the Serialize() method of the XMLSerializer object, passing to it the StreamWriter object (so it knows where to write the XML to, as remember, the stream is the link between the file, and our program), and the object to save (of which we are passing in to the method as the first parameter to our SaveToXML method). This 'objectToSave' will be an array of strings in my case; in your case, it will be an array of Integers perhaps(?)

Now, we need a method to load the object we save to.XML back into our program:

Private Function LoadFromXML(Of T)(ByVal filePath As String) As T

        Using textReader As TextReader = New StreamReader(filePath)

            Dim deserializer As New XmlSerializer(GetType(T))

            Return deserializer.Deserialize(textReader)

        End Using

End Function

Now, as we can see, it is very similar to our SaveToXML method. However, there are a few minor differences. One being that we use a StreamReader object this time, rather than a StreamWriter. This is because we want to read from a file this time, rather than write to it.

The second difference is that we call the Deserialize() method instead of the Serialize() method. We pass in the StreamReader object to this method so it knows where to read from, and the method does all the work for us, and returns a brand new object (filled with the data from the XML file we saved). We then return this object for use in our program.

And that is all you will ever need to perform basic XML serialization on any serializable object!

Now, an example of saving and loading an array of names follows (it is in a console application's Main() method:

Sub Main()

        'array of names that we are going to save.
        'You will have an array of scores in your application.
        Dim namesToSave As String() = New String() {"John", "Jill", "Jack", "James", "Jason"}

        'file path of .XML file we are going to save.
        'You don't have to create it beforehand,
        'as it will be created automatically for you if it doesn't
        'already exist
        Dim fileToSave As String = "HighScores.xml"

        'save array of names, passing in the file path of the file to save
        SaveNamesToXML(Of String())(namesToSave, fileToSave)

        'declare new array to hold loaded names.
        'Just to show you that the names are being loaded
        'correctly from the file
        Dim newArrayToHoldLoadedName As String() = Nothing

        'call the sub to load the names from the file.
        'That method returns an array containing the names,
        'just as they were when saved them.
        newArrayToHoldLoadedName = LoadNamesFromXML(Of String())(fileToSave)

        'print out names loaded from file
        For Each name As String In newArrayToHoldLoadedName

    End Sub

Here, we first create an array of names to save. We than store the file path to save to in a variable. Now, all we do is call the SaveToXML() method, passing in the object to save (our array of names), and our file path to save to.

Now, notice the (Of String()) part. This is telling the method what type of object we are trying to save. We are trying to save an array of strings. If you wanted to save an array of integers, you would use (Of Integer()), and it would still work perfectly (as long as you actually pass in an array of integers to save; not an array of strings).

All of the 'T's' in the method are therefore replaced with 'String()' when we call it.

We then create a new array variable to hold the array that will be created when we load the data back from the .XML file we created with the SaveToXML() method. You could just use the original 'namesToSave' variable, but I am using a brand new array variable to show you that the names are actually being loaded into a brand new array from the file.

To this method, we just pass in the file path of the file to read from, and the type of object we are expecting to get back from the method. We are expecting to get back an array of strings, of which contains the names we saved to file (as that is what we saved). Therefore, we specify this like this (Of String()) again. This means that the method returns an string array, as remember, all 'T's' will be replaced with 'String()'.

Finally, we print out the loaded values, to show that the values have correctly being loaded from the file. We are now free to use these loaded values in our program.

So there you have it. That may seem complicated, but trust me, it is easier than saving it manually yourself, as all the saving and loading is done for you.

I am saving and loading an array of strings. You will be saving and loading an array of integers or doubles for high scores, I would imagine.

Those two methods are all you need to save any serializable object type to XML. You call them as and when you need them in your code. SaveToXML() saves the object to a file, LoadToXML() creates a new object, filling it with the data saved to the file, and returns it for use in our program.

Any questions about the above example, then just ask :)

This post has been edited by [email protected]: 25 July 2011 - 09:09 AM

Was This Post Helpful? 4
  • +
  • -

#4 Psyguy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 365
  • Joined: 12-January 11

Re: High Score system

Posted 25 July 2011 - 08:52 AM

As a relatively new programmer, I found the example very very helpful! Thanks for the short turorial Coding. If I could double-click (or triple) the helpful button, I would.
Was This Post Helpful? 0
  • +
  • -

#5 Jester94   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-July 11

Re: High Score system

Posted 26 July 2011 - 02:10 AM

That tutorial was extremely helpful. Unfortunately it came after the project was due (I didn't think to include a high Score system till very late) but it was so good, that i worked through it anyway. Once again I am amazed at the quality of help on this forum. Like the poster above me, I wish I could just mash the reputation button all the way up to a million.

Thank you so very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1