Question about Type Extends Type
BlitzMax Forums/BlitzMax Programming/Question about Type Extends Type
| ||
Type T_Ship Field Model$="Tship",Name$="Tship" Field Pos_Vect,Dir_Vect Field Engine Field Max_Speed# Field Top_Speed# Field Speed# Method T_Thruster() Print "Thruster_Bay Upgrades not Available" End Method Method T_Radar() Print "Radar_Bay Upgrades not Available" End Method Method T_Drone() Print "Drone_Bay Upgrades not Available" End Method End Type Type T_Scout_A Extends T_Ship Field Thruster[] Method T_Thruster() Print "Thruster Level1" End Method End Type Type T_Scout_B Extends T_Ship Field Radar[] Method T_Radar() Print "Radar Level1" End Method End Type Type T_Scout_C Extends T_Ship Field Drone[] Method T_Drone() Print "Drone Level1" End Method End Type Type T_Scout_D Extends T_Scout_A Field Radar[] Method T_Radar() Print "Radar Level1" End Method End Type Type T_Scout_E Extends T_Scout_A Field Drone[] Method T_Drone() Print "Drone Level1" End Method End Type Type T_Scout_F Extends T_Scout_B Field Drone[] Method T_Drone() Print "Drone Level1" End Method End Type Type T_Scout_G Extends T_Scout_D Field Drone[] Method T_Drone() Print "Drone Level1" End Method End Type in the code above i have a Tship which is extended by three main types T_scout_a T_scout_b T_scout_c Is there a way to Create a merged type ie i want T_scout_d to be a combination of T_scout_a + T_scout_b |
| ||
Type T_scout_d Field F1:T_scout_A Field F2:T_Scout_B end type or Type T_Scout_D Extends T_Scout_a Field F1:T_scout_B EndType Not really, in C++ (so I believe) you can do Type extends Typea typeb typec etc, but not in Bmax You have to decide which you most want scout D to "be" and extend from that, and then include the other. Or just include both Personaly I would make two new types Thrusters and rader, and just include then as ness in each type you want |
| ||
| thanks h&k changed the tags :) |
| ||
| If you extend things you are saying the new type IS A old type, ie A Scout_A IS A Ship. so what you are asking for is wrong anyway Scout_D is a scout_A WITH A Radar or Better Scout_D IS A ship, with a radar and thrusters. IF when you say it its a "With A", then make them fields. Make a BASE type, ship comounents. Then TSHIP has a field which is a TLIST of TCOMPOUNENTs Then you extend TCompounent ie TRadar extends TCompounent Then when you want a new ship type you can still extend TShip if you want, but in its create function add the compounets that you want to its TCompounet list |
| ||
| I was seeing if there was a way to use methods from other types ie a merged type Scout_D of Scout_a+Scout_B would have both methods needed for the ship but a Extended Type of Scout_a need you to add a new Radar method to |
| ||
| No, the radar method should be part of a Radar type. Then when you add it to a ship it uses the radar's method, which you have only written once. ATship_D.radar.activate You were totaly right to say "hang on Im writing this again", but it was an indication that the "thing" ie radar should be a seperate object. |
| ||
Type T_Component
Global Component_Blank:T_Component=New T_Component
Global Component_Radar:T_Component=New T_Radar
Global Component_Thruster:T_Component=New T_Thruster
Global Component_Drone:T_Component=New T_Drone
Method Radar()
Print "Radar Mod Not Installed"
End Method
Method Thruster()
Print "Thruster Mod Not Installed"
End Method
Method Drone()
Print "Drone Mod Not Installed"
End Method
End Type
Type T_Radar Extends T_Component
Method Radar()
Print "LEVEL 1 Radar Installed"
End Method
End Type
Type T_Thruster Extends T_Component
Method Thruster()
Print "LEVEL 1 Thruster Installed"
End Method
End Type
Type T_Drone Extends T_Component
Method Drone()
Print "LEVEL 1 Combat Drone Installed"
End Method
End Type
Type NT_ship
Field Name$
Field Class$
Field Radar:T_Component
Field Thruster:T_Component
Field Drone:T_Component
Function Create:NT_Ship(_Name$)
Local i:NT_Ship=New NT_Ship
i.Radar=T_Component.Component_Blank
i.Thruster=T_Component.Component_Blank
i.Drone=T_Component.Component_Blank
i.Name$=_Name$
I.Class$="NT_Ship"
i.ID
Return i
End Function
Method ID()
Print "~n ~n ~n ~n"
Print "Ship Name: "+Name$
Print "Ship Model: "+Class$
'Print "~n"
Radar.Radar()
Thruster.Thruster()
Drone.Drone()
End Method
End Type
Type NT_Scout_A Extends NT_ship
Function Create:NT_Scout_A(_Name$)
Local i:NT_Scout_A=New NT_Scout_A
i.Radar=T_Component.Component_Radar
i.Thruster=T_Component.Component_Blank
i.Drone=T_Component.Component_Blank
i.Name$=_Name$
i.Class$="NT_Scout_A"
i.ID
Return i
End Function
End Type
Type NT_Scout_B Extends NT_ship
Function Create:NT_Ship(_Name$)
Local i:NT_Scout_B=New NT_Scout_B
i.Radar=T_Component.Component_Blank
i.Thruster=T_Component.Component_Thruster
i.Drone=T_Component.Component_Blank
i.Name$=_Name$
I.Class$="NT_Scout_B"
i.ID
Return i
End Function
End Type
Type NT_Scout_C Extends NT_ship
Function Create:NT_Ship(_Name$)
Local i:NT_Scout_C=New NT_Scout_C
i.Radar=T_Component.Component_Blank
i.Thruster=T_Component.Component_Blank
i.Drone=T_Component.Component_Drone
i.Name$=_Name$
I.Class$="NT_Scout_C"
i.ID
Return i
End Function
End Type
Type NT_Scout_D Extends NT_ship
Function Create:NT_Ship(_Name$)
Local i:NT_Scout_D=New NT_Scout_D
i.Radar=T_Component.Component_Radar
i.Thruster=T_Component.Component_Thruster
i.Drone=T_Component.Component_Blank
i.Name$=_Name$
I.Class$="NT_Scout_D"
i.ID
Return i
End Function
End Type
Ship1:NT_Ship=NT_Ship.create("Basic")
Ship2:NT_Ship=NT_Scout_A.create("Ship 1")
Ship2:NT_Ship=NT_Scout_B.create("Ship 2")
Ship2:NT_Ship=NT_Scout_C.create("Ship 3")
Ship2:NT_Ship=NT_Scout_D.create("Ship 4")
had a little attempt at what you said would this be good working practice ? |
| ||
| Its not exactly how I would have done it, (ie TList and a more generic TCommponent), but yes just like that. The way you have done it, makes it harder to add new different types of compounents, but yes I ment like that. The main question is, do you like programming like that? (EDIT: you are now carring "dead weight" in terms of memory, but I would worry that much about it. If you have taken what I said to mean that, then it good enough for now, you can always go back latter and change it, because how it works will be hidden from the rest of the program) |
| ||
| thanks for your reply any chance you could post what your version would be please |
| ||
| Aahah. Ok. Tommorow yes? (its 5:30 am here) |
| ||
| hope to here from u soon :) sleep well btw its 5:43 am here Coventry uk |
| ||
SuperStrict
Type TShipType
Field FCompounents:TList
Function Create:TShipType (ACompounentList:TCompounent[]=Null)
Local Temp:TShipType = New TShipType
If ACompounentList<> Null
Temp.FCompounents = CreateList()
For Local Loop:Int = 0 To (SizeOf (ACompounentList)/4)-1
ListAddLast Temp.FCompounents,ACompounentList[Loop]
Next
EndIf
Return Temp
End Function
Method ActivateCompounent:Int(Flag:String)
If Self.FCompounents <> Null
For Local Compounent:TCompounent=EachIn Self.FCompounents
If Compounent.fType = Flag Then Compounent.Do; Return True
Next
EndIf
Return False
End Method
Method Radar()
If not Self.ActivateCompounent("R") Then Print "No Radar"
End Method
Method Thrust()
If not Self.ActivateCompounent("T") Then Print "No Thrust"
End Method
Method Drone()
If not Self.ActivateCompounent("D") Then Print "No Drone"
End Method
End Type
Type TCompounent
Field fType:String
Field FLevel:Int
Method Do () abstract
End Type
Type TRadar Extends TCompounent
Method New()
fType:String = "R"
End Method
Method Do()
Print "Level "+Self.FLevel+" Radar"
End Method
EndType
Type TThruster Extends TCompounent
Method New()
fType:String = "T"
End Method
Method Do()
Print "Level "+Self.FLevel+" Thruster"
End Method
End Type
Type TDrone Extends TCompounent
Method New()
fType:String = "D"
End Method
Method Do ()
Print "Level "+Self.FLevel+" Drone"
End Method
End Type
'****************** Testing ******************
Global AThruster:TThruster = New TThruster
Global ARadar:TRadar = New TRadar
Global ADrone:TDrone = New TDrone
Global MyCheapShipType:TShipType = TshipType.Create ()
Global MyFastShipType:TShipType = TShipType.Create([TCompounent (AThruster)])
Global MyExpensiveShipType:TShipType = TShipType.Create ([TCompounent (ARadar),TCompounent (AThruster),TCompounent (ADrone)])
MyCheapShipType.Radar
MyFastShipType.Radar
MyExpensiveShipType.Radar
MyCheapShipType.Drone
MyFastShipType.Drone
MyExpensiveShipType.Drone
MyCheapShipType.Thrust
MyFastShipType.Thrust
MyExpensiveShipType.ThrustCouldnt sleep. Im not happy with it, cos there is no easy way to upcast to the object if you dont know what it is in the list. Every new compounent you think off you extend Tcompounent, for that new compounent type. Then you add to TshipType a method to call the "loop through the compounent list" which looks see if your ship has that compounent |
| ||
| woah, thats a bit over the top H&K, I think you must need sleep ;) *EDIT* oi ;) |
| ||
| no its not over the top, it does just what was asked of it. Which yours does not. (Thats not to say yours is wrong, just not what was asked for) It is providing a base for a full Ship type that can have components added to it, and each compounent can be called from the ship type if the ship has it or not. (Edit: Ok yes I was also ediiting it as I was posting it, so maybe you saw a "Draft" and not the final one) But its better than yours ;) Edit Edit: Actually I think you are right. Why didnt I just tell him to do it the way you have, then your way would have been right and I could have done somthing useful like gone to sleep |
| ||
| Well heres what i saw... :P LOL, EDIT THAT! LOL... only joshing mate ;) |
| ||
| Thats not far off the final one. Is the code I finaly posted that bad? I cannot decide anymore. Need Sleep |
| ||
| thanks for your replies guys having a look over both now :) |