Announcement

Collapse
No announcement yet.

Call C++ functions from JS

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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!
    Last edited by ArosPrince; 06-13-2019, 05:44 AM. Reason: Tags added

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

    Code:
    #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:

    Code:
    '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

    Comment


      #3
      I tried to edit:
      Code:
      \Engine\Source\Programs\UnrealBuildTool\Platform\HTML5\HTML5ToolChain.cs
      To also export ccall like this:
      Code:
      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?

      Comment


        #4
        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



        Comment


          #5
          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?.

          Comment


            #6
            To call a JavaScript function from C++, generate a js file, which calls the function. The web page will load the JS and the function runs:
            int callId = 0; void callFunction() { // the js file ofstream fout("generate.js"); fout << "caller(" << callId ++ << ", display);"; } // code
            HTML
            <html> <body> <script> var callId = 0; function caller(i, func) { if (callId < i ) { callId = i; func(); } } function display() { alert("my Function called"); } // load js script function loadScript(file) { var myId = "gh"; var e = document.getElementById(myId); if ( e != null ) e.parentNode.removeChild(e); var script = document.createElement("script"); script.myId = myId; script.src = file; document.body.appendChild(script); } window.setInterval(function() { //path to the js file loadScript("file:///path/generate.js"); }, 2000); </script> </body> </html>
            Custom Software Development
            http://www.ifourtechnolab.com/custom...re-development

            Comment

            Working...
            X