Tables as variables

Have I found a problem in Inform regarding tables?
I have many tables. I use them to initialize the properties of things in those tables, like this:

To initArmorTable:
	repeat through the Table of Body Protection:  
		assign traits to item entry consulting Table of Body Protection; 
		now AC of item entry is AC entry; 

To assign traits to (LocalItem - a thing) consulting (LocalTable - a table name):
	choose row with an item of Localitem in Localtable;
	now qty of LocalItem is nbr entry;
	now cost of LocalItem is cost entry;
	now weight of LocalItem is weight entry;

Where the table in this case is

Table of Body Protection
Item		Nbr	Cost	Weight 		AC
padded robe 		2	0.5	1.0		11
leather armor 		3	15.0 	20.0		13
chain mail		2	75.0 	50.0		15
plate mail 		1	400.0	80.0		17
shield wooden	3	0.9	5.0	1
shield metal		2	15.0	15.0		2

However, when I try to make a function out of it, like this:

To init (tbl - table name):
	repeat through tbl:
		assign traits to item entry consulting tbl;

Inform acts like the table doesn’t exist. The error is a runtime error:

At present, no table row is selected, so it makes no sense to talk about any of its entries.

I thought the repeat through table name assigns rows one at a time; not me in this particular phrasing.

I can’t test immediatly (sorry for that), but could you please try:

To init (tbl - table name):
    repeat through tbl:
        let current item be the item entry;
        assign traits to current item consulting tbl;

What I understand : “repeat through” creates a temporary row context that is valid only within the loop’s block. However, when calling an external phrase, this context is not preserved. By explicitly setting the current item, I think it will work.

I’m able to get this to do what I think you’re asking by assigning the value of the table entry into a variable first within the repeat loop, and then assigning traits.

To be clear, this is what @Monsieur.HUT has suggested above me, so here I’m doing this to demonstrate the solution.

Lab is a room.

A thing has a number called qty.
A thing has a real number called cost.
A thing has a real number called weight.
A thing has a number called AC.

A padded robe, leather armor, chain mail, plate mail, shield wooden, and shield metal are in the Lab.
	
Table of Body Protection
Item		Nbr	Cost	Weight	AC
padded robe 		2	0.5	1.0	11
leather armor 		3	15.0 	20.0	13
chain mail		2	75.0 	50.0	15
plate mail 		1	400.0	80.0	17
shield wooden	3	0.9	5.0	1
shield metal		2	15.0	15.0	2

To assign traits to (LocalItem - a thing) consulting (LocalTable - a table name):
	choose row with an item of Localitem in Localtable;
	now qty of LocalItem is nbr entry;
	now cost of LocalItem is cost entry;
	now weight of LocalItem is weight entry;
	now AC of LocalItem is AC entry.

To init (tbl - table name):
	repeat through tbl:
		let item be Item entry;
		assign traits to item consulting tbl;

When play begins:
	init Table of Body Protection;

The key addition is let item be Item entry; and then adjusting the next line to use the variable: assign traits to item consulting tbl;. Then I can assign the traits defined with the definition where you assign traits.

Within the output here, I’m showing that the properties of the items have been correctly assigned the traits found from the table.

Output:

Lab
You can see a padded robe, leather armor, chain mail, plate mail, shield wooden and shield metal here.

>showme padded robe
padded robe - thing
location: in Lab
unlit, inedible, portable; singular-named, improper-named
description: none
initial appearance: none
qty: 2
cost: 0.5
weight: 1.0
AC: 11
printed name: "padded robe"
printed plural name: none
indefinite article: none
list grouping key: none

>showme leather armor
leather armor - thing
location: in Lab
unlit, inedible, portable; singular-named, proper-named
description: none
initial appearance: none
qty: 3
cost: 15.0
weight: 20.0
AC: 13
printed name: "leather armor"
printed plural name: none
indefinite article: none
list grouping key: none

Edit: had to edit to clarify how to use the item variable in the line after.

1 Like