Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am new to VB.Net. As a self-tutorial, I decided to create a simple text based puzzle game to teach myself about VB.Net, objects, forms, etc. I figured that I could use the base knowledge I have (minor stuff from VB6), and when I get stumped, just research the problem I don't understand. The game itself was only intended to be a very small project (or is that now 'solution' in VB.Net?); just enough to get my feet wet with VB.Net.

The game took off, and has come quite far. I am still new to VB.Net, so most of my code would probably be laughed at by most of you, but I am quite proud of it.

I decided since the game can not be completed in under an hour (unless you already know all the secrets and puzzles), I would like a way for the user of the game to save their game, and then be able to load it at another time.

My question is: would this best be handled by creating a database (which I have no understanding of), or by creating a simple text file(s)? I would rather use a text file, as I do not know a thing about databases.

My game is a very basic VB.Net windows application. Just a couple of forms, and some objects. However, the variables I created are constantly changing, such as the player's score, number of puzzles completed, items discovered. I am setting these all to variables in the code, and continuously updating them when a player solves certain puzzles, such as

PlayersScore = PlayersScore + 500

when they solve a puzzle (yes - I already read about the shortcut I could use for this small piece of code - I believe it was += - but like I said, I am doing this as a basic tutorial).

How can I save all these variables (about 46 all together), so that when the player reloads a game, they are prepopulated to the same variable names (such as PlayersScore = 1500), the next time they start up the game?

I tried to search for writing to a text file on the Internet, but all I keep finding is how to take a large amount of text, write it to a text file, than read it back in later. This is not what I need. I need a way to save variables, not a bunch of text.

Any help would be greatly appreciated. Thanks!

  • *Experts*
Posted

This is a perfect situation where the System.Xml.Serialization

namespace comes into play. Using XML Serialization, you can

easily go from a class, to an XML file, and back again.

 

Before you can use it, however, you need to turn all your

variables into properties of a single class. Then you can just load

and save this class as needed.

 

You will most likely want to encrypt the values before saving, and

edcrypt them when you load them, as it is insanely easy to

change them just by opening up the XML file in notepad. This

encryption can be done with classes in the

System.Security.Cryptography namespace.

 

For information on XML Serialization:

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/Cpqstart/html/cpsmpnetsamples-howtoxmlserialization.htm#cpsmpreadwritexmlsample[/mshelp]

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.htm[/mshelp]

 

And for info on encryption:

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/Cpqstart/html/cpsmpnetsamples-howtocryptography.htm[/mshelp]

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemSecurityCryptography.htm[/mshelp]

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...