save games

Blitz3D Forums/Blitz3D Beginners Area/save games

I want to have highscores and save games in a program, but if you put it in variables you lose that data as soon as the program ends. Help!

Look at the file/stream docs in the BlitzDocs and then look at this code from the original blitzbasic2d samples:

Function savehighscores(name$)
;name$=name$+".txt"
savegame=WriteFile(name)
WriteLine savegame,"aristoids save file"
WriteLine savegame,CurrentDate()+" - "+CurrentTime()
For t=1 To 11
WriteLine savegame,hiscores(t)
WriteLine savegame,hinames(t)
WriteLine savegame,hidates(t)
WriteLine savegame,levelreached(t)
WriteLine savegame,control(t)
Next
WriteLine savegame,width
WriteLine savegame,height
WriteLine savegame,controltype
CloseFile(savegame)
End Function

;----------------------------
Function loadhighscores(name$)
;name$=name$+".txt"
savegame=ReadFile(name)
For no=1 To 2:nouse$=ReadLine$(savegame):Next
;While Not Eof(savegame)
;	Print ReadLine$(savegame)
;Wend
For t=1 To 11
hiscores(t)=ReadLine$(savegame):;Print "loader-"+t+hiscores(t)
hinames(t)=ReadLine$(savegame):;Print hinames$(t)
hidates(t)=ReadLine$(savegame)
levelreached(t)=ReadLine$(savegame)
control(t)=ReadLine$(savegame)
Next
width=ReadLine$(savegame)
height=ReadLine$(savegame)
controltype=ReadLine$(savegame)
If GraphicsHeight()<>height And GraphicsWidth()<>width Then Graphics width,height:SetBuffer BackBuffer()
CloseFile savegame
End Function

;-----------------------------
Function blankhighscores(name$)
;name$=name$+".txt"
savegame=WriteFile(name)
WriteLine savegame,"aristoids save file"
WriteLine savegame,CurrentDate()+" - "+CurrentTime()+" - default"
For t=1 To 11
WriteLine savegame,(11-t)*100
If Rnd(1,3)<2 Then WriteLine savegame,"aristides" Else WriteLine savegame,"vasso"
WriteLine savegame," "
WriteLine savegame,"0"
WriteLine savegame,0
Next
WriteLine savegame,width
WriteLine savegame,height
WriteLine savegame,controltype
CloseFile(savegame)
End Function

;-----------------------------
Function sorthighscores(file$,name$,score,levelreach)
loadhighscores(file)

done=0
t=1
Repeat
	If hiscores(t)<score Then
		done=1
		For e=10 To t Step -1
			hiscores(e+1)=hiscores(e)
			hinames(e+1)=hinames(e)
			hidates(e+1)=hidates(e)
			levelreached(e+1)=levelreached(e)
			control(e+1)=control(e)
		Next
		hiscores(t)=score
		hinames(t)=name
		hidates(t)=CurrentDate()
		levelreached(t)=levelreach
		control(t)=controltype
	EndIf
t=t+1
Until t=10 Or done=1

End Function

;-------------------------------
Function checkhighscore(thisfile$,sco)
loadhighscores(thisfile$)
highscoreplace=20
For t=10 To 1 Step -1
	If hiscores(t)<=sco Then highscoreplace=t
	;Print t+" "+hiscores(t)
Next
Return highscoreplace
End Function

;---------------------------------
Function entername(startname$,score)
yourname$=startname
PlaySound buzz_sound

flush=10
Repeat
flush=GetKey()
Print flush
Until flush=0

While Not (KeyDown(1) Or KeyDown(28))
Cls

a=GetKey()
If a>=32 And a<=122 And Len(yourname)<15
	yourname=yourname+Chr$(a)
EndIf
If (KeyHit(14) Or KeyHit(211)) And Len(yourname)>0 Then yourname=Left$(yourname,Len(yourname)-1)

Color 0,10,70
;SetFont hugefont
Text 60,200,yourname
Color 200,200,255
Text 60,170,yourname




Text 40,100,"you have achieved a high score"
Text 40,115,"please enter your name:"

If Rnd(1000)>970 Then makeexplosion(Rnd(width),Rnd(height),Rnd(100)+50,Rnd(300)+80)
UpdateExplosion()
	Nowtimer=MilliSecs()-Lasttimer
	If nowtimer<9
		Repeat:Until MilliSecs()-Lasttimer>=9
	EndIf
	Lasttimer=MilliSecs()

Flip
Wend
;Return yourname ;doesnt work!
End Function


Just ask for help so that someone (if not me) can help u. I don't totally understand it so you should try to get more help.

Check out the "How to save and load game data" link on my programming tutorial.

Yours is better WolRon thanks that helped me some 2