devilman, I've just now edited that post and added a couple of extra lines of code to the Code Snippet example to prevent an error occuring when writing the file:
Rem This will simply delete the test file if it already exists
If File Exist(FILENAME$)
Delete File FILENAME$
EndIf
Apparently you will get an error when using the "Open to Write" command if the file already exists. So, you would have received an error code if you ran my example code more than once.
I've also incorporated the read file command into the program and to skip the print commands slowing the process down.
Sorry for any inconvenience caused (if you ran the program more than once), please try it again.
You do know you only need to save your file as a "
.txt" file if it IS a text file containing ASCII values? I used "
.dat" in the example as I was simply storing values randomly between 0 and 255 - These wouldn't have made any sense if you browsed the file afterwards in Windows Notepad. You could create your own filename extensions. In your particular case you might have used "
MyMapData.lvl"
This is the updated code: (Last Update 17:05 GMT - 20/06/2003)
FAST=0 : Rem A value of 0 will slow this program down as it prints your arrays values to screen, 1 will skip it
Rem Alternative method of creating arrays using MEMBLOCK commands
Rem Note: The MEMORY commands can also be used in a similar way too, but this is safer!
BYTESIZE=1 : Rem Note: Don't change value in this example
Rem The size in bytes of each storage space within your MEMBLOCK
Rem Use a value of 1 if using MEMBLOCK BYTE to manipulate data
Rem a value of 2 for MEMBLOCK WORD and 4 for MEMBLOCK DWORD...
Rem I'm gonna make a 2 dimensional array for byte size variables (Holds integers between 0 and 255).
Rem Imagine a map grid using X (rows) and Y (columns) coordinates, 4 by 4 (positional values from 0 to 3)
Rem A 3 dimensional array would have a Z dimension for depth (pages stacked on top of each other)
XMAX=4
YMAX=4
BLOCKNUMBER=1: Rem Must be greater than 0
Make MemBlock BLOCKNUMBER,(XMAX*YMAX)*BYTESIZE
Rem Remember, positional values start at 0, so 1 is subtracted from the predefined maximums
Rem I'll be writing random, byte size values from left to right and top to bottom here using the "For - Next" loops
FILENUMBER=1 : Rem Valid numbers between 1 and 32
FILENAME$="Test.dat"
Rem Reading the file if it already exists
Rem and making sure the file matches the size of the array in this example,
Rem just incase you changed XMAX or YMAX for example
If File Exist(FILENAME$)
If File Size(FILENAME$)=4+((XMAX*YMAX)*BYTESIZE)
Print "Filling a ";XMAX;" by ";YMAX;" (2 dimensional) array with predetermined values"
Print "Reading from a file called: ";FILENAME$;"..."
Open To Read FILENUMBER,FILENAME$
Read MemBlock FILENUMBER,BLOCKNUMBER
Close File FILENUMBER
Else
Print "File size does not match new Array dimensions!"
Print "Filling a ";XMAX;" by ";YMAX;" (2 dimensional) array with random values instead:"
Randomize Timer()
For Y = 0 To YMAX-1
For X = 0 To XMAX-1
POSITION = (X+(Y*XMAX))*BYTESIZE : Rem POSITION is like a pointer
Rem This is the important formula needed to locate and retrieve data from your array.
Rem Obviously, it will need changing and become more complex for arrays with more than 2 dimensions
BYTEVALUE = Rnd(255) : Rem You would feed the memory block with the appopriate value. I'm using random here for example
Write Memblock Byte BLOCKNUMBER,POSITION,BYTEVALUE : Rem Check out other MemBlock commands for larger variable sizes, i.e. WORD and DWORD
Next X
Next Y
EndIf
Else
Print "Filling a ";XMAX;" by ";YMAX;" (2 dimensional) array with random values:"
Randomize Timer()
For Y = 0 To YMAX-1
For X = 0 To XMAX-1
POSITION = (X+(Y*XMAX))*BYTESIZE : Rem POSITION is like a pointer
Rem This is the important formula needed to locate and retrieve data from your array.
Rem Obviously, it will need changing and become more complex for arrays with more than 2 dimensions
BYTEVALUE = Rnd(255) : Rem You would feed the memory block with the appopriate value. I'm using random here for example
Write Memblock Byte BLOCKNUMBER,POSITION,BYTEVALUE : Rem Check out other MemBlock commands for larger variable sizes, i.e. WORD and DWORD
Next X
Next Y
EndIf
Print
If FAST=0
Rem Now to retrieve data and print all the array's contents to screen for a test
For Y = 0 To YMAX-1
For X = 0 To XMAX-1
POSITION = (X+(Y*XMAX))*BYTESIZE : Rem POSITION is like a pointer - same formula here as for writing above
CONTENTS = MemBlock Byte(BLOCKNUMBER,POSITION) : Rem Get the value held in your array (BLOCKNUMBER) at the current location (POSITION)
Print X;"X,";Y;"Y = ";CONTENTS
Next X
Next Y
EndIf
Rem Below I'm gonna use some extra variables here for readability purposes only.
Rem Normally I might cut out the middle man and access the values more directly. So the Print command further down would read:
Rem Print "The contents of ";X1;"X,";Y1;"Y (";MemBlock Byte(BLOCKNUMBER,(X1+(Y1*XMAX)));") and ";X2;"X,";Y2;"Y (";MemBlock Byte(BLOCKNUMBER,(X2+(Y2*XMAX)));"), added together = ";MemBlock Byte(BLOCKNUMBER,(X1+(Y1*XMAX)))+MemBlock Byte(BLOCKNUMBER,(X2+(Y2*XMAX)))
X1=Rnd(XMAX-1) : Y1=Rnd(YMAX-1) : POSITION1=(X1+(Y1*XMAX))
X2=Rnd(XMAX-1) : Y2=Rnd(YMAX-1) : POSITION2=(X2+(Y2*XMAX))
CONTENTS1 = MemBlock Byte(BLOCKNUMBER,POSITION1)
CONTENTS2 = MemBlock Byte(BLOCKNUMBER,POSITION2)
SUM = CONTENTS1 + CONTENTS2
Print
Print "The contents of ";X1;"X,";Y1;"Y (";CONTENTS1;") and ";x2;"x,";y2;"y (";contents2;"), added together = ";SUM
Print
Print "(Press a key to continue)"
Wait Key
Rem You can save your data like so:
If File Exist(FILENAME$)
Delete File FILENAME$
EndIf
Open To Write FILENUMBER, FILENAME$
Write MemBlock FILENUMBER,BLOCKNUMBER
Close File FILENUMBER
Print "File saved as ";FILENAME$
Print "This program is now terminating. Please wait..."
Wait 1000
Rem Tidy up before Terminating
Delete MemBlock BLOCKNUMBER
I tried your array size (1000*25) and encountered no slow down problems when reading the actual file itself. Enable the FAST variable to 1, to skip the Print loop I have in there to speed up the process.