I am able to call JavaScript functions from an UE4 game. . .but I’d like to go the other way. Basically, I want to see if I can flush out a user interface using HTML/CSS. For instance, I’d like to have a button event that starts the game, pauses the game, etc.
How do I go about getting the GameMode? Call a method within the class?
I’ve tried Embind (Embind — Emscripten 3.1.39-git (dev) documentation) but without success at the moment.
Any help is appreciated.
Thank you.
Based on this: [HTML5] Call C++ UFUNCTION from Webpage via JavaScript - Platform & Builds - Epic Developer Community Forums
and this:124477-
I ended up performing the following:
FirstPersonGameMode.cpp
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
#ifdef EMSCRIPTEN
extern "C" {
AFirstPersonGameMode* EMSCRIPTEN_KEEPALIVE EmscriptenGetGameMode()
{
if (GEngine)
{
TArray<APlayerController*> Players;
GEngine->GetAllLocalPlayerControllers(Players);
if (Players.Num() > 0)
{
const UWorld* World = Players[0]->GetWorld();
if (World)
{
return Cast<AFirstPersonGameMode>(World->GetAuthGameMode());
}
}
}
return NULL;
}
void EMSCRIPTEN_KEEPALIVE JSStartGame()
{
AFirstPersonGameMode* gameMode = EmscriptenGetGameMode();
if (gameMode)
{
gameMode->JSStartPlay();
}
}
}
#endif
Then in my JavaScript function:
<script>
function startGame() {
Module.ccall('JSStartGame', ], ]);
}
function setScore(score) {
document.getElementById("score").value = score;
}
</script>
<body>
<div>
<button id="" type="button" onclick="startGame()">Start Game</button>
<p>Score: <input type="text" id="score" /></p>
</div>
</body>
When the score changes I call the JavaScript function from the UE4 FirstPersonPlayerState.cpp:
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
void AFirstPersonPlayerState::AddScore(float score)
{
//GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, "AddScore called");
Score += score;
#ifdef EMSCRIPTEN
EM_ASM_ARGS({ setScore($0); }, Score);
#endif
}