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?
20 replies to this topic
#1
Posted 18 May 2008 - 01:12 AM
|
|
|
#3
Posted 18 May 2008 - 04:53 AM
Yes I am using vb 2005 express edition
#4
Posted 18 May 2008 - 04:59 AM
Then it's really easy!
To read all the data from a text file:
To read all the data from a text file:
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
#5
Posted 18 May 2008 - 05:01 AM
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
#6
Posted 18 May 2008 - 05:48 AM
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?
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.
However, you would still need to sort them in order of points. It's your choice!
For example, you could store them like this:
Tom,100 Bill,200 Charlie,300
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?
Maybe an XML file would be more useful. If we stored it like this:
<game> <highscores> <score name="Tom" points="100" /> <score name="Bill" points="200" /> <score name="Charlie" points="300" /> </highscores> </game>
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.
However, you would still need to sort them in order of points. It's your choice!
#7
Posted 18 May 2008 - 05:51 AM
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
#8
Posted 18 May 2008 - 06:09 AM
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):
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):
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:
I think this is what you do in VB, but if you get any errors, let me know. :)
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:
<game> <highscores> <score name="Tom" points="100" /> <score name="Bill" points="200" /> <score name="Charlie" points="300" /> </highscores> </game>
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):
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
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):
First, we create a new XmlDocument, that will hold the file:
Dim doc As New System.Xml.XmlDocument() doc.LoadXml(rawXml)
Now we have loaded the data, we can use it. Here's an example:
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
I think this is what you do in VB, but if you get any errors, let me know. :)
#9
Posted 18 May 2008 - 06:15 AM
#10
Posted 18 May 2008 - 06:17 AM
It says: Type 'StreamReader' Is not defined
#11
Posted 18 May 2008 - 06:22 AM
Sorry. It is part of the System.IO namespace. You can do one of two things:
@ Go to the Project Properties page, find the References bit and tick the System.Xml thingy. Or, if you can't be bothered:
@ Just replace StreamReader with System.IO.StreamReader. :)
@ Go to the Project Properties page, find the References bit and tick the System.Xml thingy. Or, if you can't be bothered:
@ Just replace StreamReader with System.IO.StreamReader. :)
#12
Posted 18 May 2008 - 06:33 AM
It doesn't work for me, but actually what I ment is that you got a label on the form where the name will be displayed and a label with how many points a person has, and that you can save your score.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









