• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How best to imitate the decorator pattern? (attaching children)

D

dallinbeutler

Guest
So here's the dilemma. I have a obj_collision which handles colliding with solid objects. It's children are things like projectiles, enemies, and the player. Only some of these objects need health, defense, etc. I don't want to make a parent with these attributes, because some projectiles need health, some enemies don't need it, etc. .
In traditional programming this is solved by using the decorator pattern. You attach objects to your parent object, and for damaging something, you check to see if it has this attachment. If it does, then do the thing.
This can all be done, but the problem comes with efficiency, and instance activating/ deactivating. Each object comes with a ton of variables, like sprite, x,y, health, image_index, sprite_height,sprite_width, etc. Are all of these variable really getting stored with every object, even if I never set or use them? This could be vastly inefficient if every object has 5 of these attached objects all with the values. And activating/deactivating instances doesn't throw an event to the affected object, so how can I ensure its attached instances are activated/deactivated accordingly, without writing a wrapper to cover every instance related function?
 

TheouAegis

Member
Objects take up almost nothing, instances take up all of those variables. You can have grandparents, so you could assign bullet types one parent combative types another parent and then assign your primary object as the parent of both objects.
 
Top