|
Show Index |
|---|
First, make a list of the details or parameters about the objects in your game that
need to be saved.
For instance, if your game world is popuated with trees, you may need to know the
following details about each tree:
the size of the tree
the number of branches
the color of the tree
the shape of the leaves
the trees location (X, Y, Z)
An easy way to store that data would be by using a Type.
Here's an example of using a Type to store the details of
each tree:
Type Tree Field Size Field Branches Field Color$ Field Leaf Field Loc_X# Field Loc_Y# Field Loc_Z# End Type
Then, to save the data, you could use some code like the following:
Filename=WriteFile("savefolder\mygame.sav")
For thisObject.Tree = Each Tree
WriteInt(Filename, thisObject\Size)
WriteInt(Filename, thisObject\Branches)
WriteString(Filename, thisObject\Color$)
WriteInt(Filename, thisObject\Leaf)
WriteFloat(Filename, thisObject\Loc_X#)
WriteFloat(Filename, thisObject\Loc_Y#)
WriteFloat(Filename, thisObject\Loc_Z#)
Next
CloseFile(Filename)
Your game data could easily be loaded with similar commands for loading:
Filename=ReadFile("savefolder\mygame.sav")
Repeat
thisObject.Tree = New Tree
thisObject\Size = ReadInt(Filename)
thisObject\Branches=ReadInt(Filename)
thisObject\Color$ = ReadString$(Filename)
thisObject\Leaf = ReadInt(Filename)
thisObject\Loc_X# = ReadFloat#(Filename)
thisObject\Loc_Y# = ReadFloat#(Filename)
thisObject\Loc_Z# = ReadFloat#(Filename)
Until Eof(Filename)
Of course, I'm sure that your game would contain more than just trees, so you need to have
a way to save/load different objects.
You could assign a number to every different type of object that your game is going to
save/load, such as
1=rock, 2=tree, 3=house, 4=lightpost, etc
In order to save the different objects, you would just have to write the object type with each object like this:
Filename=WriteFile("savefolder\mygame.sav")
For thisRock.Rock = Each Rock
WriteShort(Filename, 1) ;1=rock
WriteInt(Filename, thisRock\Type)
WriteInt(Filename, thisRock\Size)
WriteString(Filename, thisRock\Color$)
WriteFloat(Filename, thisRock\Loc_X#)
WriteFloat(Filename, thisRock\Loc_Y#)
WriteFloat(Filename, thisRock\Loc_Z#)
Next
For thisTree.Tree = Each Tree
WriteShort(Filename, 2) ;2=tree
WriteInt(Filename, thisTree\Size)
WriteInt(Filename, thisTree\Branches)
WriteString(Filename, thisTree\Color$)
WriteInt(Filename, thisTree\Leaf)
WriteFloat(Filename, thisTree\Loc_X#)
WriteFloat(Filename, thisTree\Loc_Y#)
WriteFloat(Filename, thisTree\Loc_Z#)
Next
;etc.
CloseFile(Filename)
The first WriteShort in each For/Next loop is saving the type of the object.
(I'm using shorts instead of integers, because they are only two bytes long (as opposed to four bytes for integers). I doubt you would ever have nearly 2 billion different types of objects in your game, so why not save disk space and use shorts instead. Shorts still allow you to save 65,536 different kinds of objects. And if you don't have over 256 different objects, you could even use bytes.)
And then in order to load the file you could use a Select
statement to load in each individual object. (only the tree code is shown in the
example)
Filename=ReadFile("savefolder\mygame.sav")
Repeat
objecttype = ReadShort(Filename)
Select objecttype
Case 1
;load in rock
;loading code here...
Case 2
;load in tree
thisTree.Tree = New Tree
thisTree\Size=ReadInt(Filename)
thisTree\Branches=ReadInt(Filename)
thisTree\Color$=ReadString$(Filename)
thisTree\Leaf=ReadInt(Filename)
thisTree\Loc_X#=ReadFloat#(Filename)
thisTree\Loc_Y#=ReadFloat#(Filename)
thisTree\Loc_Z#=ReadFloat#(Filename)
Case 3
;load in house
;loading code here...
Case 4
;load in lightpost
;loading code here...
Case Default
RunTimeError "Unknown object encountered."
End Select
Until Eof (Filename)
If you're observant, you will have noticed that I added the Default command into the Select statement. If the savegame file is corrupted (or if you made a mistake while saving it), the loading sequence will stop and report an error if it encounters an object type that it doesn't recognize.
| If you've reached this page and there's no index on the left, Click here to show the Index Page | |