What Type is a Type?

BlitzMax Forums/BlitzMax Beginners Area/What Type is a Type?

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

Brucey, if you have some code feel free to post it.

I guess that the problem has to do with casting, but I'm unsure. If I get it right you know how to do it in Java but not in BlitzMax? I put together a sample to show you:
Type Vehicle
	Global Creator$ = " FX-Factory"
	Field Broken=False	
	
	Method Test()
		If Not Broken Print "Vehicle works!" Else Print "It's broken!"
	End Method

	'Self refers to the Type calling
	'Example:
	'Bus.Collide(Helicopter) 
	'Bus is refered to as Self and Helicopter as V
	Method Collide(V:Vehicle)
		If Car(Self) And Car(V) Print "Car Collide with Car"
		If Car(Self) And Bus(V) Print "Car Collide with Bus"
		If Bus(Self) And Car(V) Print "Bus Collide with Car"		
		If Bus(Self) And Bus(V) Print "Bus Collide with Bus"
		Broken=True'Note that Self. is not needed here
		V.Broken=True
	EndMethod
	
End Type

Type Car Extends Vehicle
	Method Test()
		If Not Broken Print "Car works!" Else Print "Car is broken!"
	End Method	
End Type

Type Bus Extends Vehicle
	Method Test()
		If Not Broken Print "Bus works!" Else Print "Car is broken!"
	End Method
End Type

C:Car = New Car
B:Bus = New Bus

B.Collide(C)

Car2:Car = New Car
Bus2:Bus = New Bus

Bus2.Collide(B)
Car2.Collide(Bus2)
Car2.Collide(C)

Car2.Test
C.Test
B.Test
Bus2.Test



Overhead in casting?
When you cast an object TypeName( TypeToCastTo ) you get 1 if success and 0 if failed, it's as simple as that. "Object" is the base-type of all types an refers to all objects. So what your trying to do, which sound like a smart way to organize your program, should work fine!

Maybe I should add this to my tutorial?