Problem: creating an array with "DIM". Please. ?.

Blitz3D Forums/Blitz3D Beginners Area/Problem: creating an array with "DIM". Please. ?.

etxtra(Posted 2011) [#1]
Hi everyone! I have a very bad problem with arrays:
I need to create an array so I use "Dim" like this :
Dim T_Textures_Handles_E%(0)

the above line of program is in the main program (main file). it's the file that I execute. Then, in an other included Blitz3d file, I use the same array to store the "handle" of a texture, like this :
T_Textures_Handles_E ( 0 ) = LoadTexture ( "Bin\Terrain\" + V_Terrain_Ensemble_S )

The above line is in an included file.
Then, when I try to execute the main program, it doesn't compile and it says : "Function 't_textures_handles_e' not found" and all I can do is click on "OK" !
Please, tell me, what's wrong with my array ?
How do you create arrays that works ?
I've tried to figure out the solution without success...

Last edited 2011


Yasha(Posted 2011) [#2]
Are you definitely calling Dim before trying to set the array element?

When it claims an array is a function-not-found, that usually means it hasn't recognised the declaration, which in turn implies the declaration hasn't happened yet.


etxtra(Posted 2011) [#3]
hi @Yasha, yes I Dim the array first and then give it a value.
Please note that i'm having this problem with a program that has multiples included files, in my opinion this may be source of the problem because the following program works well and it is a single file:
; no problem with graphics;
Graphics3D 640,480
;nor with array, so maybe it's due multiple files included ?
Dim T_Textures_Handles_E%(0)
T_Textures_Handles_E(0) = 666
Repeat
	Locate 0,0
	Print T_Textures_Handles_E(0)
Forever


Last edited 2011

Last edited 2011


Warner(Posted 2011) [#4]
Yes, Dim should go >before< using the array.
So this wrong:
include "using array.bb"
Dim array(10)

This is right:
Dim array(10)
include "using array.bb"


Last edited 2011


Yasha(Posted 2011) [#5]
Observe:
Function aTest(n)
	array(10)=n
End Function

Dim array(10)
aTest(1)


Even though the array is declared before it's used in runtime order, there's still an error unless you move the array declaration to above the function definition.

I'm pretty sure this should be considered a bug, but it should at least work correctly if the array is defined above the function. (Do note that Global variables, Global C-style arrays, and function definitions - all the other globally-visible names - should work correctly in this situation; I'm pretty sure the bug is unique to Dim.)

To be honest, "Dim" arrays belong to the older Basic coding style (Goto, Gosub and all that rubbish) and don't fit well with proper functions anyway - they probably just weren't designed to work together properly.

Last edited 2011


etxtra(Posted 2011) [#6]
Thanks to @Warner and @Yasha ! Thank you ! :-)

-Ok, this is resolved !

Blitz3D has some little things that if you don't know you may feel lost a bit, don't you think ?! note that I have some other questions to ask but I'll wait the right moment to do this.

Last edited 2011


big10p(Posted 2011) [#7]
I guess when the preprocessor - which parses top-down - comes across a so-far unknown reference followed by an open bracket, it just assumes it must be a function that's defined later in the source. Hence, the incorrect error message.

Last edited 2011


Warner(Posted 2011) [#8]
'Dim' could be considered as two commands combined. It declares a variable as a global array (compile time), and it sets it size (runtime).
A way to do the above would be this:
Dim array(0) ;declare global variable

Function aTest(n)
	array(10)=n
End Function

Dim array(10) ;set array size
aTest(1)

I've allways assumed that BlitzArrays were intended to create some kind of user defined variable type with a custom size. That might be why they have only one dimension, and can't be resized. They are indeed really convenient, because they can be passed to functions, used as Type fields etc.
Blitz has also "Banks". They have a flexible size and I believe they keep their data after resizing.


_PJ_(Posted 2011) [#9]
I guess when the preprocessor - which parses top-down - comes across a so-far unknown reference followed by an open bracket, it just assumes it must be a function that's defined later in the source. Hence, the incorrect error message.



That seems to be the case, and would explain Yasha's 'bug' and Warner's code too, which both in threory should be perfectly acceptable, but cause an error.
Interesting points to remember


Warner(Posted 2011) [#10]
My code? An error? It was supposed to be a 'working example' ..


_PJ_(Posted 2011) [#11]
Sorry, Warner, I meant this one: :)


So this wrong:
include "using array.bb"
Dim array(10)




You'ld expect the compiler to compile the dependancy first, naturally, and of courese, the Dim isn't compiled yet, but even if "using array.bb" only had functions that referred to array(x), it still would fail compilation as the compiler seems to assume array() as a function.


jfk EO-11110(Posted 2011) [#12]
To be honest, "Dim" arrays belong to the older Basic coding style (Goto, Gosub and all that rubbish) and don't fit well with proper functions anyway


OMG Yasha... I've learned a lot from your code and you are one of my higly respected teachers (eg. shadow mapping), but I feel more and more like Skywalker becomeing Darth Vader or something... ;) "older Basic coding style" WTH!? ^^

Ok, I am old, I got that, but so is Basic and what's wrong with the original basic style? I prefere it much over eg. VBasic "style".

DIM is nice. Good for things like maps, chessboards, reverse lookuptables and a lot more. They are faster than Types and easier to handle than banks. I made pretty much everything with DIM-Arrays in other Basic languages that is now usually done with Types and Banks in Blitz.


Yasha(Posted 2011) [#13]
OK I could have been a little more diplomatic there. My apologies - and rest assured JFK that I respect you and your own code a great deal.

I do think it is a true thing that there's quite a large divide between the different Basic generations though. Subroutines and Dim-arrays come from the 80s-90s generation, while functions and C-style arrays are more of the late-90s generation as Basic starts to inherit more and more features from the C-family. And I don't think they fit together that well: with arrays, this manifests itself with the parser expecting the code to act like a top-down linear script because it can't tell the difference between Dim as a declaration and Dim as a resizing operation, despite the fact that it's also supposed to be able to declare functions in any order.

Yes it's a useful feature, given the right context. I happen not to like it or use it myself... and will try to not let that slip into what ought to be objective assessments of utility in future.


jfk EO-11110(Posted 2011) [#14]
You have a point there, but you may also ask yourself why you don't use VC++ in the first place. For me, it is (at least to some degree) the old basic style, the nostalgic, limited basic code that is fascinating me. For example even on a Sharp Pocket Computer (those old calculators with embeded basic).