Types

Blitz3D Forums/Blitz3D Beginners Area/Types

Big Shooter(Posted 2004) [#1]
ok guys, im back again. i really hope you guys dont mind helping me out. anyway, im back for some info on Types. i am TOTALLY lost about Types. now i promise, i searched through this site, BlitzCoder, i've looked in my book, i've googled it, and read the documentation that came with Blitz, and still have no idea, as to what types are, and how they are used. thank you so much guys, your the best!


eBusiness(Posted 2004) [#2]
I did a small tutorial, havn't had any feedback yet, I think it's pefectly understandable and gives good fast facts, on the other hand I wrote it and I happen to know what I talk about from the beginning. Is it brief enought?

http://www.blitzbasic.com/Community/posts.php?topic=34757


WolRon(Posted 2004) [#3]
Since you now understand arrays...

Types can be thought of as special arrays that can hold more than just one type of data per element. (They are not actually arrays, but let's just pretend that they are for now.)

For example:
Dim AnArray(0)
is an array that can contain one integer as a value.
Dim AnArray$(0)
is an array that can contain one string as a value.

Creating an array that can hold more than one integer per element is easy (create a two-dimensional array):
Dim AnArray(0,3)
Now you have an array that can hold 4 integers (0-3) per element.

Creating an array that can hold more than one string per element is easy as well:
Dim AnArray$(0,3)
Now you have an array that can hold 4 strings per element.

But what if you wanted your array to be able to hold both kinds of values (integers AND strings)? You can't create an array that can hold both types of variables.

But you can use Types to achieve this.
Type AType
  Field AnInteger
  Field AString$
End Type
Now if you create an instance of that type:
thistypeinstance.AType = New AType
You can assign both your integer and string values to it:
thistypeinstance\AnInteger = 1435
thistypeinstance\AString$ = "Hello World"
The reason I am comparing them to arrays is because like arrays (which have multiple elements), types can have multiple elements too. You just create another instance of it.
thistypeinstance.AType = New AType
Now we could assign values to this new instance if we wanted to:
thistypeinstance\AnInteger = 348
thistypeinstance\AString$ = "Hi Mom!"
PLEASE NOTE that since we created a New type instance and assigned it to the same variable 'thistypeinstance', that we can no longer access the first instance we created using that variable any more(it now points to the second instance we created).
In order to do that, we have to iterate through the type list to retrieve it:
For thistypeinstance.AType = Each AType
  Print thistypeinstance\AnInteger
  Print thistypeinstance\AString$
Next
Which would produce the results:
1435
Hello World
348
Hi Mom!



Rook Zimbabwe(Posted 2004) [#4]
Wolron wrote an excellent tut on types... Type has confused me as well for a while (still a newbie meself) but there are a few good tuts in the tutorials section online. Read and run them all... :) They helped me!

I used the advice I got from Wolron and the Rosses to aid me in my own collision detection routine (it is also in the tutorial section.)

Good luck
-Rook Zimbabwe


Big Shooter(Posted 2004) [#5]
thank you wolron, an excellent example. its not quite setting with me even after a couple days, but i'll think about it some more and try and figure it out... thank you!