1

In spark AR I've got multiple scripts: a UIManager script and a GameManager script. I've got a game over function inside of an async function in my UI manager, but I want to access the function via GameManager.

I've tried putting export before the game over function but that isn't allowed and I cant find anything online. Does someone know of a way to do this?

Code:

(async function() 
{

//Function that gets called when the game is lost
export function GameOver(score = 0)
{
    //Make the Game over screen visible
    LoseScreen.hidden = false;

    //Check if the gme has a shop
    if(GameSettings.RequireShop)
    {
        //Make the Shop button visible
        ShopButton.hidden = false;
    }
    else if(!GameSettings.RequireShop)
    {
        //Make the Shop button invisible
        ShopButton.hidden = true;
    }
    
    //Set the value to the current screen
    CurrentScreen = "LoseScreen";

    //Make the game invisible
    //NOTE: end the game here when making the final game
    GameMenuObj.hidden = true;

    //Score variable saved here and displayed on screen
    Score = score;
    TotalScore += Score;

    LoseScore.text = Score.toString();

    //Check if the game has a Leaderboard or shop
    if(GameSettings.RequireleaderBoard || GameSettings.RequireShop)
    {
        //save your new score and money
        SaveData();
    }
    
    //show / refresh money on screen
    CoinText.text = TotalScore + "";
}

//Enables async/await in JS [part 2]
})();
  • Please do not post code as pictures. – meskobalazs Sep 29 '20 at 09:05
  • _If_ I understood correctly your request and _if_ I made a fair assumption on the code, you should simply move the function declaration outside the async function, so that you can export it, and then call it inside your async function passing any required parameter. – secan Sep 29 '20 at 09:09
  • secan - thanks for the help this solved the problem after moving a few other variables and functions to. – Martijn Gelton Sep 29 '20 at 09:16

1 Answers1

1
(async function() {
  // ...
  function GameOver(score = 0) {
    // ...
  }
})

becomes

/* ... all the variables and functions declared inside the async function
 * to whom the GameOver needs access should now be passed as arguments
 */
export function GameOver(
  /* ... arguments from the async function */,
  score = 0
) {
  // ...
}

(async function() {
  // ...
  GameOver(/* ... arguments */)
})
secan
  • 2,622
  • 1
  • 7
  • 24