Archive 17/01/2023.

Call C++ function from javascript (emscripten)

Petryk

Hi,

I’m trying to call c++ function definied in my Application class from javascript, but with no success.

My application class definition is:

SharedPtr helloText;
public: SampleApplication(Context * context) : Application(context) {}
void CreateText()
	{
		helloText=new Text(context_);
		helloText->SetText("Hello!");
		helloText->SetFont(cache->GetResource("Fonts/Anonymous Pro.ttf"), 30);
		helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
		helloText->SetHorizontalAlignment(HA_CENTER);
		helloText->SetVerticalAlignment(VA_CENTER);
		GetSubsystem()->GetRoot()->AddChild(helloText);
	}
public: void ChangeTextFromJs()
{
	helloText->SetText("Hello from JS!");
}

I want to call public function ChangeTextFromJs in my javascript code (after click some html button).


      var Module = {
        preRun: [],
        postRun: [],
        print: (function() {
.....
kostik1337

Probably, what you need is embind

Petryk

@kostik1337 thank You for reply! Could you provide some example code? I don’t know how to handle current instance of my SampleApplication…

kostik1337

Well, I didn’t have any experience with Urho3D+emscripten. I can suggest you to create 2 binds, bind for a static C function that returns pointer to SampleApplication, and a bind for ChangeTextFromJs. Most probably, you’ll need to rewrite URHO3D_DEFINE_APPLICATION_MAIN macro, to store pointer to application into some static variable in global namespace