Wave's~ BlitzMax Tutorial for NG~ November, 2015 ~ Version 11
Beginners guide to BlitzMax 

Types
A type is like a structure or a blueprint of variables. It's like a skeleton you define, you can then use this skeleton for animals or objects you create. Types are the part of BlitzMax which makes it an OOP (Object Oriented Programming) language. Types are one of the hardest parts to get around if you are new to OOP or programming in general. Make a new file if you want to try the examples that follow.
 
Setup and Create Types
let's say we want a spaceship then let's first create a structure for it:
Type spaceship 'declares a new type
  'this is what the structure contains:
  Global fleet$
  Field maxspeed#
  Field armor%=2000
  Field name$
End Type 'ends declaration

Note that above is the same as this:
Type spaceship
  Global fleet:String
  Field maxspeed:Float
  Field armor:Int=2000
  Field name:String
End Type

There is no rule that says we need to have our type at the top of our file, types are like functions in this case. They can be declared almost anywhere except inside of another type. You can even keep them in separate files which is common when working with oop.

Here I'll declare the variable to use, "ship".
Local ship:spaceship
This did not create a ship we merely made a placeholder for one. We are now ready to create a spaceship, which will be an instance of our structure; an object.
ship = New spaceship
What I did above is declare a variable ship to use the spaceship structure. Then I created a new spaceship which is an object (an instance of spaceship) and I put this new spaceship into the variable ship (at least the address to it). I can now set the fields of this object using ship.spaceshipfieldname. If we don't create a new ship, our ship will be = null; it will not exist. To access the field of this new object called ship we use dot, you can see it as if the dot means "step into".
ship.maxspeed = 3.5 'Read: Ship's MaxSpeed set to 3.5
Ship.Armor = 5000 'Default was 2000 (See the type)
Ship.Name = "wavebreaker"

let's create another ship:
Local ship2:spaceship = New SpaceShip
Ship2.MaxSpeed = 7.5
Let's use the default armor value (see SpaceShip-Type if unsure)
ship2.name = "Starbreaker"

Now I have two objects of the SpaceShip Type, Ship and Ship2. I can always use the fields of these objects as a variable.

For example:
If ship.maxspeed > 200 Then Ship.Name = "LightBreaker"

Note that if I would set Ship2 = Ship then both Ship and Ship2 would point to Ship, in that case Ship2 would be lost and there would be no way to access it, and it will be automatically deleted by BlitzMax. Ship/Ship2 is simply the addresses to these objects. One object can have several pointers to it. The object itself is unique but the address to it can reside in several different variables. Think about it.

Let's continue on our space ship example:
SpaceShip.Fleet = "Quantum Light" 'refer To the Global from the Type itself
Print "Name: " +Ship.Name
Print "MaxSpeed: " +Ship.MaxSpeed
Print "Armor: " +Ship.Armor
Print "Fleet Name: " +Ship.Fleet
Print "---------------"
Print "Name: " +Ship2.Name
Print "MaxSpeed: " +Ship2.MaxSpeed
Print "Armor: " +Ship2.Armor
Print "Fleet Name: " +Ship2.Fleet
Note the Global Fleet$. Because Fleet is a global in the SpaceShip type it is shared among all Instances of Spaceship. If one SpaceShip would change it's Fleet so would all other SpaceShips. Constants act in the same way, except they can't change. Because a global is global and not bound to an instance it can be altered even if you don't have any instances of the type in question.
Try moving  SpaceShip.Fleet = "Quantum Light"  to the top of the file.
 
List types
Types store data but what if I want to store types? You can always have types in types. But often we want to loop our types, move all spaceships. Sometimes we want to sort them. You might think that arrays would be the perfect choice, and so far it seems like a perfect fit. Arrays can contain objects, any object including your homemade.

If we collect all our objects into one place we would not need to refer to them with individual names, like ship1, ship2. We could use a eachin-loop and loop all objects.

The solution is called Lists. Lists are more dynamic than arrays and therefore offer a great advantage that makes your code easier. It's all about removing and adding objects. Arrays have a set length, lists do not. The length of a list is depending on the number of objects inside it. Study code below to see example.

Create a new file. Paste, Read, Build, Read, Learn.
Strict
Global Number_of_Tanks = 10 'How many tanks to create?
 
' Having a Global like the one above makes it easy To change how the program
' acts at least in small examples like this. Now let's declare a new type
' called Tank.
 
Type TTank 'The extra T infront of the type name is good use
  Field X#, Y#
  Field Dir, Armor = 100
  Field Speed# = 0.2, Size = 25
End Type
 
Graphics 800,600
 
Global TankList:TList = CreateList()'Create a list to store all tanks
 
' TankList:TList defines TankList To be of the List-Type, CreateList() returns
' a New List. The variable TankList will be used whenever you want To add
' remove or alter this TList.
' Note that TList is a BlitzMax built-in Type And it has it's own methods and
' functions.
 
' Create a bunch of new Tanks
For Local N = 1 To number_of_tanks ' Number_of_Tanks is a Global
  Local NewTank:TTank 'Declares a variable to store our Tank-Type
  'Put the address of this new Tank into variable NewTank
  NewTank = New TTank
  'Set a random Armor 150 + 10,20,30,40 or 50
  NewTank.Armor = Rand( 5 )*10 + 150
  'Random Start Location
  NewTank.X = Rand( 5, 800 )
  NewTank.Y =
Rand( 5, 600)
  NewTank.Dir = Rand( 0, 360 )
  'Put this tank called NewTank into our TankList
  ListAddLast( TankList, NewTank )
Next
 
While Not KeyDown(Key_Escape)
  Cls
 
  'Local T is declared to hold the current Tank each loop
  'Because we called our Type TTank, Tank is availible as variable
  For Local Tank:TTank = EachIn TankList
    DrawOval( Tank.X, Tank.Y, Tank.Size, Tank.Size )
    DrawText "Number of Tanks : "+TankList.Count(), 20, 20
    DrawText "Press ESC to exit", 20, 40 '20 is X-coordinate, 40 is Y
    Tank.X:+ Tank.Speed*Cos( Tank.Dir )
    Tank.Y:+ Tank.Speed*Sin( Tank.Dir )
  Next 'This loop will loop for every Tank that was added to TankList
 
  Flip
Wend
In this way you can change all your tanks in an Eachin-loop. Note that in the above example Number_of_Tanks could have been a constant and it could have been inside the Tank-Type. Also the Global TankList could have been put inside the Type with success.
 
To Index | Next Page page 9