Call C++ functions from JS

Hi,

I need to be able to call C++ (or blueprint) functions from JavaScript. I know that Unreal uses Emscripten to pack the C++ code to HTML5 build. It should be integrated in Unreal, but it is still not clear to me how to actually use it. I am able to build my Blueprint / C++ app in HTML5 and run it, but I just don’t understand how to call the C++ functions. According to this thread, it should be possible:

https://answers.unrealengine.com/que…avascript.html

But what I don’t get is:

  • PLATFORM_HTML5_BROWSER is not defined at all. No matter if I run from editor or package the APP as HTML5, it is not defined. Am I supposed to do it myself? Because I would expect it to behave similarly as for example PLATFORM_WINDOWS…
  • those EMSCRIPTEN_KEEPALIVE and other EMSCRIPTEN macros are not defined. Am I suppose to include anything? I tried to include <emscripten.h>, but that fails right away when trying to build the editor so I cannot even get to packaging the app for HTML5.

The best thing would be if there was some comprehensive tutorial for JS / C++ integration, but that doesn’t seem to be the case. Or is it?

Thanks!

1 Like

OK, at this point, I was able to solve it by putting this in the cpp file:


#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#define PLATFORM_HTML5_BROWSER 1
#endif

I don’t know if this is the way to do it, but at this point I am at least able to package the HTML5 build.

The next question however would be… I get this in the console when I run the app:


'ccall' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)

Well, if I read the FAQ correctly it seems that it no longer exports all of the function in order to save some space. But I just can’t see where would I add it in Unreal. I guess that would be some parameter to the emscripten execution… But where do I set it?

Thanks

1 Like

I tried to edit:


\Engine\Source\Programs\UnrealBuildTool\Platform\HTML5\HTML5ToolChain.cs

To also export ccall like this:


Result += " -s EXTRA_EXPORTED_RUNTIME_METHODS=\"'Pointer_stringify', 'writeAsciiToMemory', 'stackTrace', 'ccall']\"";

But it doesn’t seem to impact anything. I am not sure it even takes the file into account just like that. Even if I make an intentional syntax error in the file, it still goes through… Do I need to recompile it somehow?

1 Like

To conclude this… This whole idea was based on an old UE integration. Nowadays, you don’t need to mess with any of the stuff above.

Files to look at:

  • Engine/Source/Runtime/HTML5/HTML5JS/Private/HTML5JavaScriptFx.js
  • Engine/Source/Runtime/HTML5/HTML5JS/Public/HTML5JavaScriptFx.h

See e.g. UE_EngineRegisterCanvasResizeListener()

As for the HTML5 macro, nowadays, it’s just: PLATFORM_HTML5

1 Like

So i wasted 2-3 days in about the same process as you have Aros_Prince. The problem is i don’t understand your last comment. Care to make a short step by step tutorial for the rest of us to understand how the new UE integration (which i can’t find any information on anywhere btw) works?.