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

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.
This site is now closed.
As of 1st November 2015, this site is a read-only archive. For more information see the intfiction forum post

Welcome to IF Answers, a site for questions and answers about Interactive Fiction.

Technical questions about interactive fiction development tools such as Inform, Twine, Quest, QuestKit, Squiffy, Adrift, TADS etc. are all on-topic here.

Non-technical questions about interactive fiction are also on-topic. These questions could be about general IF design, specific games, entering the IF Comp etc.
...