Using Extends/Inheritance

BlitzMax Forums/BlitzMax Beginners Area/Using Extends/Inheritance

Ghost Dancer(Posted 2005) [#1]
OK, I have the following two types (I've stripped them down for ease of reading):

Type TMasterUnit
	Field name$, description$, img:TImage
	
	Field unitType$, squadSize, ap, minSpeed, maxSpeed, radius, hp, defense, bays
	Field hit, hitType$, attacks, shots, minDamage, maxDamage, minRange, maxRange
End Type


Type TPlayerUnit Extends TMasterUnit
	Field player, curStatus
	Field x, y, rot
End Type


I have a list of 10 Master Units (e.g. tanks, planes etc.) with preloaded data - these just store the info for each type of unit.

I'm then trying to create a list of player units which inherit the data from Master Units (e.g. I don't want to have to reload the same data) but I'm getting an error as indicated below (again, I've stripped this down a bit):

For Local p = 1 To 4
	For Local mu:TMasterUnit = EachIn masterUnits
		Local pu:TPlayerUnit = mu  'ERROR HERE
		'unable to convert from TMasterUnit to TPlayuerUnit
		
		pu.player = p
		
		ListAddLast(playerUnits, pu)
	Next	
Next


So basically I'm trying to create a new TPlayerUnit from an existing TMasterUnit so the TPlayerUnit not only inherits the type definition but the data as well. I'm still new to OOP, but I the way I understand it, this is acheivable, but I guess the syntax is incorrect.

Does anyone know the correct way of doing this?


Azathoth(Posted 2005) [#2]
You'd have to create them as TPlayerUnits to start with if you want to just convert the data over, otherwise a function that copies the data and creates new instances of TPlayerUnit.


Lomat(Posted 2005) [#3]
What you need to do is add a create function to the TPlayerUnit type that will take an existing TMasterUnit object and create the required TPlayerUnit fom that base object. Example...

Type TPlayerUnit Extends TMasterUnit
	Field player, curStatus
	Field x, y, rot

	Function createFromMaster:TPlayerUnit(master:TMasterUnit)
		Local unit = New TPlayerUnit
		unit.name = master.name
		unit.description = master.description
		unit.img = master.img
		unit.unitType = master.unitType
		unit.squadSize  = master.squadSize
		unit.ap = master.ap
		unit.minSpeed  = maser.minSpeed
		unit.maxSpeed = master.maxSpeed
		unit.radius = master.radius
		unit.hp = master.hp
		unit.defense = master.defense
		unit.bays = master.bays
		unit.hit = master.hit
		unit.hitType = master.hitType
		unit.attacks = master.attacks
		unit.shots = master.shots
		unit.minDamage = master.minDamage
		unit.maxDamage = master.maxDamage
		unit.minRange = master.minRange
		unit.maxRange = master.maxRange
		return unit
	End Function
End Type

' Let m = a master unit object

Local a:TPlayerUnit = TPlayerUnit.createFromMaster(m)
Local b:TPlayerUnit = TPlayerUnit.createFromMaster(m)
Local c:TPlayerUnit = TPlayerUnit.createFromMaster(m)



The above example will create a copy of each field for each object. If you are planing to have lots of opjects then it might be worth thinking about going down a different route where you have your TPlayerUnit and you assign to it the TMasterUnit and then define methods to access the properties of the TMasterUnit and therefoer only every have one set of 'fields' allocated in memory. Example...

Type TPlayerUnit
	' We do NOT extend the master type.
	Field player, curStatus
	Field x, y, rot
	Field master:TMasterUnit

	Method setMaster(m:TMasterUnit)
		master = m
	End Method

	Method getName:String()
		return master.name
	End Method

	Method getDescription:String()
		return master.description
	End Method

	' .... add all methods required ...

End Type

Local a:TPlayerUnit =new TPlayerUnit
a.setMaster(m)

Local b:TPlayerUnit =new TPlayerUnit
b.setMaster(m)

Local c:TPlayerUnit =new TPlayerUnit
c.setMaster(m)




Ghost Dancer(Posted 2005) [#4]
Thanks for your replies. I think I misunderstood how it worked but after a bit of experimentation I think I have it sussed.

I did originally do it a similar way to your 2nd suggestion lomat, but I was using an array of TMasterUnit but the code started getting ugly whenever I needed to look up values.

As I said, OO is still new to me so I'm still learning some of the concepts. So if I understand correctly with your second method, to create my player objects as per my example, I would do something like:

For Local p = 1 To 4
	For Local mu:TMasterUnit = EachIn masterUnits
		Local pu:TPlayerUnit = new TPlayerUnit
		pu.setMaster(mu)
		
		pu.player = p
		
		ListAddLast(playerUnits, pu)
	Next	
Next