Types

BlitzPlus Forums/BlitzPlus Beginners Area/Types

Shuffles(Posted 2008) [#1]
I'm kina stuck on types.I've read the Blitz3D but still a little confusing.If someone could get me a good tutorial on it that'd be awesome.Thanks so far!


Sauer(Posted 2008) [#2]
Alright here we go,

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:
player1\x=player1\x-1

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:
Delete player1

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.

Good luck!
(Thats probably a tutorial right there :) Is it worthy for the tutorials section?)


andy_mc(Posted 2008) [#3]
Very nice and comprehensive.


Sauer(Posted 2008) [#4]
Thank you very much,


Hotshot2005(Posted 2008) [#5]
Great tutorial Sauer Good explain about Type


CS_TBL(Posted 2008) [#6]
For consistency, type names usually start with T, so: TShip instead of Ship. I recall ppl naming their instance 'ship' when they also have a type named 'ship', which is highly confusing. You'd get stuff like: ship.ship = new ship *aargh*

ship.TShip = new TShip is more clear. :P


boomboom(Posted 2008) [#7]
in blitz3d i tended to do it the other way around

tShip.Ship = new Ship

then I knew that tShip was using a type....but I have no conformed to the general BlitzMax way.


Sauer(Posted 2008) [#8]
That is a good programming habit that I never learned, but an excellent point as it does get confusing.