Wave's~ BlitzMax Tutorial for NG | ~ November, 2015 ~ Version 11 |
Beginners guide to BlitzMax | |
Extend Types | |
I assume that you are aware that fields, globals, functions and methods inside Types can have the same names as methods, functions in other Types. It is possible to have Car.Create() Tank.Create() and Animal.Create(); three methods which are completely different but have the same name. I have decided I'm going to use a spaceship approach in this example because it feels game related and it is easy to picture and explain the type-structure when it comes to spaceships. First I'm going to setup an example where I don't use inheritance. Then I'll use inheritance to hopefully simplify the example. | |
Example Without Inheritance: | |
'This is a fighter a quick and small ship Type Fighter Field X#,Y# Field Xspeed#,Yspeed# Field ID% Field Armor% Field Fleet$,Name$ Field WeaponSelected$ Global Gfx_Ship 'Graphics Global Gfx_Thrusters Global Sfx_Thrust ' Sound Global Sfx_Explode Field SheildRechargeRate#=0.1 Field Energy%=500 Field WeaponUpgrade% Field PowerUpgrade% Field Fuel% Field Scanner Method DockWithCruiser() '... End Method Method SelfDestruct() Armor = 0 PlaySound Sfx_Explode Explosion( X, Y) End Method Method Update() X:+Xspeed Y:+Yspeed End Method End Type 'Cruisers are big support ships Type Cruiser Field X#,Y# Field Xspeed#,Yspeed# Field ID% Field Armor% Field Fleet$,Name$ Field WeaponSelected$ Global Gfx_Ship 'Graphics Global Gfx_Thrusters Global Sfx_Thrust ' Sound Global Sfx_Explode Field SheildPower Field Reactor$ Field CrewNR% Field TractorBeamUpgrade% Field CloakingDeviceON=False Field MissileSlots[4] Method UseTractor(F:Fighter) '... End Method Method UseTractor(C:Cruiser) '... End Method Method SelfDestruct() Armor = 0 PlaySound Sfx_Explode Explosion( X, Y) End Method Method Update() X:+Xspeed Y:+Yspeed End Method End Type | |
With OO inheritance I would first create a Type Ship. Then I would extend this Ship into a Cruiser and a Fighter. You can also go a step longer and first create a type SpaceObject, The Ship can then extend SpaceObject. Comets and missiles can also extend SpaceObject in that case. | |
Example Using Inheritance: | |
Type Ship Field X#,Y# Field Xspeed#,Yspeed# Field ID% Field Armor% Field Fleet$,Name$ Field WeaponSelected$ Global Gfx_Ship 'Graphics Global Gfx_Thrusters Global Sfx_Thrust ' Sound Global Sfx_Explode Method SelfDestruct() Armor = 0 PlaySound Sfx_Explode Explosion( X, Y) End Method Method Update() X:+Xspeed Y:+Yspeed End Method End Type Type Fighter Extends Ship Field SheildRechargeRate#=0.1 Field Energy%=500 Field WeaponUpgrade% Field PowerUpgrade% Field Fuel% Field Scanner Method DockWithCruiser() '... End Method End Type Type Cruiser Extends Ship Field SheildPower Field Reactor$ Field CrewNR% Field TractorBeamUpgrade% Field CloakingDeviceON=False Field MissileSlots[4] Method UseTractor(S:Ship) 'Accept a cruiser or a Fighter (Any type that extends ship) '... End Method End Type | |
If we look at fighter it will have all fields, methods and functions that ship has plus the new ones in the extended Fighter-Type. F:Fighter = New Fighter; for the programmer this fighter will look just like the Fighter in the first example. An object oriented code will make it easier to sort out stuff like this. In this sample I used some dummy methods but when you do even medium size games the size of your types and the number of their methods will be extensive. So an OO approach is highly recommended! I would also suggest that you use OO when doing small games. If you add the Ship-type to a list each time a new ship is created, you'll be able to loop thru every Ship but you'll also be able to loop every fighter or every cruiser. This is a fast and easy way to access a lot of objects in real-time. OO also has the plus side that you can easily transport code from one project to another. Let's say you are making another space game. Just move your Ship-Type to that file, implement it using your functions and methods and reuse your code in a fast and quite simple way. You'll soon discover (if you haven't already) that parts of your games/apps will almost be stand-alone and can be used in several other games/apps. Then you can make a module of that part and implement it as a part of BlitzMax. There is already a tutorial on how to create your own module in BlitzMax. I recommend you check it out. | |
To Index | Next Page | page 13 |