Using Extends/Inheritance

BlitzMax Forums/BlitzMax Beginners Area/Using Extends/Inheritance

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)