Announcement

Collapse
No announcement yet.

Get GameMode, Call Methods from JavaScript

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

  • replied
    Based on this: https://answers.unrealengine.com/que...avascript.html
    and this: https://answers.unrealengine.com/sto...ing+to+c++.txt

    I ended up performing the following:

    FirstPersonGameMode.cpp
    Code:
    #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:

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

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

    Leave a comment:


  • started a topic Get GameMode, Call Methods from JavaScript

    Get GameMode, Call Methods from JavaScript

    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 (https://kripken.github.io/emscripten...pt/embind.html) but without success at the moment.

    Any help is appreciated.

    Thank you.

Working...
X