Magnetic poetry

Thanks everyone for bearing with my questions so far!

I’ve got a magnetic poetry minigame that looks like this…

Some magnets are in the Kitchen. Understand "magnet" as the magnets. The description of the magnets is "Tiny flat magnets, each with a word printed in black type on a white background. Currently, they are ordered to read '[magnetic poetry]'."

The magnetic poetry is a text that varies. When play begins: now the magnetic poetry is "a day summers to".

Rearranging it to say is an action applying to one thing and one topic. Understand "rearrange [something] to say [text]" or "arrange [something] to say [text]" or "rearrange [something] to read [text]" or "arrange [something] to read [text]" or "move [something] to say [text]" or "move [something] to read [text]" as rearranging it to say.

Check rearranging it to say:
	if the noun is the magnets:
		if the the player's command matches the regular expression "((to|a|summers|day) ?){4}":
			continue the action;
		otherwise:
			say "Not with the magnets you currently have!";
			stop the action;
	otherwise:
		say "That doesn't seem like a good idea...";
		stop the action.
		
Carry out rearranging it to say:
	now magnetic poetry is "[text matching regular expression]".
	
Report rearranging it to say:
	say "You rearrange the magnets to say '[magnetic poetry]'.";

…but for some reason, the “carry out” seems to be happening even if the check fails?

>arrange magnets to say "to a summers day"
You rearrange the magnets to say "to a summers day".

>x magnets
Tiny flat magnets, each with a word printed in black type on a white background. Currently, they are ordered to read "to a summers day".

>arrange magnets to say "a summers to day"
You rearrange the magnets to say "a summers to day".

>x magnets
Tiny flat magnets, each with a word printed in black type on a white background. Currently, they are ordered to read "a summers to day".

>arrange magnets to say butt
Not with the magnets you currently have!

>x magnets
Tiny flat magnets, each with a word printed in black type on a white background. Currently, they are ordered to read "".

The value of “magnetic poetry” shouldn’t be reset unless the regex matches, but for some reason it’s getting reset to the (in this case empty) regex - how come?

(By the way, if anyone has suggestions for other synonyms of rearrange I will add those in. I’m also aware that “summers summers summers summers” would be valid, but can’t figure out how to prevent it and don’t really consider it worth the time to do so.)

This line is the problem. The thing to bear in mind is that text like this is actually saved as a routine that will always print the current value of any variables referred to – and this particular variable is only valid within the context of the current regular expression match, and becomes invalid once the action has completed.

There’s two different ways to solve this – one:

	now magnetic poetry is the substituted form of "[text matching regular expression]".

This tells Inform to “expand” the variables immediately rather than saving it as a routine for later. (This is explained in WI20.7.)

The other is:

	now magnetic poetry is text matching regular expression.

By omitting the quotes you’re also telling Inform to evaluate the current value immediately and assign that to your variable, rather than saving an expression that will look it up later. (I’m not sure if there’s a specific WI reference for this; it’s just how code works.)

(These aren’t quite identical; you can construct some text that will behave differently in each case. But for this particular case, either will serve.)

Incidentally, you can simplify the place where you declare and initialise your poetry as well:

The magnetic poetry is initially "a day summers to".

Inform can infer that you’re declaring a text variable just from this initial assignment.


On a side note, your regular expression doesn’t prevent someone adding extra words at the beginning and end of their command – it will accept anything as long as the four words in the middle are the correct ones in any order (with optional space). Though it’ll also ignore these extra words when assigning the magnetic poetry, so maybe it doesn’t matter too much. Fixing this would be tricky, since you’d have to account for optional quotes and that’d complicate the assignment as well.

2 Likes

Perfect! Thank you. I didn’t realize that the way I had written it would mean that the value of magnetic poetry would always be the current value of the regex match rather than just being set at that one moment, but now that you’ve explained, it makes perfect sense.