A Beginner's Guide to TypesBlitzPlus Forums/BlitzPlus Tutorials/A Beginner's Guide to Types
| People are always asking about types and how to use them in Blitz, so I tried to make a comprehensive tutorial on types. Although daunting at first, a type can save you a lot of coding time and do many useful things for your game/application.
A type is basically a variable that has a bunch of other variables associated with it. What does this mean? Well, if you were to make 16 enemy ships on a screen, you could have something like:
enemy1x=400 enemy1y=400 enemy1fire=0 enemy2x=300 enemy2y=300 enemy2fire=1
But as you can probably see this is tedious and a lot of typing. But what you also see is that all your enemy ships have essentially the same three variables; x,y, and fire.
Enter the type. The type allows you to make a ship variable that has 3 variables 'within' it. Then, Blitz enables you to alter the parent variable, which alters all the little variables inside of it.
Ok so how do you use types? First, you're going to declare your type and all the 'mini' variables inside of it, using this format:
Type Ship Field x Field y Field fire End Type
As you can see, the 'mini' variables are also known as Fields, and will be referred to as such from now on. So essentially you've made a ship type with three fields, x,y, and fire.
Alright, so you got your type all ready, now lets make a new instance of the type. Like every other variable, you just declare it, but do so in a special notation, like so:
player1.Ship= New Ship
Ok, so what you did here was make what we call a new 'instance' of the type ship. This is basically 'naming' a ship 'player1'. Player1 will have all the attributes of a ship type, an x,y, and a fire variable. Now, to set values to all the fields, we do this:
player1\x=200 player1\y=200 player1\fire=0
Alright, now we have an instance of a ship, named player1, and its three variables have been declared and set to values. You just completed initializing a type.
Alright, now how do you use types? Well there's a couple of things that make types so easy to use and extremely useful in coding. First, you can basically use a type's fields anywhere you would use a normal variable. If you wanted to move the ship left, you would just do:
Its that simple, but you may be wondering "why don't I just go back to using variables?" You remember in the beginning when I said the problem is what if you want to make 100 ships. So far, we've initialized one type. The magic of types comes when you initialize many, and Blitz has special processes that make initializing and using many types at once very easy.
So lets go back to making a type instance. In the first example, we used player1.Ship=New Ship. What we could do however, is put it in a for loop and create many instances with the same name. Check out this code:
For x=0 to 99 player1.ship=New Ship player1\x=x*10 player1\y=x*5 player1\fire=0 Next
Alright so now what this does is make 100 'player1' ship types. This doesn't seem to make sense since you can't have duplicate variable names, but types don't follow that rule. You now have 100 instances of the type Ship with unique x and y variables. Now we need to use it.
Blitz has a special For Loop that deals mainly with types. It uses the Each command. What it does is it basically loops through all your type instances of the same name. For example, player1:
For player1.Ship= Each Ship player1.x=player1.x+1 Next
In this code example, the program scrolls through all the ships called 'player1' and adds one to their x variable, moving them all to the right or whatever it is you want them to do. You can put anything you like into these loops, for example:
For player1.Ship= Each Ship If KeyHit(57) player1\fire=1 Endif Oval player1/x,player1/y,5,5,0 Next
This will set the ships fire variable to 1 if the spacebar is pressed, and draws an oval at the current position of the ship. So, all 100 ships will be drawn onscreen.
Now, you will find that you may need to delete a type instance somewhere in your program, for instance, when a ship dies. In that case, all you do is add the line:
Wherever you want to delete it within your For-Each-Next loop. When you do this, you have to be careful when checking the typeset later on; you need to make sure the instance isn't what Blitz calls Null, or deleted. You'll find out when to use this as you become more experienced with Types.
And that's about all there is to know about types. Here are some points to leave you with:
--When manipulating large typesets, ALWAYS use a For-Each-Next loop. Otherwise you'll receive and error because Blitz has no idea what you are referencing.
--Blitz has special type manipulating commands, First, Last, Before, and After. These are for when you want to pinpoint a specific type instance in your huge group. They are located in your command reference.
--Good luck using types, and don't give up too early! Its hard at first and many programmers come and go on the basis of not understanding types. Don't let this happen to you, and never be afraid to ask questions, especially here, as someone is always willing to help.
| Oh I forgot, CS_TBL made an excellent point in another post that it's a good idea to have a standard for naming types. He suggests using TShip for the ship type, so that if you decide to create an instance named 'ship', you get something like 'ship.TShip', and not 'ship.ship' which can be confusing.
There is no standardized naming convention, but it is important that you pick something that differentiates itself from any instance names and use the same convention you choose throughout your code.