QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Dav on August 20, 2020, 11:55:56 am
-
Making a game where a GOSUB sets level data. I'm must be missing something obvious, but I'm getting a Duplicate Definition error when using this REDIM in a GOSUB routine. Not sure why. (I can already hear the..."Another reason to NOT use GOSUB") :)
Here's a shortened code that brings up the IDE error notice. Thanks for any help.
- Dav
Error version...
'=======
SetLevel:
'=======
pdata$(t, t) = "v"
pdata$(1, 1) = "x"
If I move the REDIM stuff to the top from the GOSUB it will run...
pdata$(t, t) = "v"
pdata$(1, 1) = "x"
-
When you do this in line 3:
You're forcing qb64 to create an undeclared array. If you have a REDIM line before that, you can REDIM as you like in the gosub block, because the variable will have been created as dynamic.
If you don't want to do that, just add this to the top:
-
Thanks, Fellippe! I WAS missing something obvious.
Appeciate it.
- Dav
-
this is an interesting issue...
it remembers me the issue and the bug that I found in Qbasic about DEF FN.
https://www.qb64.org/forum/index.php?topic=2524.msg117972#msg117972 (https://www.qb64.org/forum/index.php?topic=2524.msg117972#msg117972)
I admit that I never have used DEF FN in Qbasic in my programs. Yes I know it theorically, but never I used it in a real program.
Nevertheless I tried to make a tool to convert old DEF FN of QB/QBasic into FUNCTION of QB64. But for my lackness of knowledge I thought DEF FN as a separate block of code not so different from FUNCTION or SUB. What a dumbness!! DEF FN is like a GOSUB RETURN block of code so it is a piece of the main module. To complete my tool I need to develope a parser to find the variables declared into DEF FN and the variables of the main that DEF FN uses because all these must be SHARED to not break the link of code!.
Why is it matter with your experience?
Reading your code I have thought that there is no problem because in a flow of program from the high to the bottom and from left to the right of lines of source code the line
has been executed after the declaration
'=======
SetLevel:
'=======
and all these lines are in the main module... so why does the parser understand pdata$(1,1) ="x" as a different array variable with declaration and initialization in the same line of code?
The answer is: it is possible that the lines of source code are managed and changed during the compiling process and so the line IF pdata$(1,1) = "x" THEN is processed before the line REDIM pdata$(10,10).
This is the same that happens in QBasic with this code FnQbasicBug= z% * a%
a% = 10
So we can affirm that this is another gift that QB64 has eredited from QB/Qbasic and it happens to duplicate the way of working of Qbasic.