UnrealEngineHTML5 / Documentation

540 stars 183 forks source link

[Question] How to send a .js integer to actor class in cpp? #49

Open cdenny17 opened 4 years ago

cdenny17 commented 4 years ago

I have looked in HTML5JavaScriptFx.js which appears to send data from c++ to js but I am trying to figure out how to send data from js to an actor class. I tried older solutions like #if PLATFORM_HTML5_BROWSER and #if PLATFORM_HTML5 but they are no longer defined. I also tried different variations of wrapping portions of the .h and .cpp files in extern "C" similar to the HTML5JavaScriptFx.h file but then compiling would fail. I was able to compile and package with this

void AChangeMaterial::ChangeFromJs(float amount)
{
#ifdef __EMSCRIPTEN__
    EMSCRIPTEN_KEEPALIVE
#endif
    float blend = 0.5f + amount;
    DynamicMaterial->SetScalarParameterValue(TEXT("Blend"), blend);
}

And then I was able to find ChangeFromJs in the HTML5 js code but there was no way to call it because ccall and cwrap caused an error of "'ccall' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)"

What am I doing wrong here? I apologize if this is a simple question, this is my first major dive into C++

nickshinpho commented 4 years ago

@cdenny17 -- that is correct, PLATFORM_HTML5_BROWSER and PLATFORM_HTML5 are no longer used when the HTML5 was decommissioned and put on the "platform extensions" feature. (i.e. .../Engine/Platforms/...)

have you looked at #6 to send data from .js to c++?

imgntn commented 1 year ago

is there an alternative to PLATFORM_HTML5 to detecting that the HTML5 platform is running now? or can you do like !PLATFORM_DESKTOP or invert it?

zlatnaspirala commented 1 year ago

HTML5 Just like you say GetPlatformName -> HTML5 . This is sure for BP for c++ must be same...

imgntn commented 1 year ago

@zlatnaspirala thanks! here's what i learned.

UGameplayStatics::GetPlatformName works at runtime and will tell you that you're running "HTML5" as the platform.

the PLATFORM_HTML5 directive is for the preprocessor and used to protect C code from the C++ compiler. Code in PLATFORM_HTML5 block is only added when compiled for that environment.

So you can't directly replace PLATFORM_HTML5 (compile time) with GetPlatformName (runtime).

What worked for us as a proxy to PLATFORM_HTML5 was to check !PLATFORM_DESKTOP - when we compile for HTML5, we're not on desktop, so that works out! Bit of a hack but did the trick!