Saving to .dat

Adrammelech

Member
Joined
Mar 9, 2007
Messages
11
Programming Experience
Beginner
Im pretty new to vb.net and Im trying to make a simple RPG. Theres a few things I cant get to work just right. I have a character create form that I want to have saved into a .dat file as soon as the character clicks the button to create the character. The only problem is I want to have the program save it automatically(without the save box) to a .dat file that can hold all the information for as many characters as the user creates. I also want to be able to Sign in to the game with any of the characters in the .dat file.
 
.dat file doesn't mean anything, it's just a common extension used to save data, where program may save any data in any format they choose, in other words you define how you want to save your data. You haven't told what data you have and how it is currently organized. Also this topic is just as big as a full programming book, the different options are usually plain text or binary or Xml, each which can be read/written in many different ways. The answer to what you ask is basically you lack the basic knowledge of handling files in VB.Net, I don't see a reason the first learning book would miss at least a chapter about this that would give the beginner at least a start. If you could try to make an effort in looking into the topics of text-files and xml-files and binary-files and ask more specific question it would be much easier to help you.
 
I do have a book but it doesnt have enough to tell me how to do what im wanting to do. Ill try to explain what I have and what I need to get done better. When you create a character, you put in a name, password, and on the next form, it asks you for a class. When they finish creating a character, it needs to save: CharName, CharPassword, CharLevel, CharGold, CharClass, CharStrength, .... I need to know the easiest way to save all these variables so that when you start the game, It will let you sign in with every character and read all the stats for that character.
 
The absolute easiest you can do is to create a class for you character, declare a collection where you keep all character instances, and use serialization to load/save this collection to file.

For example basic class and collection (I also set up the file path here, you can do this any way you want really):
VB.NET:
<Serializable()> Public Class character
    Public CharName, CharPassword, CharLevel, CharGold, CharClass, CharStrength As String
End Class
 
Private characters As New List(Of character)
 
Private file As String = IO.Path.Combine(Application.LocalUserAppDataPath, "my.dat")
Here is the load and save serialization methods:
VB.NET:
Private Sub savedata()
    Dim ser As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Using fs As New IO.FileStream(file, FileMode.Create, FileAccess.Write)
        ser.Serialize(fs, characters)
    End Using
End Sub
 
Private Sub loaddata()
    If IO.File.Exists(file) Then
        Dim ser As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Using fs As New IO.FileStream(file, FileMode.Open, FileAccess.Read)
            characters = ser.Deserialize(fs)
        End Using
    End If
End Sub
Using the class and collection is same as with all other classes and collections, here I create a new character instance and set some properties for it and add it to the collection:
VB.NET:
Dim c As New character
c.CharName = "some CharName"
c.CharClass = "some CharClass"
c.CharGold = "some CharGold" 'etc
characters.Add(c)
Here I have a look at all the characters in collection:
VB.NET:
For Each ch As character In characters
    MsgBox(ch.CharName)
Next
Saving and loading file you do when needed, all you do is call loaddata() and savedata() methods.

Are you sure you didn't miss the page in your book that had these examples? ;)
 
i would use an xml file structure for this type of data
I just did in a matter of structure, but I saved it binary. The corresponding Xml serialization is just using the XmlSerializer instead:
VB.NET:
Private Sub savedataxml()
    Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(List(Of character)))
    Using fs As New IO.FileStream(file, FileMode.Create, FileAccess.Write)
        ser.Serialize(fs, characters)
    End Using
End Sub
 
Private Sub loaddataxml()
    If IO.File.Exists(file) Then
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(List(Of character)))
        Using fs As New IO.FileStream(file, FileMode.Open, FileAccess.Read)
            characters = ser.Deserialize(fs)
        End Using
    End If
End Sub
This would produce this kind of readable Xml data, but the data content is identical to what was serialized in binary format above.
HTML:
<?xml version="1.0"?>
<ArrayOfCharacter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <character>
    <CharName>some CharName</CharName>
    <CharGold>some CharGold</CharGold>
    <CharClass>some CharClass</CharClass>
  </character>
  <character>
    <CharName>some CharName</CharName>
    <CharGold>some CharGold</CharGold>
    <CharClass>some CharClass</CharClass>
  </character>
</ArrayOfCharacter>
 
Do you know where any good tutorials for this are cause I dont understand what a lot of this does and I dont have a clue where to put it.
 
Just put it in your form class for now.

Review the main forum index, there is a Book-Reading section where you should find much good suggested reading.
 
Back
Top