Good explainers or tutorials for Inform 7 variables?

I’m working on a project that involves a lot of randomization and procedural generation… and as a neophyte Inform 7 developer and programmer in general, the natural language element sometimes trips me up in terms of where and how I can apply the ‘things’ and ‘values’ and such. I often get stuck trying to apply a more global type when what I need is a specific application.

I just got some help over here: Assigning random colours to off-stage things

But it got me wondering if there are any specific tutorials or articles on this element specifically. I’ve found most of the major Inform 7 tutorials, and Ryan Veeder’s dinosaur project. (He’s also been a major help so far!) But I haven’t found documentation that really walks you through the global vs specific in terms of variables… until I blunder into the problem and get help!

1 Like

Hi, Jason:

I agree, it’s difficult find specific answers, you’re dependent on what the author of the material thinks should be included.

I have six or seven books (PDFs) on Inform as well as a couple I purchased.

I rely on this community a lot for help. They are always very pleasant and helpful, not trolling.

I don’t think Inform 7 makes the distinction between “global” and “local” variables the way some systems do - specifically sometimes you want a global variable for the player’s strength or the amount of money they carry, and a local or temporary variable for, say a dice roll.

The only major difference is you don’t need to declare temporary variables ahead of time; it’s done inside a rule or declaration.

To count possessions:
     let T be the number of things enclosed by the player;
     say "You're carrying a total of [T in words] thing[if T is greater than one]s[end if] things.";

Report taking inventory: count possessions.

I don’t believe you can then reference “T” later because it’s only set when you evoke “count possessions” and always needs to be set during that process.

A “repeat running through” also sets a temporary variable:

Repeat with item running through wet metal things held by the player:
     now item is rusty.

Don’t confuse adjectives/properties with variables - an adjective/property is usually an “is or is not” property and isn’t really variable:

A thing can be wet.
A thing can be metal.
A thing can be rusty.

In most systems variables can be boolean (true/false/null), a number, or a string. Inform allows you to specify what kind a variable can be.

Strength is a number that varies.
The suspect is a person that varies.
The leaky vessel is a container that varies.
The correct way is a direction that varies.

4:12 Values that Vary

The tricky one for me is when you want to name your own valued properties that a thing can have.

Brightness is a kind of value. The brightnesses are guttering, weak, radiant and blazing.
The lantern has a brightness. The lantern is blazing.

4:9 Using new kinds of value in properties

[Sees @zarf replying and anticipates learning what parts I got wrong :joy: ]

2 Likes

This is the same distinction that other languages make between global and local variables. A local variable only exists inside a function (I7 rule or phrase).

4 Likes

I see that Manon’s prediction was correct… :rofl:

best regards from Italy from an amused
dott. Piergiorgio.

At risk of overly-linking the two conversations, Zarf clarified some points of what I was trying to do and what was possible, but the lessons I’m trying to learn from it apply to this conversation.

Generally, that calling and applying variables are still discrete steps. (And that using natural language might make it harder to realize that.)

Yeah, I think the thing to realize is that Inform is actually pretty similar to other languages, it still has specific phrasings that it recognizes and that mean specific things: they’re just generally written with more words and less punctuation.

I wouldn’t say “calling” vs. “applying”, I’d probably say “making/creating” vs. “changing.”

So let makes a new temporary variable: you might say “let X be a number.” It can also assign a value to that variable at the same time (and guess the type) as in “let Y be 5.”

now changes the value of an already-existing existing temporary variable, as in “now X is 72.”

And as zarf just said, the “repeat running through” loop makes its own temporary variable (you don’t use “let” here, like you would in some languages) but that temporary variable doesn’t have to be called “item” – you could name it whatever you want.

repeat with oneOfThePens running through off-stage pens:

I wonder if Zed’s edition of Ron Newcomb’s Inform 7 for Programmers might be helpful? Or maybe Chapter 11 of Writing with Inform, which goes through control-flow structures, specifically 11.15 (let and temporary variables) and 11.11 (repeat running through)? Hmm. There doesn’t seem to be a good way to link to the table-of-contents for just that chapter, but they are listed in small italic text at the top of the chapter…

3 Likes