Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving problems

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
I'm a beginner in VB and I need to let the user save their progress in a game. I have posted this before, but I'm still having trouble. I need to save the users...]

1. Name as string * 10
2. Gold as integer
3. MaxHP as integer
4. Progress (as ?)
5. Level as integer

Progress is which battles the user has fought and won against the computer (there are a few characters that they have to fight). Can anyone help me?
 
I looked it up on the internet and found some stuff, but it still didn't work. This is pretty much the last part of the project I'm doing, and the hardest.
 
Hi,

I can think of 3 solutions:

1) Save the info in a flat file (if your lazy)
--------------------------------------------------
MyFile = "c:\tmp\test.ifo"
OutFile=freefile
if dir(MyFile)="" then
open MyFile for output as #OutFile
else
open MYFile for append as #OutFile
end if
print #OutFile Name & "," & cstr(Gold) & "," & ....
close #OutFile
--------------------------------------------------
Read the file into an array line by line when you need the saved info.

2)
Use the registry to save the info (if there are not that many users)
Create a string of the data you want to save (like above) and use SaveSetting to save it to the registry. Use Getsetting and split to retreive the info.

3) Store the info in a database (if there are many users)

Good Luck
Sunaj
 
Have a loo at the following example, and see if it is of any value ( to use this example create a form with two command buttons, and add a reference to the Microsoft Scripting Runtime):

Option Explicit

Private Type tGameStatus
Name As String * 10
Gold As Integer
MaxHp As Integer
Progress As Long ' My choice - you choose what you need...
Level As Integer
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private GameStatus As tGameStatus

Private Sub Command1_Click()

' For the purposes of the example fill the UDT
With GameStatus
.Name = "Mike"
.Gold = 112
.MaxHp = 45
.Progress = 100
.Level = 25
End With

SaveGameStatus ' Now save it...

End Sub

Private Sub GetGameStatus()

Dim fso As FileSystemObject
Dim txtFile As TextStream
Dim strGet As String

strGet = Space(LenB(GameStatus)) 'Allocate buffer space

Set fso = New FileSystemObject
Set txtFile = fso_OpenTextFile("c:\gamestate.dat")
strGet = txtFile.ReadAll ' read file contents into buffer
CopyMemory ByVal GameStatus, ByVal strGet, LenB(GameStatus) ' Copy buffer into UDT

' Clean up
Set txtFile = Nothing
Set fso = Nothing
End Sub

Private Sub SaveGameStatus()

Dim fso As FileSystemObject
Dim txtFile As TextStream

Dim strSave As String


strSave = Space(LenB(GameStatus)) ' Allocate space

CopyMemory ByVal strSave, ByVal GameStatus, LenB(GameStatus) ' Put the UDT into a buffer (represented by a string)
Set fso = New FileSystemObject
Set txtFile = fso.CreateTextFile("c:\gamestate.dat", True)
txtFile.Write strSave ' Save the buffer

' Clean up
Set txtFile = Nothing
Set fso = Nothing
End Sub

Private Sub Command2_Click()
GetGameStatus
With GameStatus
Debug.Print .Name
Debug.Print .Gold
Debug.Print .MaxHp
Debug.Print .Progress
Debug.Print .Level
End With
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top