Page 1 of 1

can't create an array based on string passed in my subroutin

Posted: Thu Jun 21, 2012 7:41 pm
by ChristopherWhite
So, I'm creating graphics with DATA and trying to create a subroutine that I can call for each graphic.

The routine is:
SUB initGraphics (gfxName$, xWidth, yWidth)

gfxName$ is the name of the array that will be storing the graphic data. In the subroutine, I create the array with this line:

DIM gfxName$(xWidth * yWidth)

I am using QB64, and while I don't get any error messages, once I try to run the program, I get the message "Illegal Function Call".

Does anyone have a solution? Full code posted below.


Code: Select all

CLS
SCREEN 7


100: 'a 5x5 white block
DATA 15,15,15,15,15
DATA 15,15,15,15,15
DATA 15,15,15,15,15
DATA 15,15,15,15,15
DATA 15,15,15,15,15

101: 'a 5x5 blue block
DATA 09,09,09,09,09
DATA 09,09,09,09,09
DATA 09,09,09,09,09
DATA 09,09,09,09,09
DATA 09,09,09,09,09

RESTORE 101 'load the blue block first
CALL initGraphics("BlueBlock", 5, 5) 'use the subroutine to get the blue block, saving the data in the array named "BlueBlock"
RESTORE 100 'load the white block
CALL initGraphics("WhiteBlock", 5, 5) 'use the subroutine to get the white block, saving the data in the array named "WhiteBlock"

PUT (1, 1), BlueBlock 'put the blue block on the screen
PUT (6, 1), WhiteBlock 'put the white block on the screen, next to the blue one

SUB initGraphics (gfxName$, xWidth, yWidth)
'subroutine to intialize graphics, where gfxName$ is the
'desired name for the array that will store the graphic DATA
'and xWidth and yWidth are the dimensions

FOR y = 1 TO yWidth 'read the data, put it on the screen
    FOR x = 1 TO xWidth
        READ z
        PSET (x, y), z
    NEXT x
NEXT y

DIM gfxName$(xWidth * yWidth)
'create an array using the string passed to the subroutine as its name
'this is the part that doesn't seem to work

GET (1, 1)-(xWidth, yWidth), gfxName$()
'I get the same problem here in the code once I try to get the image and
'store it in the array

CLS

END SUB

Posted: Fri Jun 22, 2012 1:13 pm
by burger2227
1) You should always DIM or REDIM arrays first! REDIM allows you to resize an array later.

2) You cannot create Arrays inside of running programs using string names. The array names must be created by the programmer before it is used. The only way you could create program variables and code would be to PRINT it to another BAS file before it is run.

GET is used to transfer existing screen data to an INTEGER array. PUT can place the array data back to the screen.

You can REDIM an array to change its size and put the data into it.

REDIM Array(wide * high)

Use PSET to set the colors on the screen and then GET the box area to an Array. You can then BSAVE the contents of that array to a file which can use the name you wanted to give the array.

BTW you cannot create DATA in a running program either, but you can PRINT DATA fields to the end of a Qbasic BAS file when it is not running.