Create boolean variable in Inform 7

Hello, I’m coming from a programming background (Java, C++, C#, python, etc) and this seems like it should be a basic thing but I’m having trouble creating a boolean variable in Inform 7. I’ve read several seemingly-relevant chapters from the Inform 7 book but haven’t found any explicit explanation on how to do this.

Say I want to make a variable called “tired” that can be either true or false.

Are boolean variables in Inform called truth states?

I’ve tried the following:

tired is a truth state variable. 

and

tired is a truth state that varies.

But neither of those compile.

I’ve also tried just declaring and initializing the boolean at the same time with

When play begins:
     now tired is false.

Can someone point me in the right direction?

Thanks,
Mark

tired is a truth state that varies. works fine for me.

Maybe there’s something else going on in your code? What error messages are you getting?

1 Like
Salvation earned is a truth state that varies.

When play begins:
	now salvation earned is true.

Bland Room is a room.

Instead of jumping:
	if salvation earned is true:
		say "You jump to heaven.";
	else:
		say "You jump on the spot, fruitlessly."

This compiles for me. (Inform documentation: §11.5. Conditions and questions, in case it was a different section you saw.)

2 Likes

tired is a truth state variable is usually the way to do it. But you can make it even simpler. Minimal compiling example:

There is a room.
tired is initially false.

And Inform will figure the rest out.

Note that declaring it outside of a rule makes it a global variable (and globals must be declared outside of rules). If you wanted a local boolean inside a single rule only, the syntax for that is different:

Before sleeping:
	let tired be whether or not the number of things carried by the player is greater than 15;
	if the anvil is carried, let tired be true;
	unless tired is true, say "But you're not tired enough to sleep." instead.
1 Like

Thanks guys! I got it to work. There WAS a problem somewhere else in my code.

I think I am still learning which words are reserved words in Inform.

I tried to create a truth state variable called “Inside Warehouse”. But I also had a backdrop called “Warehouse”. So I think the problem stemmed from the fact that “inside” is a reserved word, so Inform was trying to figure out what I meant by “Inside Warehouse” since “Warehouse” was already a thing (is that a correct interpretation of the issue?), instead of treating “Inside Warehouse” as a two-word variable name (as I’d intended it).

I’m also unused to being able to have variable names with more than one word! And that leads to some confusion on my part as to how the compiler will interpret things.

At any rate, I’m glad to learn I wasn’t too far off base when it came to defining a boolean variable. :slight_smile: I just had to ditch the two-word variable name and it worked. Then the " is initially false" was perfect for what I wanted.

Thank you!

PS - I made the classic forum-posting gaffe here of simplifying my code for clarity for posting and simplified it too much until it turned into something that does actually work. My apologies for the wild goose chase.

1 Like

Also, if the fact of being tired applies to the player specifically, it might be better, style-wise, to write:

Yourself can be tired. [yourself is the default object for the player.]

[Then, in a rule]
if the player is tired:

(It’s mainly a matter of style. I believe it’s more “Informian” to use either/or properties than boolean variables.)

As for your second issue, I think it’s not strictly speaking because of reserved words; it’s just that Inform can be confused when a word has multiple meanings (inside is already a direction, for instance).

A way to avoid that kind of problems is to make your names single-word, by using dashes for instance (i.e. inside-warehouse). In the cases of objects, you’ll then need to add the printed name and understand lines.

2 Likes

If you haven’t seen the “Inform 7 for Programmers” resource do check it out. It’s handy for things like this.

http://www.ifwiki.org/index.php/Inform_7_for_Programmers

Good luck!

Thanks, I had not seen this. So far I’ve been using inform7.com/book, mainly because that’s what Google keeps pointing me to…

I’ve done this too! To help avoid this, I have a “sandbox.inform” project where I try to reproduce the simplest test case that fails, then build up to when and how things go wrong

So often when the simplest case works and my in-game case doesn’t, it’s because I misused a reserved word, or I let two variables’ name spaces step on each other. For instance, one thing that kept stinging me was where I had “giant pin” and “old giant” but “giant” went to “giant pin,” which was defined first. I’d be interested if there would be a way to direct Inform when I get sloppy with namespaces, but I suspect it’s something I need to do for myself.

Another solution to your problem might be a “decide whether” routine:

to decide whether inside-warehouse:
    if Warehouse is touchable, yes; [could also be "visible" but that's slower]
    if player is not in Security Gate, no; [this is just an example to allow for seeing the warehouse from outside]
    no;

I use hyphens for boolean variables a lot (‘inside-warehouse’), and I’ve even experimented with having a header file where I define all my variables. (I could make a volume in story.ni, I guess, but separating the code that much more helps me get organized. It’s not hard to make a python script that tracks all the organization.)

Hope this helps!

1 Like