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

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
ChristopherWhite
Newbie
Posts: 1
Joined: Thu Jun 21, 2012 7:36 pm

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

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply