I want to make a highscoreboard for my game, but how can I save or load data from a text file, so the highscorerecords will be saved?
Yes I am using vb 2005 express edition
Then it's really easy!
To read all the data from a text file:
Code:Try Dim filePath As String = "C:\temp\thefile.txt" Dim sr As New System.IO.StreamReader(filePath) Dim theText As String = sr.ReadToEnd() sr.Close() sr.Dispose() 'At this point, the string variable theText contains the text from the text file. Catch exc As Exception MessageBox.Show("An error has occured: " & exc.Message) End Try
And how would I make a scoreboard of that? Let's say: Tom scored 100 points, How would I do that so when you restart the programm: On 1 label will be the name displayed and on the other laber the score
Ah. In this case a text file might not be enough. You see, you need to store the data in the text file in such a way that the program can easily read it back into the program again.
For example, you could store them like this:
As you can see, each new line is a new player, and the values are separated by commas. We could parse out the data, but there is a problem - how would we arrange them on order of points?Code:Tom,100 Bill,200 Charlie,300
Maybe an XML file would be more useful. If we stored it like this:
You see? If we do it like this, I can show you how to use the System.XML namespace to automatically read in the data and extract the rows, just like a normal database.Code:<game> <highscores> <score name="Tom" points="100" /> <score name="Bill" points="200" /> <score name="Charlie" points="300" /> </highscores> </game>
However, you would still need to sort them in order of points. It's your choice!
Ok I want to do that, how can I add this XML file to it, you really got to help me with this because I have never worked with files actually
That's OK - it's a big task to master working with files. However, it just got a weeny bit easier.
You see, the .NET Framework provides a namespace called System.Xml. It allows you to read XML files without having to actually examine every single character and work out whether it is a tag or not etc.
Are you familiar with XML? If not, see my example. It is a way of storing data. The best bit is, it is still just a text file, so you can manipulate it just like a normal text file.
First, we will work out how to read text files - writing them is more complex. Therefore, create a new text file - call it game.xml or something. Write the code for the XML file I gave you into it, so that the game.xml file contains the text:
OK. Now go back to your program. I don't know where you want to load the data, but maybe when the window is loaded? Anyway, make the following method (and pardon me if I make a mistake, I'm used to C# not VB):Code:<game> <highscores> <score name="Tom" points="100" /> <score name="Bill" points="200" /> <score name="Charlie" points="300" /> </highscores> </game>
OK. After this I need to start explaining the code, so I'm going to write it here. Type all the following code after the comment (just before End Sub):Code:Private Sub LoadHighScores() 'This is the XML file. Dim filePath As String = "C:\temp\game.xml" Dim sr As New StreamReader(filePath) Dim rawXml As String = sr.ReadToEnd() sr.Close sr.Dispose 'Here, rawXml contains the text from the file. End Sub
First, we create a new XmlDocument, that will hold the file:
Now we have loaded the data, we can use it. Here's an example:Code:Dim doc As New System.Xml.XmlDocument() doc.LoadXml(rawXml)
I think this is what you do in VB, but if you get any errors, let me know.Code:For Each node As System.Xml.XmlNode In doc.SelectNodes("game/highscores") MessageBox.Show("The player called " & node.Attributes("name").Value & " got the score " & node.Attributes("points").Value & ".") Next![]()
You solved some of the problems I had with xml. Thanks Xav
Posted via CodeCall Mobile
It says: Type 'StreamReader' Is not defined
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks