Inheritance, object type conversion

BlitzMax Forums/BlitzMax Beginners Area/Inheritance, object type conversion

abdedf(Posted 2010) [#1]
The error is "Identifier 'speed' not found. How do I access this variable?

Global list:TList=CreateList()

Type TGameObject
	Field x:Int,y:Int
End Type

Type TPlane Extends TGameObject
	Field speed:Int
End Type

Local newobject:TPlane=New TPlane
ListAddFirst list,newobject

For eachobject:TGameObject=EachIn list
	If TPlane(eachobject)
		eachobject.speed=5
	End If
Next




ima747(Posted 2010) [#2]
For eachobject:TGameObject=EachIn list
	If eachobject
		TPlane(eachobject).speed=5
	End If
Next


you have to typecast it when you use it, it's only valid when it's cast, it doesn't remember that you accessed it as a specific type later.

you could also
For eachobject:TPlane=EachIn list
	If eachobject
		eachobject.speed=5
	End If
Next


depends how you're going to handle it moving forward etc... I haven't played with mixed TLists in a while (please someone correct me if I'm wrong) but the second example *should* only get objects of type TPlane from the list... don't hold me to that thought, like I said, been a while :0)


abdedf(Posted 2010) [#3]
The second way works exactly as you described.

Thanks.


Jesse(Posted 2010) [#4]
the first would give erros. it should have been like this:
	If Tplane(eachobject)
		TPlane(eachobject).speed=5
	End If



GW(Posted 2010) [#5]
The "If eachobject" statements are redundant.


Czar Flavius(Posted 2010) [#6]
If TPlane(eachobject) checks that eachobject can act as a plane, before you use it as a plane (a good idea)

If eachobject on its own is redundant, as GW says.


ima747(Posted 2010) [#7]
good catch jesse and good explination czar. For whatever reason I thought it just needed to check to see if the object exists so I dropped the typecast... pointless since for it to come out of the list it has to exist... brain fart on my part.