Bind a New Module in Engine

creator 3.8.0
I am trying this manual and get some problems.
https://docs.cocos.com/creator/manual/en/advanced-topics/jsb/swig/tutorial/

I just want to add one c++ static function that will be called from ts.

I guess some paths are incorrect and I can’t see some folders or files.

  • native/tools/swig-config isn’t in my creator directory.
    It seems to be C:\ProgramData\cocos\editors\3.8.0\resources\resources\3d\engine-native\tools\swig-config.

  • I copied swig-interface-template.i and named it as new-engine-module.i.
    I don’t understand how to write new-engine-module.i. Please give me an example or guide me.

  • I can’t see engine/native/cocos/CMakeLists.txt.

  • Where’s cocos/bindings/auto ?
    C:\ProgramData\cocos\editors\3.8.0\resources\resources\3d\engine\native\cocos\bindings has manual folder but not auto folder.

  • And then how to generate binding?
    run ‘make’ in terminal in path of CMakeLists.txt ?

Please guide me.

Hey @zzf_Cocos , @pandamicro
Could you help me please?

@dumganhar could you please take a look?

All paths in the documentation is relative paths to engine root. It’s C:\ProgramData\cocos\editors\3.8.0\resources\resources\3d\engine in your editor directory not engine-native.

And if you don’t want to modify engine code or don’t want to add a new module in engine, I supposed you want to create binding code for your own classes, you probably need to check out this section : Cocos Creator 3.8 Manual - Swig Tutorial .

From 3.8.0, the binding glue code for engine (cocos/bindings/auto) will be generated by cmake automatically, its location will be in native project’s directory, e.g. your_project/build/<target_platform>/proj/generated/cocos/bindings/auto.

The binding glue code for user’s code could be customized.

Hi, @dumganhar. Thank you for your reply.
I followed it, first of all, I want to add new module in NewProject (creator 3.8).

In cmd, it shows like this;

What is namespace or class name that can be called in ts?
Are these files correct ?

I attach my files.
my-module.i: C:\Users\GameKing\Documents\NewProject\tools\swig-config\my-module.i
swig-config.js: C:\Users\GameKing\Documents\NewProject\tools\swig-config\swig-config.js
jsb_my_module_auto.h: C:\Users\GameKing\Documents\NewProject\native\engine\common\bindings\auto\jsb_my_module_auto.h
jsb_my_module_auto.cpp: C:\Users\GameKing\Documents\NewProject\native\engine\common\bindings\auto\jsb_my_module_auto.cpp

swig-config.zip (1.6 KB)
jsb_my_module_auto.zip (2.5 KB)

It seems that you didn’t %include any c++ header in your .i file. So swig will bind nothing.
You set the module name by %module(target_namespace="my_module_name_in_js") my_module
So all c++ classes exported to JS will be under my_module_name_in_js namesapce.

For instance, you bind MyAClass, in JS, you have to use it like

const a = new my_module_name_in_js.MyAClass();

@dumganhar
To add MyAClass, which files should I add?
Could you share the file paths and codes of it?
Thanks.

:sweat_smile:

I just take an example.

Plz read the whole documentation.

Sorry but I really don’t understand the document.
Could you share project that has manual binding or automatic binding?
engine/native/cocos doesn’t include CMakeLists.txt.

NewProject doesn’t include native folder in it, is it okay?

I uploaded the demo here : GitHub - dumganhar/cocos-swig-binding-demo

You need to build a native project, compile and run it.
If it succeeds, you would get the following log:

15:55:56 [DEBUG]: ==> a: 100, b: 1

15:55:56 [DEBUG]: JS: ==> myObj.publicFloatProperty: 1.2300000190734863
15:55:56 [DEBUG]: JS: ==> old: myObj.coolProperty: 1234
15:55:56 [DEBUG]: JS: ==> new: myObj.coolProperty: 666
15:55:56 [DEBUG]: ==> hello MyObject::methodToBeRenamed
15:55:56 [DEBUG]: JS: ==> old: myObj.type: 333
15:55:56 [DEBUG]: ==> setType: v: 888
15:55:56 [DEBUG]: JS: ==> new: myObj.type: 888
15:55:56 [DEBUG]: JS: ==> featureObj: undefined
15:55:56 [DEBUG]: ==> helloWithAnotherObject, a: 135.246002, b: 999

Thank you very much for your sample, @dumganhar

In MyObject.h, I added one function to convert Uint8Array to Image. (Uint8Array is not RGBA value but raw binary of jpg file)
static bool makeSpriteFrame(se::State& s)

I upload my project here.

I’ve success to genbinding of MyObject.h

When open ccl4.sln in build/windows, jsb_my_module_auto.cpp has error. this cpp file is automatically generated when run genbinding

Could you check it please?

Everyone, could you have a look on this issue please?

I couldn’t download the attachment you uploaded. Could you upload to google drive or github?

From the snapshot, I think you probably didn’t bind the my_ns::ImageInfo class, so there is not an overrode method sevalue_to_native for ImageInfo type.

You need to %include the header file which contains your ImageInfo class.

Hi, I fixed it and now it has no compile error and It works similar but callback doesn’t get Image param, instead get object.
Could you connect to my pc by Anydesk?

Your createImageInfo method is returning ImageInfo object, not Image object.

I added this function to jsb_global.h and jsb_global.cpp.
bool js_loadImageBinary(se::State &s);

And MyObject::makeSpriteFrame calls js_loadImageBinary();
image

Current source is very different with before you saw.
When you connect Anydesk, you will understand soon.
Here’s my anydesk.
1 636 339 914

Sorry, I don’t have anydesk installed on my mac.
It’s better to discuss issues in forum.