Another object question
BlitzMax Forums/BlitzMax Beginners Area/Another object question
| ||
Ok I am trying to wrap my head around objects... I have a base type that can display an object and handle input. When it is created, it goes into a master list for display items. Now I extend that type do make the varrious display objects do different functions. One type of display object displays a player, another shows stats, etc. All of the functionality is handled here as to what happens when the player interacts with the object. When I create I assume that I call the extended type to create both. Now when I loop through the master display list, do I get the extended type as well as the base display type? Or do I need to have some kind of index to tie them together? Example: type displayitem method showitem() stuff... end method method new() displist.addlast self end method end type type playerselection extends basetype method handleinput() stuff end method method create() return new playerselection end method end type local p:playerselection local a for a = 1 to 1000 p:playerselection = p.create() next Now when I loop through my display list do I do my eachin on playerselection objects, or displayobjects? If I do the loop on displayobjects (since that is what is added to the list) do I get the associated playerselection object as well? I love this object stuff, but this is my first go with it. Neat and really functional! |
| ||
Your code is a bit confusing. If you do your eachin against playerselection it will show all playerselection objects. If you do it against displayitem it will do all playerselection AND any other objects on displist which has been extended from displayitem. I was hoping the colours would show this better but this might help... SetGraphicsDriver GLMax2DDriver() Type displayitem Global displist:TList Field x Field y Method showitem() DrawRect x,y,10,10 End Method Method New() displist.addlast Self End Method End Type Type player1 Extends displayitem Function create() If displist=Null displist=CreateList() p1:player1 = New player1 p1.x = Rand(1,200) p1.y = Rand(1,200) displist.addlast p1 End Function End Type Type player2 Extends displayitem Function create() If displist=Null displist=CreateList() p2:player2= New player2 p2.x = Rand(1,200) p2.y = Rand(1,200) displist.addlast p2 End Function End Type Graphics 640,480 SeedRnd MilliSecs() Local p1:player1 Local p2:player2 Local a For a = 1 To 10 player1.create() Next For a = 1 To 10 player2.create() Next Cls SetColor 255,0,0 DrawText "Player 1 types",0,0 For my1:player1 = EachIn player1.displist my1.showitem() Next Flip WaitChar SetColor 0,0,255 DrawText "and Player 2 types",0,10 DrawText "Hit any key",0,30 For my2:player2 = EachIn player2.displist my2.showitem() Next Flip WaitChar Cls SetColor 0,255,0 DrawText "and displayitem",0,0 For my3:displayitem = EachIn displayitem.displist my3.showitem() Next Flip WaitChar End <edit> that's better. Good ole GLdriver |
| ||
[quote]If I do the loop on displayobjects (since that is what is added to the list) do I get the associated playerselection object as well?[quote]Ys and no. Here's how polymorphism works: If you itterate through your basetype (in your example), you do get the extended types, however you can only directly manipulate objects through methods defined in the basetypes interface. So in your case you would not be able to handleinput, without a specific downcast, however if the playerselection objects had a different implementation of showitem() than the basetype, then the playerselection version would be the one invoked. |
| ||
Ah I think I understand. If I wanted to call the extended type version of a method, I need to have one in the base method. Then when I loop through the base menthod, I get the associated extended method and can use the code for handling input (from my example). Thanks again, this is a really odd way of thinking :) |