PDA

Click to See Complete Forum and Search --> : Game Question


Gorf
Apr 25th, 2001, 09:28 PM
I was wondering if anyone knows the code to implement a save and load feature into a game, plz post a snippet of code or detailed example, i know a fair amount but adviously not that much.

Please don't make it some incredibly hard small code, id'd rather have a huge easy to understand code.

Gorf

PsychoMark
Apr 26th, 2001, 03:57 AM
http://forums.vb-world.net/showthread.php?s=&threadid=65908&highlight=save+games

plenderj
Apr 26th, 2001, 04:06 AM
Well you basically just have to know what you want to load/save.
Here is a sample :


Option Explicit

'Code assumes save.game is in following format :
'
'name =Blah Blah Blah
'age =334
'TotalKills =3493
'ExperienceLevel=324
'

Private thePlayer As playerType
Private Type playerType
Name As String
Age As Integer
TotalKills As Long
ExperienceLevel As Long
'and others if applicable
End Type

Private Sub LoadGame()
Dim varTemp As String
Open App.Path & "\game.save" For Input As #1
Do Until EOF(1)
DoEvents
Line Input #1, varTemp
Select Case Left(varTemp, 16)
Case "name =":
thePlayer.Name = Mid(varTemp, 17)
Case "age =":
thePlayer.Age = Mid(varTemp, 17)
Case "TotalKills =":
thePlayer.TotalKills = Mid(varTemp, 17)
Case "ExperienceLevel=":
thePlayer.ExperienceLevel = Mid(varTemp, 17)
End Select
Loop
Close #1
MsgBox "Game Loaded !"
End Sub

Private Sub SaveGame()
Open App.Path & "\game.save" For Output As #1
With thePlayer
Print #1, "name =" & .Name
Print #1, "age =" & .Age
Print #1, "TotalKills =" & .TotalKills
Print #1, "ExperienceLevel=" & .ExperienceLevel
End With
Close #1
End Sub

PsychoMark
Apr 26th, 2001, 04:09 AM
plenderj, if you've followed the link you'll notice that I'm using the UDT too, but instead of manually writing and reading each line, I'm writing the whole UDT at once, which is much easier...

plenderj
Apr 26th, 2001, 04:13 AM
Sorry, It took me so long to write the post I had never read yours ;)
Anyway yeah I had never actually use UDTs like that before ... I never even knew you could.

Well we all learn things ;)