a continuation of this abandoned thread:
Generally Accepted Approach to Creating Game Objects - #3 by bacon
- entity extends Object (inheritance)
- entity has an Object (as in: var sprite:Object; composition)
- … other ways??? factory? i dunno
is there a preference? especially taking into consideration how Heaps whole scene tree works…?
when i first started, as in the first few days, i just used the inheritance way. It was simple; it got me started quick: just override update(), add it to the scene, and you’re good to go. Then, when i sat down and made a tiny system, i used the second approach. But… now, i’m really not certain which is the way to go anymore.
With the composition way, entity can have multiple Objects (and things that extend it). Let’s say an Anim animation and some Graphics effects. But then, it kinda feels like it’s detached from the scene tree… Like you’d have to handle how all of those Objects are added to/removed from the scene tree / Layers, moved around in the scene tree, etc. I dunno, it’s a fuzzy feeling i’m trying to describe here…
Whereas with inheritance, the entity is an Object itself. And so, if you wanted multiple Objects, you can still add them to the entity, but this way, you’d always have a parent Object (super/this; inheritance parent), as, well, a parent Object (in the scene tree)… It’s more of a 1:1 association to the scene tree… and that somehow makes more sense to me.
huh, well, that’s all my brain has to say about that, for the moment…
i personally like composition, just to keep the core class clean ‘n abstracted as much as possible, with very few methods to distract me… But that’s not really a good reason, is it? lol.
I don’t even know if it’s worth abstracting Object! As of now, I just keep the Object as a public var, and manipulate it directly, as in myEntity.itsDrawableSprite.x = 123. I mean, with multiple Objects, abstraction would be a pain, no? Should entity.x = 123 change all of it’s objects?.. Or do you keep one Object as a sorta parent Object (scene tree parent, not inheritance parent), for transform purposes only, and make the rest [of the Objects] children?
God halp me. lol.
inheritance actually makes sense. You’d probably want a base Object, so that when you do manipulate it’s transform, entity.move(10, 0), all of it’s children objects are manipulated too… Otherwise, with composition, you’d have to do something like entity.mainObject.move( (unless you wrote wrappers, which i kinda attest).
for the record, ld40 (Jonathan the Sorcerer) uses composition, deepnight’s gamebase uses inheritance (iirc).