Cocos2d-x general question about class design and Inheritance

I did some research but didn’t quite understand the subject of class design within Cocos2d (v3.8)

From what I understood Cocos encourages Inheritance, by giving you virtual methods that you should override in case you want X to do Y .but reading general game class design books and papers Inheritance is a big NO NO and you should prefer composition a.k.a Component design pattern. On the other side digging into the code of Cocos I saw that it is using components itself below the hood.

So my question is: are there any features that forces me to use Inheritance to get the job done? or to rephrase it, can anything that can be achieved with Inheritance can also be achieved with composition in Cocos ?
Or should I Inherit from the Sprite/Layer/Scenes… etc just once (1 level Inheritance) and then from that point only use composition/component ?

I’m just starting up with both C++ and Cocos and I’m building simple games now but I’d like to know the general notion and axioms about this subject.

1 Like

You aren’t actually forced. I wrote something for another here about sort of 2 options: Layers and Scenes logic

Thanks, do you have any good example of fully component based game design in cocos2d ?

I don’t have a full example. I just know what I do.

Thank you, I’d like to hear another opinions on my question!

You can do whatever you want. It is true that Cocos pushes a bit to the inheritance approach… which is not inherintly bad as you may think. For small games almost any approach is good.

If you are starting both C++ and Cocos I advice to you not worrying a lot in architectural side of things and do not overdesign your games… just try to get something working. It is a very common mistake in newcomers to try to make the perfect design and this complicates the game development flow, due to lack of experience and lost of big picture of things.

So do not be discouraged by the fact of cocos being a more “inheritance-centric” engine. You can make great games with this engine. Just try to get small games working and you will learn a lot in the process.

You would still have to implement the virtual functions of the layer.

There is no way to get around inheritance and implementing the virtual functions, because it’s just how the engine was designed. You would need to re-write huge parts of the engine, if you want to go with a component approach.
Even component based engines have some sort of inheritance. E.g. the systems are derived from a base system.
Nevertheless there are approaches without any inheritance at all.

I agree with @Dredok. First get experienced in making an actual game based on that engine not an engine itself, or worrying about designs of the used engine. Your first game won’t get any benefits from digging into engine design, as it won’t be the next AAA blockbuster(I guess ;-)), in need to squeeze out everything from your hardware or worrying about overheads of virtual function calls or the like.

Just don’t think around code. Think around data! In the end everything is just data, even the code.
E.g. if you want to have an asset, which also has the functionality of a sprite, but offers other features. You could implement it in two different ways:
Deriving from Sprite and implement the features: myMegaSprite->applyMyMegaFeature();.
Just use the Sprite and transform it’s data: applyMyMegaFeature(myBasicSprite);,

The former uses inheritance, the latter composition/data transformation.

You can only apply this to your game code, but not the engine code, as you would have to change the engine design after all.

1 Like

@Dredok, @iQD Thank you so much for the detailed and honest answers. I surely tend to do overthinking a lot, It’s a malicious habit I should get rid of .I decided to make my levels as inherited layers and see how it goes from there. Thank you!

Your’e welcome!

Just to let you know. You don’t have to derive your levels from the Layer class. This was just owned to legacy code. You just need a Scene and Nodes. With the new implementations, Nodes can have touch events and can also be used for layering(the new “Layer” method). Old legacy code needed a Layer for touch information.
You just have to derive from Layer, if you want the functionalities from the Layer class, which are different to that of the Node class.

With that answer you exactly nailed down my source of confusion! I totally looked at legacy code examples! Kudos to you for saving me hours of despair. Thanks again! I appreciate your help!