advanced table HELP! how to reference key pairs from table 2

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

advanced table HELP! how to reference key pairs from table 2

Post by Komag »

I'm a bit stumped here, trying to build up a table of strings and then convert them to numbers based on another table I've pre-defined.

This isn't exactly what I'm doing, but an illustration:

the "dictionary" table: (within script entity "theScript")
G = {arrow = 5, rock = 3}

If I type in console:
print(theScript.G.arrow)
I get
5
which is correct, theScript being the script entity, G being the table, and arrow being the key to value 3

I can also type in:
print(theScript.G["arrow"])
and I still get
5
which the alternate way, fine.

So my champion is carrying an arrow and a rock and I have a function in a script to search his whole inventory. When it find the arrow and the rock it adds their names to a table:
T = {}
table.insert(T,item.name)
and at the bottom of the search inventory function I have
for i=1,#T do print(T) end
and when I run the function it prints
arrow
rock

which is correct

So here's the problem, I can't get it to print an item from table T in terms of table G.
If I add this at the end of the search inventory function
for i=1,#T do print(g.T) end
and run the function it prints
nil
nil

??? why? :( :x :cry:
I tried:
for i=1,#T do local b=T print(g.b) end
but no luck, still the nil nil
I tried
for i=1,#T do local b=T print(g.) end
but then get script error '<name>' expected near '['

How can I do this???
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: advanced table HELP! how to reference key pairs from tab

Post by Komag »

holy cow I was so close!

ANSWER
for i=1,#T do local b=T print(g) end
works beautifully! prints:
5
3


And the local isn't needed, as this works too:
for i=1,#T do print(g[T]) end

never tried nesting brackets like that!
Finished Dungeons - complete mods to play
Post Reply