Defining Variables and Lists

Hello, I’m new to programming in Inform 7. I am a veteran programmer, and I know many languages. Somebody showed me the source code to an Inform 7 program, and I thought it was so ridiculously verbose and obtuse that I had to give it a try.

Right now, I am completely stuck when it comes to creating variables, lists and tables.

The documentation seems to suggest that the following should be valid:

let Messages be a list of text;

add "...." to Messages;

add "..." to Messages;

add "..." to Messages;

However, the compiler complains about not understanding “let”. This is the case if I use “let” for anything.

For instance this is illegal as well, apparently:

let X be 5;

If I try this instead:

Messages is a list of text;

The compiler complains that “list of text” is a type, and so can’t be used in an assignment.

How is this done?

The problem isn’t your syntax, but (I’m guessing) that you’re using these phrases in a context where Inform expects an assertion or a rule header. (If you are unfamiliar with the idea of assertions and rules, see sections 2.1 and 2.2 in the documentation.) Basically, you’re writing procedural code without any wrapper to tell Inform what function it should belong to.

If you want to do something as part of a rule, you can use that “let…” syntax. If you are trying to create a global list variable, then you need an assertion, and the form needs to be “a list of text that varies”.

The following code demonstrates:

[code]Foo is a room.

[Here’s a rule:]
Every turn:
let Messages be a list of text;
add “…” to Messages;
add “…” to Messages;
add “…” to Messages;
say “Messages are: [Messages][line break]”;
say “Other Messages are: [Other Messages][line break]”;

[Here’s an assertion:]
Other Messages is a list of text that varies. Other Messages is { “bar”, “baz” }

Test me with “z”. [/code]

That said: Inform is a domain-specific language; if you’re trying to do something in it that is deeply contrary to its purpose, then it’s unlikely to show its strengths.

Ah, okay, so the declaration of local and global variables differ! It would have been very helpful if the parser gave a more useful error here :frowning:

I am not trying to do something outside of I7’s domain. I am making a text adventure game. Specifically here, I am trying to make a phone have a list of messages that gets decremented every time the player answers it.

You may find the programmer’s guide to Inform 7 a bit more direct in its explanations. (see my sig)

I find that Tables are a much more reasonable way of managing that sort of thing in Inform.

In fact, I hardly ever need to use local variables at all. A lot of things can be done with properties (i.e. object member variables) and adjectives (a bit like functions that return a value for a particular object, or when used in the context of descriptions, functions that return a list of objects having the same value). Of course I use global variables sparingly, but they can definitely be useful for keeping track of the overall game state, and in my experience it’s not at all unreasonable for them to look very different from temporary variables in rules.

Cool. Sorry if I sounded unhelpful – the opening of your post just made me think you were setting out to break the system to prove a point, and, well, that’s easily done and not very rewarding. :slight_smile:

Game writing questions are always welcome.

Coming at I7 as a progammer - especially if you take a procedural point of view such as a c programmer might - can often cause you to devise solutions for game problems that don’t work very well for I7. I’ve found it more productive to let I7 expand my view of progamming languages than to fit it into my existing view of what a programming language is. Don’t let the “natural language” veneer fool you- underneath the English-like syntax is an amazing new thing that’s not like “natural” language or “computer” language as one might have conceived it. Although the minimal punctuation does make it more friendly with text-to-speech readers.

It took me a long time to get the hang of object-oriented programming, and I’m still getting the hang of the kind of asynchronous things you have to do in GUI code… it took me a long time to get the hang of the predicate style of SQL… and now I’m trying to get the hang of I7’s rulebook structure, which is very powerful, and unlike anything I’ve ever used before. By the way, descriptions in I7 are a lot like “where” clauses in SQL.

So, as a programmer, I encourage you to seek out I7’s strengths, and not see its differences from other languages as weaknesses. Ron Newcomb’s book is immensely valuable for that.