# Visual Basic > Games and Graphics Programming > Game Demos >  [VB.Net]RPG Game Accounts

## Odin the Wise

Hello, I am making a VB.Net RPG and I would like to set it up so there are Log-Ins, I want the program to read(write/edit) text from a Text file and interpret the text as Usable data by my program. What I need to know is how would I (First)create a Directory in my program from which I can place/retrieve said Text Files and (Second) Read the text so the program can interpret it / Write to the file to save character information / and Edit Text to modify stats and such and then Save the Text File.

I was hoping this was possible my using Resources.TextFile but apparently, those are meant to NOT be edited, which is good, because I dont want these said text files to be (easily at least) editable by outside sources, only by my programs Runtime Code

Thanks in Advance for any information!

----------


## Odin the Wise

Anybody? And Nevermind the Encrypt

----------


## FunkyDexter

It sounds like you're just after some pretty straight forward file handling.  I'd suggest reading up on FileSystemObject which should provide everything you need.

----------


## Odin the Wise

> It sounds like you're just after some pretty straight forward file handling.  I'd suggest reading up on FileSystemObject which should provide everything you need.


Hey FunckyDexter, I'm not sure I want to go along with Updating Text Files anylonger and just implement a Database into my program. The Only problem with that is, I dont have Access, and I want a database with multiple tables to save space. And when ever I try to create a database and Tables inside VB.net, It won't let me edit/add rows to the table. So I'm stuck with an empty Table. Any Idea where I can get a FREE database program that allows for multiple Tables?

----------


## Jacob Roman

Why on earth would you want to use Access to handle saving databases? The beauty of game programming is saving the data in your own format based on the structure you created, save it as a binary file using whatever extention you want, and as an option since you don't really have to do it, you can also encrypt it and decrypt it. True game programmers never use database files that run in things like Access.  :Wink:  That would almost would be as bad as making a game using pictureboxes and command buttons  :EEK!:

----------


## dday9

I agree with Jacob Roman here. Why not create a class and then use the built in .NET binary serialization class? This will be simpler for you because:
Classes help keep the code organizedThe serialization is already built for youBinary serialization with give you some encryption

----------


## Odin the Wise

Ok, so before I respond to this, let me set up for you what my text file looks like:



```
Username = Password :: Name: CharacterName $$ Race: CharacterRace $$ Class: CharacterClass $$ etc, etc.. ::EndofCharacter1:: ::EndofAccount::
```

And I was trying to use File.ReadAllText and File.WriteAllText to edit the lines by loading it into an Array, and splitting the aray into lines, editing the line, then butting them back together. But apparently I don't know how to do that because it won't work. I get an error stating that VB can't access the file because another program is using it. 

I, too, agree with you guys. I don't want to use a database. I wan't to use a custom file, butI cant seem to get it to work. I'll look up this binary serialization stuff and see what I can do.

----------


## dday9

Here is a quick example that I worked up for you:


```
Imports System.Runtime.Serialization.Formatters.Binary
Imports System
Imports System.Runtime.Serialization.Formatters.Binary

Public Module Module1

    Public Sub Main()
        Console.WriteLine("Hello World")
    End Sub
End Module

Public Class Character

    Private _username As String
    Public Property Username() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            If _username <> value Then
                _username = value
                Me.OnUsernameChanged()
            End If
        End Set
    End Property

    Private _password As String
    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(ByVal value As String)
            If _password <> value Then
                _password = value
                Me.OnPasswordChanged()
            End If
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            If _name <> value Then
                _name = value
                Me.OnNameChanged()
            End If
        End Set
    End Property

    Protected Overridable Sub OnUsernameChanged()
        RaiseEvent UsernameChanged(Me, EventArgs.Empty)
    End Sub

    Protected Overridable Sub OnPasswordChanged()
        RaiseEvent PasswordChanged(Me, EventArgs.Empty)
    End Sub

    Protected Overridable Sub OnNameChanged()
        RaiseEvent NameChanged(Me, EventArgs.Empty)
    End Sub

    Public Event UsernameChanged(ByVal sender As Object, ByVal e As EventArgs)
    Public Event PasswordChanged(ByVal sender As Object, ByVal e As EventArgs)
    Public Event NameChanged(ByVal sender As Object, ByVal e As EventArgs)

    Public Sub Save(ByVal path As String)
        Using stream As IO.FileStream = IO.File.Create(path)
            Dim formatter As BinaryFormatter = New BinaryFormatter()
            formatter.Serialize(stream, Me)
        End Using
    End Sub

    Public Function Restore(ByVal path As String) As Character
        Dim loadedCharacter As Character = Nothing
        Using stream As IO.FileStream = IO.File.OpenRead(path)
            Dim formatter As BinaryFormatter = New BinaryFormatter()
            loadedCharacter = DirectCast(formatter.Deserialize(stream), Character)
        End Using

        Return loadedCharacter
    End Function
End Class
```

Obviously you would create more properties for race, class, and the etc's. I just didn't know what types those would be.

----------


## Odin the Wise

Hey dday, thanks for that, but theres a small problem. I wouldn't consider my self "new" by anymeans, just a little.... rusty. I havn't worked with VB in over 2 years before I started this project, so most of the stuff I've learned is from videos and core knowledge and common snese lol. So with that said, would you be so kind as to explain what this does? It looks like it just updates whenever stats change, but how do I call that specific function(do this when UserNameChanged, or PasswordChanged, etc..)

Any help would be extremely appreciated. I've also decided to put this project off for a little bit to move to something a little less complicated. I'm making an 2d TopView zombie shooter. It's be kinda open-world with all kinds of weapons. NO CURRENCY! When the zombie apocolypse happens, do you really think people are going to be using Money? No.

----------


## dday9

Yeah, I'll breakdown just the Username property as all the others do exactly the same thing(only for their specific property). First let's start out with this chunk of code:


```
Private _username As String
Public Property Username() As String
	Get
		Return _username
	End Get
	Set(ByVal value As String)
		If _username <> value Then
			_username = value
			Me.OnUsernameChanged()
		End If
	End Set
End Property
```

What it does is first declare a private variable named __username_, this will be the object that we work with inside of the class. Next it declares a property named _Username_, this will be the object that we work with outside of the class. 

Next we setup the Get/Set methods. The Get function is simple in that all that it does is return the value of the variable __username_ when somebody wants to get the value of the property _Username_ outside of the class. If you really wanted to screw with somebody, return a different variable other than the one it's designed to represent! The Set sub seems a little bit complicated, but it's really not. Basically what it does is it checks if the __username_ variable is different then the value it's to be set to. If it is, then set the __username_ variable to the user desired value and call the OnUsernameChanged method. If it isn't, then don't do anything at all because the value really did not change.

Now let's move onto the OnUsernameChanged method and the UsernameChanged event:


```
Protected Overridable Sub OnUsernameChanged()
	RaiseEvent UsernameChanged(Me, EventArgs.Empty)
End Sub

Public Event UsernameChanged(ByVal sender As Object, ByVal e As EventArgs)
```

The OnUsernameChanged method is protected and overridable meaning that it is accessible only from within it's own class or from a derived class as well as that a programmer override the method. Inside the sub all we do is raise the UsernameChanged event while passing the current class as the sender and pass empty event arguments.

The UsernameChanged event is used to be raised when the __username_ variable is changed. The parameters follow the normal standards of other events. Take a look at the following button click event:


```
Private Sub btnFoo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFoo.Click

End Sub
```

Notice how it passes the same exact handles? This means that we can actually handle both a button's click event as well as a Character's UsernameChanged event all in the same method:


```
Private Sub Anyname(ByVal sender As Object, ByVal e As EventArgs) Handles btnFoo.Click, characterFoo.UsernameChanged

End Sub
```

That is how the Properties and their Events work.

----------


## Odin the Wise

Thanks for that dday, I appreciate you taking the time to explain that  :Big Grin: 

Now I have a very serious and heartbreaking problem. My laptop has been through hell and back and has acquired malware and adware. So I system restored it to factory settings, resulting in the complete destruction of my projects D': So I will begin anew with all this information you all have shared with me. Thanks alot and I'm sure I'll be back with more problems!

----------


## Jacob Roman

Also be sure you are using a graphical library such as DirectX so it has a more professional look.  :Wink: 

If you need help with spell icons having that World of Warcraft "Cooldown" effect, take a look at my World of Warcraft Spell Cooldown effect in my signature, along with other sample DirectX applications to get a feel on how to get this badboy going. Who knows, maybe it'll end up looking like Bosskillers. Its the best RPG I've ever made considering that I literally made WoW in 2D.

----------


## Odin the Wise

Thanks, I'll keep that in mind. I dont know how to use DirectX, but im sure I can learn.

----------

