Save all Global Variables

mattkw80

Well-known member
Joined
Jan 3, 2008
Messages
49
Location
Ontario, Canada
Programming Experience
Beginner
Hey Everybody,


I've got a RPG style game I am putting together - mostly to teach myself vb.net. I am using alot of global variables to hold game values.

ie:

Health
Money
Attack
Defence
Monster1_Health
Monster2_Type
monster2_XP_Points


The reason these are global variables, is because I need these values always available to many different forums.

My question is -- I am trying to make a "SAVE" and "LOAD" the game's state. Is there a simple way to save the values of all my global variables, without having to type everysingle variable out. Is there anyway to call upon all my global variables at one time, and possible save/load them to a text file ?


Thanks in Advance.
 
If you want to save multiple values then you have save each individual value at some point. The only way to avoid writing a line for each is to have them in a collection that you loop through, but that has it's own considerations. There's only six values there anyway.
 
I'd collect them into a class/structure:
VB.NET:
Public Structure Info
    Public Health, Money As Integer
End Structure
If I understand you correctly about 'global' then you can also keep an instance of the Info structure globally:
VB.NET:
Module Globals
    Public game As Info
End Module
So now it can be accessed from anywhere like this:
VB.NET:
game.Health = 10
game.Money = 30
To the point, an Info structure object can be serialized to and from file with very little code, it only takes a XmlSerializer and a file stream. Here is two general utility methods Load/Save that can be used:
VB.NET:
Public Class Utils
    Public Shared Sub Save(ByVal o As Object, ByVal path As String)
        Dim ser As New Xml.Serialization.XmlSerializer(o.GetType)
        Using file As New IO.FileStream(path, IO.FileMode.Create)
            ser.Serialize(file, o)
        End Using
    End Sub

    Public Shared Function Load(Of T)(ByVal path As String) As T
        Dim ser As New Xml.Serialization.XmlSerializer(GetType(T))
        Using file As New IO.FileStream(path, IO.FileMode.Open)
            Return CType(ser.Deserialize(file), T)
        End Using
    End Function
End Class
To save the game Info object:
VB.NET:
Utils.Save(game, "game.xml")
To load the game Info object:
VB.NET:
game = Utils.Load(Of Info)("game.xml")
The file output in this case is:
HTML:
<?xml version="1.0" encoding="utf-8"?>
<Info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Health>10</Health>
  <Money>30</Money>
</Info>
:confused:
Now what if you decide to add another field to the Info structure, for example Attack? Well, the loading still works, Attack was initialized to 0 and ignored by the deserializer. When saving the Attack node was automatically added also. And if a field was removed from the structure, also no problem. So this is also extensible.
:mad:
What if you were to keep a set of Info objects, for example one for each player? If these still were globals the module could now have this variable, an array of Info objects:
VB.NET:
Public players(1) As Info
In code you might set:
VB.NET:
players(0).Health = 10
players(1).Health = 15
To save the players:
VB.NET:
Utils.Save(players, "players.xml")
To load the players:
VB.NET:
players = Utils.Load(Of Info())("players.xml")
The sample file output:
HTML:
<?xml version="1.0"?>
<ArrayOfInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Info>
    <Health>10</Health>
    <Money>0</Money>
    <Attack>0</Attack>
  </Info>
  <Info>
    <Health>15</Health>
    <Money>0</Money>
    <Attack>0</Attack>
  </Info>
</ArrayOfInfo>
:D
As you see serialization is very flexable and easy to use with little code required to transfer object data across a stream.
Using classes/structures allows you to keep a set of properties and handle them as a single object.
 
Back
Top