Login
Remember
IF Answers
Ask a Question

# calculating the value of a variable

+1 vote
57 views
asked Jul 3 in Authoring by G.D. Lascelle

This feels like an embarrassingly basic question, but I can't find a clear answer (at least that I understand) in the I7 documentation.

I want to calculate the value of a variable from two other variables - e.g. Z = X - Y.

The first part seemed easy enough. I used:

    X is a number that varies. X is initially -30.

    Y is a number that varies. Y is initially 10.

    Z is a number that varies.

Then I tried what to my untutored eye seemed the most intuitive way of assigning a value to Z:

    Z is X minus Y.

But inform wouldn't compile. So after looking over a couple of examples in the Inform 7 Handbook I tried:

    To decide which number is Z:
        let Z be X minus Y;
        decide on Z.

Which compiled fine, but later on when I try to use Z in a description - something like:

    "On the video-screen it says: the value of Z is [Z]."

Instead of returning what I expected, which is -40, it returns 0.

Obviously I'm doing something very stupid, so would appreciate any help.

Thanks in advance

- inform7
- programming

## 2 Answers

+1 vote
answered Jul 3 by Juhana (246 points)
Best answer

I believe the main problem is that you still have the `Z is a number that varies` line left from the first attempt.

When you have both a variable and a "to decide" phrase with the same name, the variable "wins" and the phrase is effectively ignored. That's why it prints 0 because that's the default value of the variable. So removing that line should help.

After that you'll have to change the phrase slightly: you can't use a local variable that has the same name as the phrase.

    To decide which number is Z:
        let result be X minus Y;
        decide on result.

Or, since the local variable is redundant in this simple case,

    To decide which number is Z:
        decide on X minus Y.

commented Jul 3 by gdLascelle (1 point)
Ah! Of course that makes total sense. Thanks very much.
+1 vote
answered Jul 5 by HerrRau (26 points)

The intuitive way you tried first actually works, with two twists:

(1) Setting the value of a variable requires the use of "now":

    Now Z is X minus Y.

(2) And the line has to appear somewhere within a rule or definition, it can't just stand on its own. This would work:, for example

    When play begins:
         Now Z is X minus Y.

## Related questions

- What's the best way to ask if any of a kind has a certain value for a property?
- Copying the location of a thing to a variable when it's in something other than a room.
- How do I move an item I have more of in the world (since it's a kind of thing)?
- How to set up a condition involving the presence of a character.
- What are some of the classic Inform 7 mistakes to look out for?

...