Changing thing description each time examined skips numbers

This is elementary, but there seems to be a bug that I can’t spot.

I want to change the description of a thing, in relation to how many times it gets examined. Suppose there are some graffiti on the wall, which the player examines, one after the other.

The following code seems right to me, but it skips numbers; it starts from the 2nd graffiti and then shows the 4th. I don’t get it.

"Test graffiti bug 2"

The Bathroom is a room. "All mod cons. There is some graffiti on the tiled walls."

The graffiti is scenery in the Bathroom. The description of the graffiti is "You go closer and read: [wall-writing]."Understand "wall/walls/tiles/tile/tiled" as the graffiti.

The wall-num is a number that varies. The wall-num is 0. [This increments, as we read the graffiti, one after the other, until we see the last one.]

To say wall-writing:
	if the wall-num is 0:
		say "'First graffiti...' Okay";
		increment the wall-num;
	else if the wall-num is 1:
		say "'Second graffiti...' Right";
		increment the wall-num;
	else if the wall-num is 2:
		say "'Third graffiti...' A-ha";
		increment the wall-num;
	else if the wall-num is 3:
		say "'Last graffiti.' Super".

It gives:

It’s not a bug, but it’s an unobvious behavior.

The system can inspect a text either to print it or to test its value. In particular, the EXAMINE action tests the description of an object to see if it’s empty. (If it’s empty, it prints “You see nothing special.”) Testing the value of a text means experimentally running all its embedded code. The result isn’t printed, but it still runs.

So you’re incrementing your value during these non-printed activations.

The easy way to avoid this is to use one of the variation constructs describe in 5.7.

say “You read: [one of]This[or]That[or]Other[or]Last[stopping].”

If you want to write it yourself, as above, you can check “if expanding text for comparison”

		if not expanding text for comparison purposes:
			increment the wall-num;

Thank you.

I see what you mean. But now you’ve made me paranoid; I’ll never feel safe using this kind of structure again. :open_mouth:

In a nutshell, the moral of the story is not to use “increment” within the frame of a “to say…” phrase or other text tokens?

The safe version is: don’t put any code that changes state into a text token. Or if you do, use the “if not expanding text for comparison purposes” test.

There are situations where I don’t worry about it. For example, I sometimes write code like

A thing can be known.
To say set-known:
	now the item described is known.
The tablet is a thing.
The description of the tablet is "It's shiny[set-known]."

Now if you examine the tablet, the set-known phrase will be called twice. But that’s harmless. It’s not like increment, where calling it twice gives you the wrong outcome.

Zarf, thanks so much. This really clarifies things for me.

Re your code:
This is interesting. The “item described” value can actually be carried along into the “to say” phrase?

I suppose that this is not a safe practice in general, in situations where the “to say” phrase can be called by different rules, and not only descriptions of items, correct?

“item described” is used in a bunch of situations. In this case: when you invoke a text property of an object (e.g. the description property), “item described” will refer to the object.