Monkey patching cocos2d-x classes

I want to add new functionality (methods and variables) to cocos2d-x classes e.g. Sprite or Node or Label but without changing source code of cocos and without creating a new class. Objective-c can do this through extensions. Is there a similar approach in c++? This is very handy in case you want to update cocos2d-x to new version.

What about using git patch?

What do you need that the framework can not provide you?
Maybe the cocos team reads this topic and can add new features :wink:

I have lots of handy functions, many times just shortcuts. One simple example obj->getRealWidth() which does the following: obj->getContentSize().width * obj->getScaleX().

One way to do it is to add a class that can manipulate object instances like getRealWidth(spriteObj). i was just wondering if there was a hackish c++ to achieve same effect as in obj-c. what do you mean by git patch?

I think it’s not a good example because you can use getBoundingBox() :smiley:
I mean:

obj->getContentSize().width * obj->getScaleX() should be equal to obj->getBoundingBox().size.width
and:
obj->getContentSize().height * obj->getScaleY() should be equal to obj->getBoundingBox().size.height

Anyway, I don’t know how to do what you want in c ++, but I recommend you create your own classes so that it is clear what your methods are and what are the methods of the framework. It’s just my advice.

Any way to implement this in c++ that actually modifies the class method pointers is most likely more of a hack than writing the ‘C’-like code you’ve already tested (e.g. getRealWidth(spriteObj)). You’d probably have to modify v-tables or something and whatever you end up doing I wouldn’t be surprised if it only worked on specific compile/STL/platform.

A few games I’ve worked on used a custom GameObject class derived from Sprite for most entities and I think it allows for enough customization that it might give you enough of what you want? Or at least limit the amount of engine modification you ultimately need to make.

It is almost impossible to replace Node (and Ref) without modifying the engine source since it’s embedded in most other classes, but you could also try to just replace Sprite for your game’s entities directly by copying its code into MySprite and then creating one of those everywhere you’d have used Sprite.

Objective-C monkey patching was a nice capability compared with C++/Swift.

If you end up wanting to modify engine source, I’d recommend creating a git fork of the repository so you can pull in updates or even cherry pick specific commits. You could use git patch with a non-forked repo, but that’s probably more work in the long run. Kinda depends how large (and long, in dev time) your project is going to be.

Short answer: The concept of extensions isn’t available in C++.

It’s the same issue with Kotlin and Java. In Kotlin you can write extensions, but if you want to use it in Java it ends up as new classes with static Methode. So a String.add(text: String) in Kotlin will be in Java as StringKt.add(obj: String, text: String).

Because C++ didn’t know about extensions, you will need to create a subclass or a helper class with static methode as well.

@mars3142 did I somehow miss what the OP wants to accomplish here. When I read the topic I thought they wanted to make changes to the engine that persist between versions of the engine.

Correct @slackmoehrle. I want basically to know all the options on the table with their prons and cons.

I do understand its possible to subclass a cocos class and put your functions there. But then you need to change all Sprites -> MySprite in the whole project and every time you want to transfer this functionality to an new project, you need to repeate this for the project.

If extensions as in objectice-c were possible, then you provide some header files to any project and baaam all functionality ready without chagnign any existing line of code.

You don’t need to repeat that process for every new project. If you structure your sub-classes so that they exist in their own section/module, then you can just move that across to every project you create. Don’t put game-specific logic in the sub-classes of Cocos2d-x classes.

Others on here have posted up similar things as what you’re trying to achieve. An example is this:

Didn’t know about this library. Yes seems similar to what i try to achieve. I will have a look at it! Thanks R101 ))