Is there a way to access a function that is inside an async function from another script?

180 views Asked by At

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]
})();
1

There are 1 answers

0
secan On BEST ANSWER
(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 */)
})