Using object extensions and default functions
BlitzMax Forums/BlitzMax Beginners Area/Using object extensions and default functions
| ||
Ok basically I'm trying to learn how inhearatince works in Bmax. I have an object that is the base for all my enemy types. This enemy object has some basic code in it's methods that all the extension objects use. I would like to do something like the following. type enemy method update() (command set) end method endtype this is what I would like to do with an extension object type red_enemy extends enemy method update() (parent command set) individual commands endmethod endtype I would like the extension objects to have the parent command data PLUS some small original code added inside those. |
| ||
So were is the problem. Or even a question What you are doing is ok, but I would call the parent command set from the child command set. Super.update |
| ||
?? If you want to call the parents methods, use super.someMethod (or super.Update() in that case) all fields / globals / const are automatically present in the extended class as well. |
| ||
Well if I put code in the extension object's update command, it doesn't get executed.Type enemy Field x#,y#,HP#,speed#,direction#,sprite:TImage Global enemy_list:TList = New TList Function Create(x#,y#,speed#,direction#) Local temp:enemy = New enemy temp.x=x temp.y=y temp.sprite=enemy_1 temp.speed = speed temp.direction = direction enemy_shot.Create(x,y,5,90) ListAddLast(enemy_list,temp) End Function Method Update() x:+Cos(direction)*speed y:+Sin(direction)*speed ' check to see if a shot has hit If CollideImage(red_shot,x,y,0,1,0) score:+150 explosion.Create(x,y) Destroy() EndIf ' destroy if too tall If x<0 or y<0 or x>320 or y> 240 Destroy() EndIf draw_locked_rotated(sprite,x,y,direction,4) End Method Method Destroy() ListRemove(enemy_list,Self) End Method EndType Type small_enemy3 Extends enemy Function Create(x#,y#,speed#,direction#) Local temp:enemy = New enemy temp.x=x temp.y=y temp.sprite=enemy_3 temp.speed = speed temp.direction = direction enemy_shot.Create(x,y,5,90) ListAddLast(enemy_list,temp) End Function Method Update() direction:+7 End Method EndType |
| ||
1) you havent shown us where you are trying to execute it from. However as a guess a would say that you are using the list to loop through all the enemies and calling the create from there. Im afraid that if that is what you are doing then your problem is that the list doesnt know which ones are enemies and which ones are small enemies. You need to cast the enemy back to a small enemy. But to know when to do this you need a field in Enemy that is allocated a value to indercate what type each enemy really is. 2) In the create function you are creating an enemy and not a small enemy |
| ||
Type small_enemy3 Extends enemy Function Create(x#,y#,speed#,direction#) Local temp:small_enemy3 = New small_enemy3 temp.x=x temp.y=y temp.sprite=enemy_3 temp.speed = speed temp.direction = direction enemy_shot.Create(x,y,5,90) ListAddLast(enemy_list,temp) End Function Method Update() direction:+7 End Method EndType try this |
| ||
hello Ryan. H&K and Dreamora talk the correct call to update. In small_enemy3 you put in update: Method Update() super.update() direction:+7 endmethod And you call update from instance of small_enemy3 Bye Paposo |
| ||
Thanks I've been away from the game a bit but will try this and get back to you. Edit: That works great! What is "super"? |
| ||
Edit: That works great! What is "super"? It's an object cast to the type of it's immediate parent type. IE: If TypeB Extends TypeA then any TypeB can call a TypeA method by casting it to a TypeA. Super does that, only without you necessarily having to know that it's a TypeA in order to make the cast. Does that make sense? |
| ||
Yes, I think that is what I was looking for all along thanks. |