I am making a game for HTML5. At a certain moment I call the ad block, before that I put the game on pause. When the player closes the ad block, I need to unpause the game (I use Blueprints). I need to call UFUNCTION (BlueprintImplemetableEvent) from JS, but I didn’t find how.
I use this code in cpp:
#ifdef __EMSCRIPTEN__
#include "SDL/SDL.h"
#include "emscripten/emscripten.h"
#include "emscripten/html5.h"
#else
#define EMSCRIPTEN_KEEPALIVE
#endif
#include "string"
#include "iostream"
#include "sstream"
//'"FString Text" I assign to test in Blueprints
void AChristmasMole4_23CPPGameModeBase::Test()
{
std::string OutboundSendString(TCHAR_TO_UTF8(*Text));
#ifdef __EMSCRIPTEN__
EM_ASM_ARGS({
var theText = UTF8ToString($0);
console.log('Hello' + theText);
}
, OutboundSendString.c_str()
);
emscripten_run_script("testJS();");
#endif
}
extern "C" void EMSCRIPTEN_KEEPALIVE CallTest(char *str)
{
//I don't know how to call UFUNCTION(BlueprintImplementableEvent) here
//I used (char *str) because I don't know how to use Module.ccall to call a function with no arguments
}
In [ProjectName] -Shipping.UE4.js file:
function testJS()
{
var str = "Hi from JS!";
var lenUTF8 = Module.lengthBytesUTF8(str) + 1;
var ptr = Module._malloc(lenUTF8);
Module.stringToUTF8(str, ptr, lenUTF8);
Module.ccall('CallTest', 'null', ['string'], [str]);
Module._free(ptr);
}
When I call the testJS() function, it shows that Module.lengthBytesUTF8 is not a function and Module.ccall too.
I don’t really like working with global variables, I would like to have a callback in JavaScript that would trigger events in the drawings, I do it like this
To work Module.ccall you need to export ccall function (add it to exported methods array) in Engine\Platforms\HTML5\Source\Programs\UnrealBuildTool\HTML5ToolChain.cs (~473 line):
Result += " -s EXPORTED_RUNTIME_METHODS=\"['stringToAscii', 'ccall']\"";