The idea behind custom types is to 'wrap up' a bunch of data into one 'object'. This VERY un-BASIC feature is actually pretty useful once you get used to it!
Typically, BASIC programmers use multiple arrays for handling common data.
For example, say your game features aliens, each of which has an 'x', 'y' and 'kind' property. A common technique for handling situations like this in BASIC is to set up separate arrays for each property. For example:Dim Alien_x(100),Alien_y(100),Alien_kind(100)Custom types allow you to tackle this kind of thing in a different way. Instead of spreading all your alien properties out across different arrays, you instead create a custom type that contains all these properties. For example:
Type Alien
Field x,y
Field kind
End TypeNote that when you define a custom type, the only thing you can place inside the 'Type...End Type' are Field commands.
Each 'Field' command is followed by a list of variables, and these make up the 'contents' of the custom type.
The fields may themselves be of any type - integer, string, floating point, or even another custom type (more on this later).
Its important to understand that the 'Type' command only describes an Alien. To actually create an Alien we have to use the 'New' command:
a.Alien=New AlienThe 'New' command creates a new Alien for us to use. 'New' is always followed by the name of a custom type, and it returns what is known as an 'object' of that type. The 'a.Alien' bit is simply the name of a variable we want to store our new alien - or object - into.
Once we have created an Alien, we can start using it's fields, like this...
a\x=Rnd(640)
a\y=Rnd(480)
a\kind=1The '\' character is used to access the fields of an object.
The fields can be used just like an ordinary variable, so we can do things like...a\x=a\x+1
DrawAlien( a\x,a\y,a\kind )...and so on.
Once you've finished with an object, you should use the 'Delete' command to get rid of it, for example:
a.Alien=New Alien
Delete aThis code creates an Alien, then promptly deletes it - not very useful! Typically, you'd delete an Alien when it was shot by the player, or when the game has ended.
But what if we want more than 1 Alien? How about 50 aliens? Well, there are 2 ways you can go about this. First, you could create an array of aliens, like this:
Dim aliens.Alien(50)
For k=1 To 50 aliens(k)=New Alien
aliens(k)\x=Rnd(640)
aliens(k)\y=Rnd(480)
aliens(k)\kind=1 NextHowever, Blitz has a nifty features that allows you to keep track of all Aliens with very little fuss!
Whenever you create a new Alien, Blitz automatically adds it to a special list called a 'type list'. Each custom type you in your program has it's own type list, and this allows you to get at ALL the aliens (or bullets, or players or whatever...) without having to keep track of them in an array.
The simplest way to access a type list is through a 'For Each...Next' loop, like this:
For a.Alien=Each Alien a\x=a\x+1 DrawAlien( a\x,a\y,a\kind ) NextThis chunk of code will loop through all the Alien objects you have created using 'New', and assign each of them in turn to the variable 'a.Alien'. It's up to you what you put in such a loop, but typically it might involve moving or drawing the object.
When you delete an Alien, it is automaticaly removed from the Alien type list, so will not appear in any futute 'For ... Each' loops.
There are plenty of other nifty things you can do with type lists - check out the blitz language reference for more.
It is important to remember that the ONLY way to create an object is with the 'New' command. This can lead to some surprising results. For example...
Type Test ;define custom type 'Test'
Field x ;Test has only one field...x
End Type
a.Test=New Test ;create an Test object
b.Test=a
a\x=10
Print b\xIf you run this, you may be mildly surprised to discover it prints '10
The key concept involved here is indirection. The variables 'a' and 'b' both refer to the SAME object, so any changes made to either 'a' or 'b's fields will be reflected in the other.
If you're finding all this a bit confusing - DON'T PANIC - If you're used to arrays for handling your data, and all this fancy custom type stuff just sounds like a big headache, then feel free to stick with arrays.
Custom types, like many programming tools, are just a means to an end...