Blitz3D Docs -> 2D - Category -> Basic -> Type
Parameters:variable = any legal variable name
Description:If you know C programming, a TYPE is basically a STRUCT in Blitz Basic. If you don't know C, read on!
TYPE is your best friend. It is used to create a 'collection' of objects that share the same parameters and need to be interated through quickly and easily.
Think about SPACE INVADERS. There are many aliens on the screen at one time. Each of these aliens have a few variables that they all need: x and y coordinates plus a variable to control which graphic to display (legs out or legs in). Now, we could make hundreds of variables like invader1x, invader1y, invader2x, invader2y, etc. to control all the aliens, but that wouldn't make much sense would it? You could use an array to track them; invader(number,x,y,graphic), and the loop through them with a FOR ... NEXT loop but that is a lot of work! The TYPE variable collection was created to handle just this sort of need.
TYPE defines an object collection. Each object in that collection inherits its own copy of the variables defined by the TYPE's FIELD command. Each variable of each object in the collection can be read individually and can be easily iterated through quickly. Use the FIELD command to assign the variables you want between the TYPE and END TYPE commands.
If it helps, think of a TYPE collection as a database. Each object is a record of the database, and every variable is a field of the record. Using commands like BEFORE, AFTER, and FOR ... EACH, you can move change the pointer of the 'database' to point to a different record and retrieve/set the variable 'field' values.
Not a database guru? Need another example? Okay. Let's say you are setting up an auditorium for a speech or event and you are putting up hundreds of chairs for the spectators. The chairs have to be in a certain place on the floor, and some will need to be raised up a bit higher than others (visiting dignitaries, the mayor is coming, etc.). So being the computer genius you are, you start figuring out how you can layout the chairs with the least amount of effort. You realize that the floor is checkered, so its really a huge grid! This will make it easy! You just need to number the floor on a piece of graph paper and put into the grid how high each chair should be, based on where the boss told you the important people are to sit. So, for each chair, you will have a row and column on the graph paper (x and y location) and a level to adjust the chair to (height). Good, we are organized. Now, even though we have it all on paper, we still have to do the work of placing all the chairs. After you are done, let's say your boss walks up to you and says "they aren't centered right .. move'em all over 1 square". Oh no! You have them all perfect, and even though it is a simple thing to move a chair one square to the right (after all, their order and height won't change) - you still have to move each and every chair! Sure would be nice if you could just wave your hand and say "For each chair in the room, add 1 square to its x location" and have it just magically happen. Alas, in the real world, get busy - you've got a lot of chairs to move!
In Blitz, you could have set up a TYPE called CHAIR, set the TYPE's FIELDS as X, Y, and HEIGHT. You would then create as many chairs as you need with the NEW command (each time you call NEW, it makes a new chair, with its OWN X, Y, and HEIGHT variables) and assign them the X, Y, and HEIGHT values you decide upon. In our example above, when the boss told you to move the chairs over 1 box, you probably groaned inside. That's a lot of work! In Blitz, we could use four lines of code to adjust all our CHAIR objects to the new position (using FOR ... EACH commands).
Still lost? Its okay - TYPEs are hard to get a grasp on. Look at the example and we'll try to show you how types work in a practical environment. I recommend looking at other people's code too, to help you get a handle on them. Once you do, you will know why C people are crazy for STRUCTs and why almost all Blitz programs use them.
A cunning trick for debug purposes, or for saving data from types to a file, is to use the Str$ command. Print Str$(
Advanced programmers might like to know that Types are stored in a "doubly linked list".
See also: Field, New, Null, First, Last, Before, After, Insert, Before.
; Define the CHAIR Type Type CHAIR Field X Field Y Field HEIGHT End Type ; Create 100 new chairs using FOR ... NEXT using the collection name of ROOM For tempx = 1 to 10 For tempy = 1 to 10 room.chair = New Chair room\x = tempx room\y = tempy room\height = Rnd(0,10) ; set a random height 0 to 10 Next Next ; Move them all over 1 (like the description example) For room.chair = Each chair room\x = room\x + 1 Next
| For some unknown reason youve called a Chair "Room". If you had called it "GlobalChair" or "AChair" or "MyChair" or "LocalChair" or heaven forbid "Chair" it would be a lot better.|
Calling it something not Chair related is bad enough, but to call it something that might contain Chairs is stupid
It a really really stupid way to name an instance of "Chair", because its just confusing the issue between "Is A", and "Contains A".
ARoom may Conatian A Chair, and as such any Type TRoom can have fields of lists of Chairs or Arrays of chairs, or pointers to Banks of chair, in short in may conatin Chairs.
A Chair on the other hand Is Not A room, it is in no way similar to a room (Cept they both might have fleas in them), and shouldnt be called A room at all
| Would be nice to see how to pass a Type to a function. |
| To pass a Type object to a function, just include a pointer for that type as one of the function's parameters.|
Type T_mytype Field name$ End Type my_type.T_mytype = New T_mytype ; Create a new 'T_mytype' custom type object. my_type\name$ = "Bill" Function MyFunction( a_type_pointer.T_mytype ) Print a_type_pointer\name$ End Function MyFunction( my_type ) WaitKey : End
To return a Type object from a function, just declare the function as returning data of that type, and use the Return command to return the data.
Type T_mytype Field name$ End Type my_type.T_mytype = New T_mytype ; Create a new 'T_mytype' custom type object. my_type\name$ = "Bill" Function MyFunction.T_mytype( a_type_pointer.T_mytype ) a_type_pointer\name$ = "Ralph" ; Change the name to 'Ralph'. Return a_type_pointer End Function some_type.T_mytype = MyFunction( my_type ) ; Create a new pointer variable of type T_mytype and ~ ; store the returned pointer in it. ; Note that the 'my_type' and 'some_type' pointer variables now both point to the same ~ ; custom type object. Print some_type\name$ WaitKey : End
Blitz3D Manual Forum
BlitzPlus Equivalent Command