Inheritance
BlitzMax Forums/BlitzMax Beginners Area/Inheritance
| ||
I have some characters that move about, and I have a particle effect working fine in isolation. Now I want the particles to move with the correct characters, and when the character is deleted so are the particles. I came up with the idea of using an IdNumber to associate the character with the particle set which should work fine. But could Inheritance be used in some way to automate this process? Some simple code would be appreciated as I'm still getting used to the OOP way of things. |
| ||
No, inheritance is quite pointless here unless you extend the emitter from a general pivot class or similar. Inheritance extends a given base object, thats why it isn't of much use here. |
| ||
Allocate your particles to an 'emitter' and then assign the emitter to a field in the character type definition. |
| ||
what Dreamora and Tonyg said. to elaborate a little. I do not think inheritence is the right thig to do. You might want to try a concept called 'Composition'. instead of the DERIVED type extending a BASE type, you would build your Character type from other, seperate types (i.e Compose). remember that adding Field emitter:Temitter does not instantiate an object of Type Temitter. you still have to do that with NEW. Type Character Field emitter:Temitter Method update handleemitter() ' call the emitter handler draw() ' call the draw method and draw the character End Method Method draw ' draw character End Method Method handleEmitter() pseudocode If its time To emit particles Then emitter.emit() EndIf End Method End Type Type Temitter Field x,y Method emit() ' emit particles End Method End Type |
| ||
Thanks for the suggestions and code. |
| ||
The best way to determine when to use inheritance (well maybe not best, but easiest) is to, assuming you have two objects A and B, ask yourself "Is A a type of B, or does A have a B." So if you had two objects, TChicken and TWing, it's pretty obvious that the TChicken isn't a type of wing but instead has a wing. So "has a" relationships are best modeled through composition. "Is a" relationships are best modeled through inheritance. Given objects TChicken and TBird we can conclude that inheritance is the best modeling approach since a chicken is a type of bird. |