Types n stuff
BlitzMax Forums/BlitzMax Programming/Types n stuff
| ||
I have a type "Tiles" and two sub types that extend it called "background" and "foreground"Type Tiles Field X,Y Method Draw() End Method End Type Global Tile_List:TList = CreateList() the sub types have their own draw methods ' Back Type B_Type Extends Tiles Field F Method Draw() If x > Screen_X - Screen_Border And x < Screen_X_Max + Screen_Border If y > Screen_Y - Screen_Border And y < Screen_Y_Max + Screen_Border DrawImage gfxTileset,x,y,f EndIf EndIf End Method End Type and when I create one of either sub types they get added to the main list Tile_List.AddLast( NewB ) I want to be able to easily draw all of the tiles with one command... so I was thinking For Local Tile:Tiles = EachIn Tile_List Tile.Draw() Next However, this just runs the Draw method from the Tiles type. If I remove said method, the command "Tile.Draw()" doesnt work. But I want each entry in the list to run its own Draw method. What am i doing wrong? ta! |
| ||
Althouth the list excepts both subtypes, because they are tiles. The draw that you have overriddien in b_Type wont be called. Because the list is a list of tiles. Not B Types |
| ||
Ahh got ya... so basically, I may as well just have two completely separate types, ya? |
| ||
No, I think its doable with just one list, I think you put a field in ttile, that lets you know what each is. And casting always gets me, So maybe you can cast the B_Type back. Chris. I really dont know. Im not very good at this. Wait till one of the 'sperts answer ;) |
| ||
hehe ok, thanks for ya time anyway :) Its something I will definately need for other objects in the game but it would be cool to get it working on simple terms first. So anyone else got any ideas? ta! |
| ||
Damnit, erm.... sorry. Ive found the problem and as I suspected very early on but at the same time couldnt work out why..... the reason it didnt work was because I am a complete arse. The "theory" I was going for does actually work perfectly though, which rocks :D |
| ||
Well post the code then. And put me out of me missery |
| ||
The above code should indeed work. Defining the list iterator as a super type will allow it to loop thru all instances of the super type *and* all its sub-types. (Tiles being a super-type, B_Type being a sub-type) Annoying, isn't it, when you *know* something should work in a particular way, and it doesn't... and always the last thing that dawns on you is that you may have coded it wrong somewhere along the line ;-) |
| ||
thats the thing H&K the code I posted was actually spot on, it was some other part that I borked. Note to self, use Strict. ;) Ill give that super thing a look too ta Brucey. Currently I am just adding a newly create object to two (or more) lists. This Extend stuff is really impressing me though, makes it so easy to define which objects i want to draw and which I dont. |
| ||
H&K, go through my tutorials here. The concepts are explained there good luck |
| ||
Method Draw() Abstract that will fix it |
| ||
Assari, Ive looked thought all you tutorials, and whilst they are undoubtably good, they fail me utterly in one key feature. They are not memorable. I dont mean offence by this, as Im only able to use MaxGui at all because of them. But they just dont seem to sync with me. |
| ||
Maybe this tutorial will help: http://www.blitzbasic.com/Community/posts.php?topic=59233 It describes inheritance and polymorphism and how they are best used in situations like this. |