draw method in parent type

BlitzMax Forums/BlitzMax Programming/draw method in parent type

slenkar(Posted 2007) [#1]
I have a parent type called spaceobject
there are 2 types that extend from it:
ship
and
asteroid

if I have a global image in ship-type and asteroid-type how do I draw them from the parent type 'spaceobject'

it says 'not found' when I try to do it.
it only works if I put the image in a field, but that means loading a new image for each type instance


TaskMaster(Posted 2007) [#2]
Make your draw function abstract in the space object, and the asteroid and ship types have their own draw functions that draw their object.

Or, put the two graphics in the spaceobject type.


ziggy(Posted 2007) [#3]
Being globals, them can be drawed 'intance-free'. So you could do something like:
DrawImage(TMyType.MyGlobalImage,x,y)

from anywhere, including the spaceobject class.


nino(Posted 2007) [#4]
what youre looking for is

Type Tobj
Field x,y
Method draw()
    Plot(x,y)
EndMethod
EndType

Type Tship Extends Tobj
Field img:Timage=yourGlobalShipImage
Method draw()
    DrawImage(img,x,y)
EndMethod
EndType


Type Trock Extends Tobj
Field img:Timage=yourGlobalRockImage
Method draw()
    DrawImage(img,x,y)
EndMethod
EndType

Global myObjs:Tlist=New Tlist

While Not AppTerminate()
    For Local o:Tobj=EachIn myObjs
        o.draw()
    Next
Wend


--edit to add: you put your Tship and Trocks in the list and it will auto use the right draw() in case that wasnt obvious


Azathoth(Posted 2007) [#5]
it only works if I put the image in a field, but that means loading a new image for each type instance
Whats wrong with reusing the same reference to the image?


TaskMaster(Posted 2007) [#6]
Are you saying that if you load the same image over and over, it is actually only loaded once and every time after that only gets a reference to the one loaded image?

Is this true, confirmed?


Who was John Galt?(Posted 2007) [#7]
No it's not. He's saying just load the image once and have more than one reference pointing to the same image.

What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing.


Azathoth(Posted 2007) [#8]
What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing.
From other topics people have said the new field exists along with the old one, and can be confusing as to which one you're accessing.


TaskMaster(Posted 2007) [#9]
I see, that is how I normally do it.

Thanks.


nino(Posted 2007) [#10]

What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing.



My code will work but you are right the semantics are wacky and not the clearest way to handle it.

Having an img field in Tobj makes a lot more sense. Set that img to whatever in a constructor is a more proper way to do it.


nino(Posted 2007) [#11]

I would usually avoid this type of thing.



There is no reason to avoid this kind of thing. It is a powerful tool called polymorphism. http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

The only place I would avoid it is if youre using multiple inheritance which BMax does not support.

multiple inheritance + polymorphism = oo spaghetti code.


Azathoth(Posted 2007) [#12]
There is no reason to avoid this kind of thing. It is a powerful tool called polymorphism.
Sure there is. Blitzmax doesn't support re-declaring a field of the base type, it creates a new field instead.


nino(Posted 2007) [#13]
why is that bad?
isn't that how i can call Super.whatever() and it works?


Azathoth(Posted 2007) [#14]
No, thats a method. A field is like a variable in a type.


nino(Posted 2007) [#15]
wasn't that what we were talking about ?

draw method in parent type


in my code it's the draw() method that's polyed


Azathoth(Posted 2007) [#16]
The person you quoted asked what would happen if a field gets re-declared, followed by what you quoted
I would usually avoid this type of thing.

I took it they were refering to re-declaring a field.