Here we go, the long awaited lesson on types. Types are very difficult to understand, and even more difficult to use properly. Don’t feel stupid if you have to read parts of this tutorial multiple times. If you have any questions, just reply to this thread and I will answer them.
Okay, to start off, types are a way to store variables, and are especially useful if you need to create multiple variables. For instance, how many of you have ever played Galaga, or Space Invaders? For those games, at any given point in the game, there could be enemy ships on the screen. For each of the ships on the screen, think of all the variables that are required to use it. For instance, a variable called shipx could be used to store the x coordinate, and another variable called shipy could be used to store the why coordinate. A third variable called shipimage could contain the image for the ship. Now, if you really want to, you might be able to go through the game, and each time you create an enemy ship, you could define the following:
shipx = 100
shipy = 100
shipimage = LoadImage(“shipimage.bmp”)
however, this would also require much more work to update each and every ship. Imagine that you have 30 ships on the screen at once. Without using types, is there a way to move all of the ships in the direction that they are supposed to be moving in? No, you can’t.
If you need to create enemy ships like the ones I defined above, there is a much easier way to do this using types. To define a type, you must use the following protocol, and I will use the ship example to illustrate that.
NOTE: any text in programming code preceded by a semicolon is called a comment. This is to show what that piece of the program does, and it does not affect the way the program runs. If you are familiar with python, in python you could do the same thing with a pound sign (#).
Type ship
Field x,y ;the ship’s location
Field image ;the ship’s image
End Type
There. Now you have successfully created your first type. Now, to access the elements of the type. Let’s say you need to place an enemy ship at the coordinate (200,200). In a real game, you would most likely have a variable that would contain the coordinates, but for this example, we will use the specific coordinates. First of all, creating a new type that has all of the variables defined by Type ship in it is declared like this:
instancename.typename = New typename
Using our example of ship, let’s create a new set of variables named enemy with the parameters of ship.
enemy.ship = New ship
now, there have been three new variables created. The x value, the why value, and the image. To access a variable within a type, the protocol is this:
instancename\variablename
also, be careful not to confuse a backslash (\) and a division sign (/)
now, we have to change the coordinates of the enemy to (200,200). To do this, we need to set the variables within the type.
enemy\x = 200
enemy\y = 200
and it’s just that simple!
Now, what if you need to update all 30 or so of the enemy ships you have on the screen? To even create 30 ships, you would need to set up a For loop that would create 30 different ships, and then declare an array that would determine where to put the ships (yes, I do know how to do this, but I have neither the time nor the energy to do it right now. I will continue this tutorial into an arrays tutorial later). Since I haven’t explained how to do this yet, we will skip straight to the important part. Let’s say that in this type, there is another variable called hitpoints, and you can probably guess what it’s for. Let’s say that in your game, you would want to have it check every frame whether or not the any of the enemy ships are dead or not. Here’s how you would go about doing that. This is called a For-Each-Next loop:
For enemy.ship = Each ship
If hitpoints <= 0
Delete enemy
EndIf
Next
What this loop does is it checks every enemy ship, and if its hit points are less than or equal to 0, than it is deleted. See? Types aren’t that hard!
This concludes today’s lesson, but feel free to tune in next time when I give a tutorial on arrays that connects to this lesson.
Enjoy!