Qbasicnews.com

Full Version: Loading data into automatically REDIM 3d-array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Subscript out of range

Code:
'$DYNAMIC
REDIM SHARED tileSet(1 TO 1, 1) AS INTEGER

FOR SprNum = 0 TO 0 'number of tiles to load - 1
  gfxFile$ = gfxPath$ + LTRIM$(STR$(SprNum)) + gfxExt$
  LoadISet gfxFile$, tileSet(), SprNum
NEXT SprNum

-------------------------------------------------------
SUB LoadISet (fileName$, ImageArray(), SprNum)
  FileNo = FREEFILE
  OPEN fileName$ FOR BINARY AS #FileNo
    Ints = (LOF(FileNo) - 7) \ 2
  CLOSE #FileNo
  REDIM PRESERVE ImageArray(1 TO Ints, SprNum)
  DEF SEG = VARSEG(ImageArray(1, SprNum))
  BLOAD fileName$, 0
  DEF SEG
END SUB

I'm trying to load multiple tiles into a 3d array and have the array redimension itself accordingly. Redimensioning it with PRESERVE results in a Subscript out of range. The tiles are regular BSAVE files, 32x32.

This currently should load "0.bsv" into the array "tileSet(1, 0)"
So PUT (0,0), tileset(1,0) should put the loaded tile on the screen.


btw, is it [ syntax=qbasic ] for the syntax tag? i've never used it (except for that one cut and paste in another thread)
I think with REDIM PRESERVE, it needs room in memory for both the old and new array, temporarily while it copies over the data. Can you tell how big the array is and how much free memory there is when you get the error message?

It's [syntax="qbasic"], I think. Lemmie make sure:[syntax="qbasic"]10 PRINT "Hello, World!"[/syntax]

EDIT: I just found this in QB 7.1's help, notice the bottom line about /R:
Code:
■ The PRESERVE keyword allows you to raise or lower the outer
      bound of a dynamic array without erasing data. For example:

      REDIM X(10, 10, 10)
      .
      .
      .
      REDIM PRESERVE X(10, 10, 15)

      This example keeps any data entered in the array X() and adds
      space for more elements in the third dimension of the array.
      The third dimension of X() is the rightmost bound of the array.
      To change the leftmost bound, you must compile the program with /R.

And here's what BC.EXE's /R option does:
Code:
/R              Stores arrays in row-major order. BASIC normally stores
                arrays in column-major order.
so i can only test it after compile? =o= nuts
Or, you could only change the last dimension, and leave the first at a safe size, like whatever the biggest sprite needs.
Well, now I'm not getting the Subscript error, so thanks for that!

Another problem developed though --
Background info -- my tiles are 32 x 32 (32 x 32 + 1 = 1025)

[syntax="qbasic"]
FOR SprNum = 0 TO 2 'number of tiles to load - 1
gfxFile$ = gfxPath$ + LTRIM$(STR$(SprNum)) + gfxExt$
LoadISet gfxFile$, tileSet(), SprNum
NEXT SprNum
[/syntax]

If I try to PUT tileset(1025, 1), or PUT tileset(1025, 2) I get a blank.
If I PUT tileset(1025, 0) I get tileset(1025, 2)

This means that SprNum is not increasing?
[syntax="qbasic"]
DEF SEG = VARSEG(ImageArray(1, SprNum))
BLOAD fileName$, 0
DEF SEG
[/syntax]
just overwrites tileset(1025, 0) with whatever it is BLOADing.

Any clue on why it won't store into tileset(1025, 1) or tileset(1025, 2)?
What? What? What!? There's a REDIM PRESERVE function? Aww man! That would have saved so much time and made so many of my programs better. Dammnit! Why didnt I know About this earlier! >_<
dark_prevail: :lol: It's only in QB 7.1 and up. 4.5 and Qbasic don't have it.

potato: about this code:
[syntax="qbasic"]
DEF SEG = VARSEG(ImageArray(1, SprNum))
BLOAD fileName$, 0
DEF SEG[/syntax]
...you shouldn't assume that ImageArray(1, SprNum) has a VARPTR of 0. You can only make that assumption with the start of the array. It's probably always overwriting the first one because VARSEG is returning the same number (the address of the array) no matter what SprNum is. So you'll have to use VARPTR also.
Quote:What? What? What!? There's a REDIM PRESERVE function? Aww man! That would have saved so much time and made so many of my programs better. Dammnit! Why didnt I know About this earlier! >_<

You really need to look at QB's help section. Its very well documented with examples and stuff... You could've found it, if you were using QB v7.1 though...
[syntax="qbasic"] REDIM PRESERVE ImageArray(1 TO 515, SprNum)
DEF SEG = VARSEG(ImageArray(1, SprNum))
BLOAD fileName$, 0
DEF SEG
[/syntax]

---

[syntax="qbasic"] REDIM PRESERVE ImageArray(1 TO 515, SprNum)
DEF SEG = VARSEG(ImageArray(1, SprNum))
BLOAD fileName$, VARPTR(ImageArray(1, SprNum))
DEF SEG
[/syntax]

---

SUCCESS

!!! [Image: shyu4.gif]

THANKS STERLING!!
No problem, glad to have been able to help! Big Grin