saving

BlitzPlus Forums/BlitzPlus Programming/saving

Kyler(Posted 2007) [#1]
How would i make it where this code saves and load each type, so you can save, move them all, load, and they return to all of there own postions where they were saved?

If you even understood that, the code is below. It only makes one dot return to its original position, instead of all of them. Sorry its not formatted correctly. I dont know how to make the code have its own spiffy little box. thnx.








Type p
Field x,y
End Type

p.p = New p

p\x= 100
p\y = 200



p.p = New p

p\x= 170
p\y = 249


p.p = New p

p\x= 500
p\y = 100




Graphics 800,600



While Not KeyHit(1)
Cls



For p.p = Each p



;loads the dots position
If KeyHit(2)
; Open a file to write to
fileout = WriteFile("mydata.dat")

; Write the information to the file
WriteInt( fileout, p\x)
WriteInt( fileout, p\y )


; Close the file
CloseFile( fileout )
EndIf





;loads the dots position
If KeyHit(3)
; Open a file to write to
filein = ReadFile("mydata.dat")

; Write the information to the file
p\x=ReadInt( filein )
p\y=ReadInt( filein )


; Close the file
CloseFile( filein )
EndIf





;draws the dot
Oval p\x,p\y,10,10


;moves each dot around
If KeyDown(200)
p\y=p\y-2
EndIf

If KeyDown(208)
p\y=p\y+2
EndIf

If KeyDown(203)
p\x=p\x-2
EndIf

If KeyDown(205)
p\x=p\x+2
EndIf



Next

Flip
Wend


Matty(Posted 2007) [#2]

function SaveAllPTypeToFile(FileName$)

outfile=writefile (FileName$)
if outfile=0 then runtimeerror("Invalid File Name Specified")

for P.P=each P
      count=count+1
next
writeline outfile,count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

for p.p=each p
writeline outfile,p\x
writeline outfile,p\y
next

closefile outfile

end function

Function LoadPTypeFromFile(FileName$)
infile=readfile(FileName$)
if Infile=0 then runtimeerror("Not a valid file")
count=readline(infile)
for i=1 to count
p.p=new p
p\x=readline(infile)
p\y=readline(infile)
next

closefile infile
end function




That should help you along a bit.


Kyler(Posted 2007) [#3]
Wow! Thanks a ton! I've been trying to get this code right for months! I applied it to my game and it works perfectly!
Again, Thanks! :)


Senzak(Posted 2007) [#4]
you can make a code box using
[code] to begin it 
and

[/code] to end it


Kyler(Posted 2007) [#5]
Hmmm....on second thought, theres huge glitches. I save the game, quit, load it back up and it just lays down random tiles in random spots.
Can somebody please check my save\load function? Thnx.

;SAVE 
If KeyHit(2)
outfile=WriteFile (filename$)
If outfile=0 Then RuntimeError("Invalid File Name Specified")



For c.c=Each c
      ccount=ccount+1
Next
WriteLine outfile,ccount ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For c.c=Each c
WriteLine outfile,c\x
WriteLine outfile,c\y
Next






For b1.b1=Each b1
      b1_count=b1_count+1
Next
WriteLine outfile,b1_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For b1.b1=Each b1
WriteLine outfile,b1\x
WriteLine outfile,b1\y
Next






For b2.b2=Each b2
      b2_count=b2_count+1
Next
WriteLine outfile,b2_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For b2.b2=Each b2
WriteLine outfile,b2\x
WriteLine outfile,b2\y
Next





For b3.b3=Each b3
      b3_count=b3_count+1
Next
WriteLine outfile,b3_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For b3.b3=Each b3
WriteLine outfile,b3\x
WriteLine outfile,b3\y
Next





For b5.b5=Each b5
      b5_count=b5_count+1
Next
WriteLine outfile,b5_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For b5.b5=Each b5
WriteLine outfile,b5\x
WriteLine outfile,b5\y
Next





For g.g=Each g
      g_count=g_count+1
Next
WriteLine outfile,g_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For g.g=Each g
WriteLine outfile,g\x
WriteLine outfile,g\y
Next




For k.k=Each k
      k_count=k_count+1
Next
WriteLine outfile,k_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For k.k=Each k
WriteLine outfile,k\x
WriteLine outfile,k\y
Next




For f.f=Each f
      f_count=f_count+1
Next
WriteLine outfile,f_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For f.f=Each f
WriteLine outfile,f\x
WriteLine outfile,f\y
Next




For f2.f2=Each f2
      f2_count=f2_count+1
Next
WriteLine outfile,f2_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For f2.f2=Each f2
WriteLine outfile,f2\x
WriteLine outfile,f2\y
Next








For sw.sw=Each sw
      sw_count=sw_count+1
Next
WriteLine outfile,sw_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For sw.sw=Each sw
WriteLine outfile,sw\x
WriteLine outfile,sw\y
Next








For sw2.sw2=Each sw2
      sw2_count=sw2_count+1
Next
WriteLine outfile,sw2_count ;a count to put into the file of how many 'p' types you are going to store, not necessary as you could use EOF when loading but still useful...

For sw2.sw2=Each sw2
WriteLine outfile,sw2\x
WriteLine outfile,sw2\y
Next






WriteLine outfile,sx
WriteLine outfile,sy

WriteLine outfile,sx2
WriteLine outfile,sy2

WriteLine outfile,d\x
WriteLine outfile,d\y











CloseFile outfile
EndIf





.load
;LOAD

If KeyHit(59)
infile=ReadFile(filename$)
If Infile=0 Then RuntimeError("Not a valid file")


For sw.sw = Each sw
Delete sw
Next

For sw2.sw2 = Each sw2
Delete sw2
Next

For b1.b1 = Each b1
Delete b1
Next

For b2.b2 = Each b2
Delete b2
Next

For b3.b3 = Each b3
Delete b3
Next

For b5.b5 = Each b5
Delete b5
Next

For c.c  = Each c 
Delete c 
Next

For g.g  = Each g 
Delete g 
Next


For k.k  = Each k 
Delete k 
Next


For f.f  = Each f 
Delete f 
Next

For f2.f2  = Each f2 
Delete f2 
Next






ccount=ReadLine(infile)
For i=1 To ccount
c.c=New c
c\x=ReadLine(infile)
c\y=ReadLine(infile)
c\xx = c\x
c\yy = c\y
c\f = 0
c\w = 100
c\t = 0
Next




b1_count=ReadLine(infile)
For i=1 To b1_count
b1.b1=New b1
b1\x=ReadLine(infile)
b1\y=ReadLine(infile)
Next



b2_count=ReadLine(infile)
For i=1 To b2_count
b2.b2=New b2
b2\x=ReadLine(infile)
b2\y=ReadLine(infile)
Next



b3_count=ReadLine(infile)
For i=1 To b3_count
b3.b3=New b3
b3\x=ReadLine(infile)
b3\y=ReadLine(infile)
Next





b5_count=ReadLine(infile)
For i=1 To b5_count
b5.b5=New b5
b5\x=ReadLine(infile)
b5\y=ReadLine(infile)
b5\xx = b5\x
b5\yy = b5\y
Next






g_count=ReadLine(infile)
For i=1 To g_count
g.g=New g
g\x=ReadLine(infile)
g\y=ReadLine(infile)
g\xx = g\x
g\yy = g\y
g\f = 0
g\d = Rand(0,1)
g\w = 100
g\t = 0
Next




k_count=ReadLine(infile)
For i=1 To k_count
k.k=New k
k\x=ReadLine(infile)
k\y=ReadLine(infile)
k\xx = k\x
k\yy = k\y
k\f = 0
k\d = Rand(0,1)
k\w = 100
k\t = 0
Next






f_count=ReadLine(infile)
For i=1 To F_count
F.F=New F
F\x=ReadLine(infile)
F\y=ReadLine(infile)
F\xx = F\x
F\yy = F\y
F\f = 0
F\d = Rand(0,1)
F\w = 100
F\t = 0
Next




f2_count=ReadLine(infile)
For i=1 To F2_count
F2.F2=New F2
F2\x=ReadLine(infile)
F2\y=ReadLine(infile)
F2\xx = F2\x
F2\yy = F2\y
F2\f = 0
F2\d = Rand(0,1)
F2\w = 100
F2\t = 0
Next



sw_count=ReadLine(infile)
For i=1 To sw_count
sw.sw=New sw
sw\x=ReadLine(infile)
sw\y=ReadLine(infile)
sw\xx = sw\x
sw\yy = sw\y
Next








sw2_count=ReadLine(infile)
For i=1 To sw2_count
sw2.sw2=New sw2
sw2\x=ReadLine(infile)
sw2\y=ReadLine(infile)
sw2\xx = sw2\x
sw2\yy = sw2\y
Next




sx=ReadLine(infile)
sy=ReadLine(infile)

sx2=ReadLine(infile)
sy2=ReadLine(infile)

d\x=ReadLine(infile)
d\y=ReadLine(infile)












CloseFile infile
EndIf




;ENd load



UUICEO(Posted 2007) [#6]
you might try grouping all those values in to one larger type like this:
type TileLocation
        Field Ccount,cX,cY
        Field B1count,b1X,b1Y
end type

Then you can save your information using the following.
for Total.TileLocation = each TileLocation
    AllData$ = Total\Ccount + ","
    AllData$ = AllData$ + Total\cX + ","
    AllData$ = AllData$ + Total\cY + ","
    AllData$ = AllData$ + Total\B1count + ","
    AllData$ = AllData$ + Total\b1X + ","
    AllData$ = AllData$ + Total\b1Y
    WriteLine(outFile, AllData$)
    AllData$ = ""
next


That will give you a Comma Delimited string containing each instance of your type. You then simply need to read it back in to a string, split it up into its component parts and restore the information back into each field. This can be done using the following code:
Function Split(AllData$)
	Local splitcounter = 0
	pntr = -1:counter = 0: oldpntr = 0
	While pntr <> 0
		pntr = Instr(AllData$, ",", pntr + 1)
		If pntr > 0 Then splitcounter = splitcounter + 1
	Wend
	Dim SplitData$(splitcounter + 1)
	pntr = 0:oldpntr = Instr(AllData$, ",", 0)
	SplitData$(1) = Left$(AllData$, oldpntr - 1)
	For counter = 2 To splitcounter
		pntr = oldpntr + 1
		oldpntr = Instr(AllData$, ",", pntr)
		SplitData$(counter) = Mid$(AllData$, pntr, (oldpntr - pntr))
	Next
	SplitData$(counter) = Right$(AllData$, Len(AllData$) - oldpntr)
	Return (splitcounter + 1)

End Function


The above code with split the string AllData$ in to it's component parts and return each individual item in the Array SplitData$() so to reassign your values to your Fields you could use something like this:
			inFile = ReadFile("Your_File.dat")
				While (Not Eof(inFile))
					FilesProcessedCurrent = FilesProcessedCurrent + 1
					Total.TileLocation = New TileLocation
					AllData$ = ReadLine(inFile)
					totalSplits = Split( AllData$ )
					Total\Ccount 			= SplitData$(1)
					Total\cX = SplitData$(2)
					Total\cY	 		= SplitData$(3)
					Total\B1count	 	= SplitData$(4)
					Total\b1X	 	= SplitData$(5)
					Total\b1Y		= SplitData$(6)
				
				Wend
			CloseFile(inFile)




This is UNTESTED speculative code. But I think it should work.


Kyler(Posted 2007) [#7]
this is extremely confusing...Ive never even heard of half these comands