Using tables

I don’t know about anybody else but I find the table documentation lacking in the included inform documentation.

How do I choose a random row of a table? How do I assign random values from that corresponding table rows to variables?

Sorry for the beginning question, I appreciate the input.

2 Likes

16:5

We can also choose a row quite at random:

choose a/the/-- random row in/from (table name)

This phrase makes a uniformly random choice of non-blank rows in the given table. Note that although a table always has at least one row, it can’t be guaranteed that it always has a non-blank row, so it’s possible for this to fail: if it does, a real-time problem message is thrown.

I don’t work with tables much at all, but I know there’s a way to “repeat through” a table and grab elements for text substitution, so I’m sure you can assign table entries to variables.

2 Likes

Away from my computer right now so I can’t test but something like this should work, I think:

Choose a random row in the Table of Randomized Results;
Let A be the name entry;
Let B be the year entry

(Made up the table and column names, of course)

5 Likes

Thank you for the quick help, this entry worked. How would I print A in the description of a room? [A]?

Yeah, but “let A be (blah)” is only for values that are part of rules (local variables, if you’re familiar with the term). For putting them in a description like that, the easiest thing would be to add a line earlier in your program saying “A is a number that varies” (or whatever kind of value it is), and then in the rule you’d say “now A is (blah)”. Hopefully that makes sense - sorry, still typing on my phone!

2 Likes

Now if anyone can tell me why my variables aren’t printing:

Aentry is text that varies.
Bentry is text that varies.
Centry is text that varies.
Dentry is text that varies.
Eentry is text that varies.
Fentry is text that varies.

carry out entering Operation_room for the first time:
	choose a random row in the Table of Patient Problems;
	now Aentry is the BP entry;
	now Bentry is the Description of issue entry;
	choose a random row in the Table of Patient Problems;
	now Centry is the BP entry;
	now Dentry is the Description of issue entry;
	choose a random row in the Table of Patient Problems;
	now Eentry is the BP entry;
	now Fentry is the Description of issue entry;
	continue the action.

medical chart is a written thing on coffee table.  it is readable. it has description "It two pieces of metal with pages in-between, the type of medical chart that you see in hospitals."  The writing of the medical chart is "Medical conditions to examine: [Paragraph break][Bentry][paragraph break][Dentry][paragraph break][Fentry]".

When I read the chart it says “Medical conditions to examine:” and then nothing but blanks.

1 Like

If you take out “for the first time” does it work? “For the first time” is tricky because it counts only once in the game whether the rule succeeds or not and you usually want something like this in an After rule in case “carrying out going” fails initially.

If this only gets set once in your game, you might want to make it a “When play begins…” rule.

“Entering” does not usually apply to rooms, so that code block won’t be executed. Instead of the line carry out entering Operation_room for the first time, you could use When play begins (as Hanon said), or Before going to the Operation_room for the first time or After looking in the Operation_room for the first time (as looking is automatically carried out when the player moves into a room or starts out in it).

4 Likes

Thank you!