Inheritance
BlitzMax Forums/BlitzMax Programming/Inheritance
| ||
I'm having inheritance problems, heres an example that hopefully is indicative of my problems. I have a type GameObject: Type GameObject Abstract Field ID Field dead Method update(deltaT#) Abstract Method setInput(inputData:TList) Abstract Method getOutput:TList() Abstract Method draw() Abstract End Type And I have a projectile which extends it: Type Projectile Extends GameObject Method update(deltaT:Float) Print "updating" End Method Method setInput(inputData:TList) End Method Method getOutput:TList() End Method Method draw() print "drawing" End Method Function create:projectile(info:projectileInfo, firerID, targetBase, targetOther, x#, y#, rot#, vx#=0, vy#=0, vrot#=0) End Function End Type I have a bunch of projectiles in a list, and I run through the list calling ".update()" on them and I don't get any print out. Anyone know what is wrong, and have any sage tips on how inheritance in bmax might be a bit unusual? |
| ||
You have a list of projectiles or Gameobjects? if gameobjects you need to cast to pprojectiles a= projectile (The specific GameObject) |
| ||
Its a list of projectiles. Now the plot thickens, however: I did a simple test: 1) I modified Projectile so that it doesn't extend GameObject 2) I created a new projectile 3) I called myProj.update(.5) and nothing happened! It doesn't call the update! Now I'm really freaking out, I can't figure out why that would be! Import "Code/Includes.bmx" GameData.current = New Gamedata GameData.current.loadGameData("Data/DataManifest.txt") Graphics 1680, 1050 Pip:projectileInfo = GameData.current.projectileInfoNamed("Gatling Laser") np:projectile = projectile.create:projectile(pip, 0, 0, 0, MouseX(), MouseY(), 0) Global deltaT#, lasttime = MilliSecs() While Not(KeyHit(key_escape)) deltaT = (MilliSecs() - lasttime)*.001; lasttime = MilliSecs() If MouseHit(1) Then Pip:projectileInfo = GameData.current.projectileInfoNamed("Gatling Laser") np:projectile = projectile.create:projectile(pip, 0, 0, 0, MouseX(), MouseY(), 0) End If np.update(DeltaT) np.draw() Flip; Cls; Wend Update never gets called! argh, this is freaking me out! |
| ||
You have a list of projectiles or Gameobjects? if gameobjects you need to cast to pprojectiles I shouldn't think so, the update method in the parent type is abstracted so surely the overridden method in the type should be called without the need to cast. |
| ||
Method updateProjectiles() For p:projectile = EachIn projectiles DebugStop() If p.dead Then ListRemove(projectiles, p) Else p.update(deltaT) End If Next End Method That was the original source, thats a method in Simulation which has a list of projectiles which it runs through, I've verified that it makes it to the line right p.update(deltaT) Also, in the post above yours I have it being called not from an item in a list. |
| ||
Thats pretty odd, have you tried putting a breakpoint inside the update method to see if its actually being called but that the code inside is not producing the desired results? |
| ||
Does this work for you... ? If so, your problem is somewhere else. <edit> Arrrgh editing while I'm typing. |
| ||
tonyg - thanks, that works, the problem must be somewhere else then. Do you have any experience with what might cause a method to not call right? Helios - yes, i tried making debugstop the first line in update(DeltaT#) and it doesn't get called. |
| ||
Nope, I agree you should put a debugstop or, my preferred method, add an 'Entering xxx', 'Exiting xxx' debug statement to each of my functions and methods. |
| ||
first, my best advice... use SuperStrict! Really... You have a list of projectiles or Gameobjects? if gameobjects you need to cast to pprojectiles that shouldn't matter... SuperStrict Type GameObject Abstract Field ID:Int Field dead:Int Method update(deltaT#) Abstract Method setInput(inputData:TList) Abstract Method getOutput:TList() Abstract Method draw() Abstract End Type Type Projectile Extends GameObject Method update(deltaT:Float) Print "updating" End Method Method setInput(inputData:TList) End Method Method getOutput:TList() End Method Method draw() Print "drawing" End Method End Type Local list:TList = New TList For Local i:Int = 0 To 10 list.AddLast(New Projectile) Next For Local go:GameObject = EachIn list go.update(1.0) Next the above prints "updating" for each object. |
| ||
Ok, I think i've figured it out. Somehow the objects were getting replaced with something that inherited from Projectile :P I'm sorry to have led us on this wild goose chase - and thanks so much for all your help! |