Array Of Types

Blitz3D Forums/Blitz3D Programming/Array Of Types

QuickSilva(Posted 2003) [#1]
Hello,

Can someone please explain to me the concept of having an array of types as I`m finding it a little hard to get my head around. A simple example would be most helpful if anyone has the time featuring possible uses for it.

Regards,

Jason.


soja(Posted 2003) [#2]
The first thing that will help you undertand is that whenever someone says "type", they're almost always talking about *custom* types. Remember, that strings, ints, floats, etc are all "types".

So, in the same way that you can have an array of any standard type variable...
Dim ArrayOfStrings$(10)
Dim ArrayOfInts%(10)
Dim ArrayOfFloats#(10)

...you can also have an array of custom types. Let's make our own "Hue" type, for example.
Type Hue
	Field R%
	Field G%
	Field B%
End Type

Now lets create a new Hue object (or instance) and assign a variable name to it.
Color1.Hue = New Hue

Let's look at that statement in more detail. First, the part to the right of the equals sign ("New Hue"). This part of it tells Blitz to allocate enough space for one instance of your custom type. In this case, it allocates 32 bits for each integer value (R, G, B), plus a little bit for the type overhead (which we don't worry about). This chunk of RAM now becomes an instance of Hue type. Blitz also internally puts it in the global linked list of Hue-type instances (by default, accessible for "First Hue" or "Last Hue").

Now the left of the equals sign. The ".Hue" part of "Color1.Hue" just means that there is a new variable called "Color1" that is for accessing Hue-type variables. (Just like "Color1$" would be for accessing strings.) The part you need to understand is that Color1 is NOT the actual type instance that you just created with the New command, but a *pointer* to it. So now to access that type instance, you could say "First Hue" (which would return the address where the type is), OR you could say "Color1".

Of course, Color1 could be assigned to point to any other instance of type Hue. Like this:
Color1.Hue = New Hue ; This becomes the first instance of the Hue linked list
Red.Hue = New Hue ; This becomes the second (and last) instance of the Hue linked list

(at this point there are a number of ways to access the first Hue we made)
AnotherColorPointer.Hue = First Hue
AnotherColorPointer.Hue = Color1
AnotherColorPointer.Hue = Before Last Hue
AnotherColorPointer.Hue = Before Red
(and there are a number of ways to access the second/last Hue element)
AnotherColorPointer.Hue = Last Hue
AnotherColorPointer.Hue = After First Hue
AnotherColorPointer.Hue = Red
AnotherColorPointer.Hue = After Color1
(if we did this...)
Color1 = Red

(then these would be ways to access the first Hue we created)
AnotherColorPointer.Hue = First Hue
AnotherColorPointer.Hue = Before Color1
AnotherColorPointer.Hue = Before Last Hue
AnotherColorPointer.Hue = Before Red
(and these would be ways to access the second/last one)
AnotherColorPointer.Hue = Last Hue
AnotherColorPointer.Hue = After First Hue
AnotherColorPointer.Hue = Red
AnotherColorPointer.Hue = Color1

Sorry for being long-winded, but I think that the best way to answer your question is to help you understand that any variable of a certain custom type (Color1.Hue) is just a pointer to the actual instance of that type. In this case, the pointer is just a number. SO... going back to the original question, does it make you understand why this is valid?
Dim ArrayOfHues.Hue(10)

It's just an array like the other ones above, except that it contains Hue pointers, because it was declared with ".Hue".

So now you can do stuff like this:
For i% = 1 To 10
	ArrayOfHues(i) = New Hue
Next

...and then you would have 10 Hues instantiated with "First Hue" and ArrayOfHues(1) pointing to the first one, ArrayOfHues(2) pointing to the second one, ArrayOfHues(3) pointing to the third one... etc... all the way until "Last Hue" or ArrayOfHues(10).

You could change them just like this:
ArrayOfHues(5)\r = 255
ArrayOfHues(5)\g = 128
ArrayOfHues(5)\b = 0


And now that you have Hue pointers for every Hue instance, if you want to use a certain color in your Hue list, you don't need to search through the entire linked list of them -- you just use your pointer directly from the array.

Follow?


QuickSilva(Posted 2003) [#3]
Thanks soja, thats been a GREAT help, thanks for taking the time to reply :)

Jason.