1

I'm having trouble modifying a variable within a text adventure game I'm writing code for in JavaScript. I want to change the value of a global variable using a function within a function (no, not closure).

/*This is just example code.*/
    var health = 100;
    var exp = 0;

    function refreshStats() {
      health -= 10;
      exp += 1;
    }

    function foo(flag) {
      if (flag == DONOTHING) {
        refreshStats();
      }

      if (health <= 0) {
        say("You died, bwahaha.");
      }

      if ((exp/10) == Math.floor(exp/10)) {
        health += 10;
        say("You leveled up!");
      }
    }

How the game works is that each function (defined as actions or areas within the game) will be called by buttons and forms placed by JavaScript that the user can click or write in, respectively. I need refreshStats() to update the health and exp variables so foo() can use them correctly; the function doesn't seem to be updating the variables until after foo() runs. I do wonder if it's a browser compatibility issue, which really would tick me off, but I'm hoping it isn't that.

Any suggestions?

3
  • Best guess. Your "global" variable isn't actually defined in global scope so different pieces of code are modifying different scoped copies of the variable. But, can't know this without seeing the real code and the context around it. Commented Sep 3, 2011 at 5:49
  • Is DONOTHING a variable, or is that supposed to be the string "DONOTHING"? Commented Sep 3, 2011 at 6:10
  • DONOTHING is a flag-like variable, but I didn't define it in the example code. I know it wouldn't work under these circumstances, but I'm more concerned about the global variables, not the flags (the flags in the actual code are properly defined). Commented Sep 3, 2011 at 18:35

2 Answers 2

2

Store your values into your window object, so it will be available in any scope of that window:

//take care to not overwrite native properties of the window
window.health = 100;
window.exp = 0;

function refreshStats() {
  health -= 10;
  exp += 1;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like it's working for me. Perhaps it's a scoping issue - instead of example code, could you post your actual relevant code?

3 Comments

More of a comment, even if relevant.
Your code is missing the if-else statements that grab the value of the variables to decide if you died or not. Functions like alert() and methods like log() will display the variable as it should, but not if-else statements.
@zero That doesn't make any sense.. the value of a variable at any point in time is the same whether or not it is alert()ed or evaluated in a conditional. Post some real code, because something else is happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.