Improving Types in this Code

BlitzMax Forums/BlitzMax Beginners Area/Improving Types in this Code

Type Weapon
   field Name:string
   field atk!,str!,skl!

   method fireWeapon()
        'A universal function to fire any weapon.
   end method

end Type

Type Bronze Extends Weapon

    method New()
       Name = "Bronze"
       str=20
       skl=30
       atk=20
    end method

   method FireWeapon()
        'but for special weapons you can circumvent the base fire weapon method
   end method

end Type

type sword extends weapon
 
   method new()
     name="blah."
     skl = 20 'etc
   end method
 
   method fireWeapon()
       super.fireWeapon()
       'or simply extend it by calling it using the super keyword.
   end method

end type




now you can share different weapons, as they are from the same base.

Temp:Weapon = New Bronze 'will compile fine.

You can then convert weapons to and from base/child classes to obtain their specific functions.

Temp:Weapon = new bronze
Brn:Bronze = Bronze( Temp )

'bronze is not a function above, but a cast.