Click to See Complete Forum and Search --> : Load/Save a game
overseer21
Jun 16th, 2001, 05:22 PM
Hi there!I'm making the Hanoi game on vb,but I don't know how can I make a procedure so that the player can save his game and load it at anytime.I'll appreciate if anyone can help me.Thanks.
FinnPanther
Jun 17th, 2001, 07:53 AM
Hmm... what kind of game it is?
I think the easiest way to make a save is something like this:
'cmdSave.Click
open "d:\path\save.ext" for output as #1
print #1, strVar1
print #1, strVar2
print #1, strVar3
print #1, intPlayerXPos
print #1, intPlayerYPos
'use the write # statement for boolean values
close #1
'cmdLoad.Click
open "d:\path\save.ext" for input as #1
line input #1, strVar1 'not sure about this, check the msdn help
line input #1, strVar2
line input #1, strVar3
line input #1, intPlayerXPos
line input #1, intPlayerYPos
close #1
If you'd like, I could put here a save/load procedure from a game I'm working on... It works like above, but in addition it crypts the save file so it can't be edited :p
BTW: Again I'm sorry I can't use that vbcode function here... :confused:
Have to learn someday.
PsychoMark
Jun 18th, 2001, 03:07 AM
http://psprogramming.multimania.com/index.php?page=tutorials/vb/file/udtbinary.html
That is, in my opinion, the easiest yet most flexible way of loading and saving game data.
Fox
Jun 18th, 2001, 04:30 AM
Yep. Heres some sample code: (use in a module)
'Types
Type tREPLACE_NAME
Value1 As Long
Value2 As String
Value3 As Integer
End Type
'Variables
Public REPLACE_NAMECount As Long
Public REPLACE_NAME() As tREPLACE_NAME
Sub LoadData(iFileName As String)
Dim FileNumber As Integer
'Reset
Reset
'Verify filename
If Not Exists(iFileName) Then
'Error: File not found
Exit Sub
End If
'Read file
FileNumber = FreeFile
Open iFileName For Binary As FileNumber
'Count
Get FileNumber, , REPLACE_NAMECount
'Data
If REPLACE_NAMECount > -1 Then
ReDim REPLACE_NAME(REPLACE_NAMECount)
Get FileNumber, , REPLACE_NAME
End If
Close FileNumber
End Sub
Sub SaveData(iFileName As String)
Dim FileNumber As Integer
'Delete file
If Exists(iFileName) Then: Kill iFileName
'Write file
FileNumber = FreeFile
Open iFileName For Binary As FileNumber
'Count
Put FileNumber, , REPLACE_NAMECount
'Data
If REPLACE_NAMECount > -1 Then
Put FileNumber, , REPLACE_NAME
End If
Close FileNumber
End Sub
DaoK
Jun 18th, 2001, 06:36 AM
CAnt we just create a new "extension" like .sav and put stuff on it like in Diablo or other game like that ?
Fox
Jun 18th, 2001, 08:07 AM
What's the problem?
Use my function like this:
SaveData "test.sav"
Extensions do say nothing about the file contents/format. In windows the header is important but if you're making savegames you can just use a function like mine and put the data in... you can chose any extension you want, even .exe .com .txt -" .savegame of my file" - whatever
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.