how can i output a twine variable like $money from a JavaScript function

KingOfBuns

Newbie
Game Developer
Feb 2, 2020
54
107
JavaScript:
function endGame() {
    clearInterval(countdown);
    clearInterval(moleInterval);
    gameOver = true;
    alert(`Game Ended!\nYour final score: ${score}`);
    if (typeof State !== 'undefined') {
        State.variables.money = (State.variables.money || 0) + score;
        console.log(`Updated money: ${State.variables.money}`); // For debugging
    }
    timer = 60;
    scoreDisplay.text(`Score: ${score}`);
    timerDisplay.text(`Time: ${timer}s`);
    startButton.prop('disabled', true);  // Disable the start button
    endButton.prop('disabled', true);    // Disable the end button
}
 

guest1492

Member
Apr 28, 2018
324
272
Depends on where you want to output it.

For example, you used alert to display a message. You could also use alert to display the value of the variable.

You also changed the contents of scoreDisplay and timerDisplay (presumably jQuery objects defined elsewhere). You can do the same for some specific output element.