Table to set criteria for binary character response

I’ve created a table of characters (NPCs) that sets the initial location and produces a unique description for each character. What I also want the table to do is return a specific response (e.g. “Yes.”) if the player ‘answers a character that something’ and the topic matches (or is) an entry in that character’s row in the table – and to return a different response (e.g. “Nope.”) if the player ‘answers a character that something’ and the topic isn’t that entry in the character’s row. Here’s an example:

Table of characters
character [TAB] drink
referee [TAB] lemonade
senator [TAB] soda
driver [TAB] juice
student [TAB] water

When the player speaks to a character and mentions that person’s favorite drink (e.g. SENATOR, SODA), the game should be able to give a different answer (e.g. “Yes.”) than when the player says anything else to the character (e.g. “Nope.”).

Currently I’m using the code below, and I get the “Yes!” response no matter what I say to any of the characters in the table.

Instead of answering a character that something:
Repeat through the Table of characters:
if the topic is the drink entry, say “Yes!”;
Rule succeeds.

Here the simplest approach is just to look up the drink entry corresponding to the character you have answered to: ‘the noun’ is set to the character you have answered to, so you can use that to look up the drink they like, as so:

Instead of answering a character that something:
	if the drink corresponding to the character of the noun in the Table of characters is the topic understood:

To make your repeat loop work, you’d need to make sure that you’re checking both the character and the topic together:

if the character entry is the noun and the drink entry is the topic understood, say “Yes!”;

Note that if you’re going to ask Inform to compare a text column in a table to a ‘topic understood’ (which is technically not a text, but a snippet) you must compare the text to the topic, not the topic to the text (i.e. ‘if the drink entry is the topic understood’, not ‘if the topic understood is the drink entry’), otherwise the comparison won’t work. Doing it the first way ensures that the topic is converted to text to enable the comparison to take place. Doing it the second way, the topic is not converted to text and the comparison is always false.

So counter-intuitively, ‘X is Y’ can be true if X is a text and Y is a snippet, but nevertheless ‘Y is X’ is always false.

One other way round this, apart from making sure the text column comes first in the comparison, is to force the conversion of the topic understood to text by making use of a text substitution: ‘if “[topic understood]” is the drink entry’.

The ‘correct’ way to do it, if you don’t need the drink column to be a ‘text’ column for other purposes, is to label the column heading ‘topic’ rather than ‘drink’ (or ‘drink (topic)’ if you want to keep calling it drink)- that way Inform knows to treat the contents as topics rather than as texts- and compare using ‘if the topic understood matches the topic entry’ (or ‘if the topic understood matches the drink entry’ if you’ve labelled it ‘drink (topic)’). Note that when using ‘matches’ the snippet always comes first and the topic second.

2 Likes