How Does Composition Over Inheritance Work in Game Development?

0
7
Asked By CuriousCoder42 On

I've got a question about using "Composition over Inheritance" in game development. Let's say we have an Entity that handles both Collision and Render processes. How do we achieve this without needing a unique implementation for each process? For example, an octopus and a spaceship would likely have very different implementations for the same processes. Doesn't that defeat the whole point of modularity and making composition easier?

1 Answer

Answered By GameDevGuru99 On

Great question! The usual guideline is that composition is for "it has a..." while inheritance is for "it is a..." In your case, Collision and Render are behaviors—both octopus and spaceship are entities that can collide and render, so you want to treat them as such in higher-level code. Inheritance works here because it allows code reuse. You also might find opportunities to use composition if certain entities share similar collision behaviors.

TechieTom -

Interesting point! How could we blend these concepts in code? Like, if we just have an Entity class, would we use composition for easier behaviors like rendering and then have an intermediary superclass for specifics like octopus movement?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.