Saving a Game

Blitz3D Forums/Blitz3D Beginners Area/Saving a Game

I figured out how to get the results I wanted So I'm sharing it with the rest of guys for your input on it.

AppTitle "My little box"

Graphics3D 800,600,32,2

SetBuffer BackBuffer()
Global message$ = "move the boxes... '[' and ']' to change box"
Type test
	Field x
	Field y
	Field z
	Field name$
	Field shape
	Field anim
End Type

For count = 1 To 1
	n.test = New test
	n\x = Rand(10)
	n\y = Rand(10)
	n\z = Rand(10)+10
	n\name$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65)
	n\shape = LoadAnimMesh("BOX.b3d") 
    Animate n\shape,1,2,0,100
	EntityColor n\shape, count*50, 0,0
Next

camera=CreateCamera()

light=CreateLight()

RotateEntity light,0,0,90

Global reload.test
Global control.test = Last test
maxshape = control\shape
control.test = First test
minshape = control\shape

;box=LoadAnimMesh("BOX.b3d")
ScaleEntity n\shape,.1,.1,.1 
PositionEntity n\shape,0,-.9000,5 
TranslateEntity n\shape,30,-.70,-.70
EntityShininess n\shape,1
EntityBlend n\shape,1
SetAnimKey n\shape,15,True,True,True
AddAnimSeq(n\shape,15)
EntityAutoFade n\shape,80,70

Repeat

If KeyDown(200)=True Then control\y = control\y + 1
If KeyDown(203)=True Then control\x = control\x - 1
If KeyDown(205)=True Then control\x = control\x + 1
If KeyDown(208)=True Then control\y = control\y - 1

If (KeyHit(26)=True And control\shape <> minshape) Then control = Before control
If (KeyHit(27)=True And control\shape <> maxshape) Then control = After control

For update.test = Each test
    PositionEntity update\shape, update\x, update\y, update\z
    EntityColor update\shape,count*50, 0,0
Next

If KeyHit(64) Then save
If KeyHit(65) Then load 

UpdateWorld 
RenderWorld 

Text 20, 20, message$
Flip 

Until KeyHit(1)
End 

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function save()

fileout = WriteFile("Shay1.SM")
For n.test = Each test
	Print Str$(n)
	WriteString (fileout, Str$(n))
Next
CloseFile( fileout ) 
Cls 
message$ = "game data written to file..."
FlushKeys 

End Function
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function load()
Cls
For destroy.test = Each test
    FreeEntity destroy\shape
    Delete destroy
Next

filein = ReadFile("Shay1.SM") 
While Not Eof(filein)
      Read1$ = ReadString$( filein ) 
      If Read1$<>"" Then
        reload.test = RestoreTestInfo(Read1$)
      EndIf
Wend 
DebugLog "finished with loaded file"
FlushKeys
control = First test
End Function 
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function RestoreTestInfo.test( SavedString$ )
	message$ =  "Read from file --> " + SavedString$ + "    ...     "
	DebugLog message$   ; typical type looks like ... [-3,-4,14,"LQT",18898368]
	SavedString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove end square brackets
	
	firstcomma = Instr(SavedString$, ",")
	xvalue% = Left$(SavedString$, firstcomma-1)   ; convert first value (up to comma) to an int
	
	SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to 1st comma

	firstcomma = Instr(SavedString$, ",")
	yvalue% = Left$(SavedString$, firstcomma-1)  ; convert up to new first comma to another int
	SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to new 1st comma

	
	firstcomma = Instr(SavedString$, ",")
	zvalue% = Left$(SavedString$, firstcomma-1)  ; convert up to new first comma to another int

	
	SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to new 1st comma
	ThirdString$ = Mid$( SavedString$, 2, Len(SavedString$))     ; remove first quote from string

	firstcomma = Instr(SavedString$, ",")
	ThirdString$ = Mid$(SavedString$, 2, firstcomma-3)
	
	message$ = message$ + "x: " + xvalue + "   y: " + yvalue + "   z: " + zvalue + "     name: " + ThirdString$
	DebugLog message$

	reloaded.test = New test			; make a new type
	reloaded\x = xvalue%			    ; and assign the
	reloaded\y = yvalue%		     	; reloaded values
	reloaded\z = zvalue%
	reloaded\name$ = ThirdString$		; to the fields
	reloaded\shape = LoadAnimMesh("BOX.b3d")
	ScaleEntity reloaded\shape,.1,.1,.1  
	Animate reloaded\shape,1,2,0,100
	Return (reloaded)
	
End Function
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++