Main Menu

Load & save Integer Arrays

Started by kevin, January 08, 2005, 09:14:50 AM

Previous topic - Next topic

kevin

This code is a quick hack of a generic integer array load/saver.  



PlayBASIC Code: [Select]
; ====================================================================
; >> Generic Array Loading/Saving
; ====================================================================
; This is really simple example to show how you can write your own
; generic (to a point) load & save array functions using array
; instances. The functions included allow you To save And reload
; any 1D,2D,3D Integer array.
;
; They work by passing the source/dest array into the function, then
; either dumping that data to the disc, or reloading it into a
; local version of the instance array. Since instances are basically
; redirections, the data your saving/loading is actually coming from,
; passed array.
; ====================================================================


Dim Table(10)

Print "Original Array Data"
For lp =0 To GetArrayElements(Table(),1)
Table(lp)=Rnd(100)
Print Table(lp)
Next

File$="C:\TEstArray.dat"
SaveIntArray "C:\TEstArray.dat",GetArray(Table())


` ' Make another array
Dim table2(100)

Print "Loaded Array Data"

` Load the previously saved TABLE array into the TABLE2() array
LoadIntArray File$,GetArray(table2())

For lp=0 To GetArrayElements(Table2(),1)
Print Table2(lp)
Next

Sync
WaitKey
End

; ====================================================================
; >> Save Integer 1D/2D/3D Arrays <<
; ====================================================================


Function SaveIntArray(Filename$,ArrayHandle)
If FileExist(filename$)=True Then DeleteFile filename$
MakeArray Array()
SetArray Array(),ArrayHandle

Fn=GetFreeFile()
WriteFile filename$,Fn
Dimensions=GetArrayDimensions(Array())

WriteWord fn,dimensions
For lp =1 To Dimensions
WriteInt fn,GetArrayElements(Array(),lp)
Next

Select Dimensions
Case 1

MakeArray Array1D()
SetArray Array1d(),ArrayHandle

For lp =0 To GetArrayElements(Array1D(),1)
WriteInt fn,Array1d(lp)
Next
Case 2
MakeArray Array2D()
SetArray Array2d(),ArrayHandle
For ylp =0 To GetArrayElements(Array2D(),2)
For xlp =0 To GetArrayElements(Array2D(),1)
WriteInt fn,Array2d(xlp,ylp)
Next
Next
Case 3
MakeArray Array3D()
SetArray Array3d(),ArrayHandle
For zlp =0 To GetArrayElements(Array3D(),3)
For ylp =0 To GetArrayElements(Array3D(),2)
For xlp =0 To GetArrayElements(Array3D(),1)
WriteInt fn,Array3d(xlp,ylp,zlp)
Next
Next
Next

EndSelect

CloseFile Fn
EndFunction



; ====================================================================
; >> Load Integer 1D/2D/3D Arrays <<
; ====================================================================


Function LoadIntArray(Filename$,ArrayHandle)
If FileExist(filename$)=False Then Exitfunction

MakeArray Array()
SetArray Array(),ArrayHandle


Fn=GetFreeFile()
ReadFile filename$,Fn
Dimensions=ReadWord(fn)
Dim Dimensions(3)
For lp =1 To Dimensions
Dimensions(lp)=ReadInt(fn)
Next

Select Dimensions
Case 1
` Save the Data from a 1D array

MakeArray Array1D()
SetArray Array1d(),ArrayHandle
Dim Array1d(dimensions(1))

For lp =0 To Dimensions(1)
Array1d(lp)=ReadInt(fn)
Next

Case 2
` Save the Data from a 2D array
MakeArray Array2D()
SetArray Array2d(),ArrayHandle
Dim Array2d(Dimensions(1),Dimensions(2))

For ylp =0 To Dimensions(2)
For xlp =0 To Dimensions(1)
Array2d(xlp,ylp)=ReadInt(fn)
Next
Next

Case 3
` Save the Data from a 3D array
MakeArray Array3D()
SetArray Array3d(),ArrayHandle
Dim Array3d(Dimensions(1),Dimensions(2),Dimensions(2))
For zlp =0 To Dimensions(3)
For ylp =0 To Dimensions(2)
Login required to view complete source code



tomazmb

Hello Kevin,

I can't say I fully understand the meaning of arrays. Can you explain a bit more the purpose of 1D, 2D and 3D arrays in a bit more english way (or what is useful for what in gaming sense). Thanks.

Have a nice day,

Tomaz
My computer specification:

AMD Athlon 64 2800+
MB ASUS K8V Socket 754 VIA K8T800
SB Audigy 2
3 GB RAM DDR 400 MHz PQI
AGP NVIDIA GeForce 7600GT 256 MB-Club 3D
Windows XP Pro SP2
DirectX 9.0c

Travis P

#2
@kevin:
Thats just what I needed, Thx.

@tomazmb:
Arrays can store many values, for example.
1 demision

dim TheArray(10)

now you can use TheArray(0),TheArray(1),....,TheArray(9),TheArray(10) Like normal varibles.
2 demisions

dim TheArray(10,1)

now you can use TheArray(0,0),TheArray(0,1),....,TheArray(10,0),TheArray(10,1).
and then 3 demisions

Its can be helpfull in alot of ways.
Heres one simple way.

for num=0 to 10
TheArray(num)=Rnd(99)
next num

instead of...

number_one=Rnd(99)
number_two=Rnd(99)
...


Hope that helps.
Note: The above I didn't say, your just crazy.

Not a ripoff of The Twilight Zone

tomazmb

Hello Travis P,

Thanks, every bit of information helps.

Have a nice day,

Tomaz
My computer specification:

AMD Athlon 64 2800+
MB ASUS K8V Socket 754 VIA K8T800
SB Audigy 2
3 GB RAM DDR 400 MHz PQI
AGP NVIDIA GeForce 7600GT 256 MB-Club 3D
Windows XP Pro SP2
DirectX 9.0c