Error when adding to a table?

Posted by Faedara on Tue 05 Jul 2011 12:10 AM — 15 posts, 53,146 views.

#0
I'm using Trevize's old rift script (Achaean) because I like a neat and orderly view of my rift, but it hasn't been updated since the reagent release. For those of you that don't know it, this is the location of the Rift XML file: http://sites.google.com/site/trevizemoonflair/main/MUSHclient.zip?attredirects=0&d=1
Not too complicated, but when I tried adding a reagants table/group to it I kept returning a parsing error, saying that I was returning a nil value.

Does anyone know how I would add a new group to the existing table?
USA #1
Can you show us the full error you're getting, and what you're changing?
#2
I changed the following lines:
326
cats = {"herbs", "concoct", "inks", "comms", "crys", "misc", "reag"},

408-410
    reag = {
      ["buffalo horn"] = "bhorn",
      },

431
   reag     = {bhorn     = 0}

555
    reag    = false,


And get this:
Run-time error
Plugin: Rift (called from world: Achaea)
Immediate execution
[string "Plugin"]:214: attempt to index field '?' (a nil value)
stack traceback:
        [string "Plugin"]:214: in function 'parse'
        [string "Trigger: riftitem"]:4: in main chunk
Error context in script:
 210 :   parse = function (item, amount)
 211 :     local x = false
 212 :     for k, v in pairs (rift.table) do
 213 :       if v[item] then
 214*:         rift.current[k][v[item]] = amount
 215 :         x = true
 216 :       end -- if
 217 :     end -- for
 218 :     if not x then


Which, if I knew what it meant, would probably have a very simple explanation.
USA Global Moderator #3
Quote:
Which, if I knew what it meant, would probably have a very simple explanation.


The first step to successful programming is understanding what your error messages mean.

attempt to index field '?' (a nil value)

"attempt to index...a nil value"
Means that you tried to look deep inside a non-existent table index.

Let's say you have a table 'foo' that contains three keys 'bar', 'buzz', and 'pants' with associated values. If you try to look at foo.pants (equivalently foo["pants"]), you'll get whatever is stored for that key. Maybe that's a number or maybe that's another table. If you try to look instead at foo.hello_my_name_is_fred, you'll get nil, because foo doesn't have anything stored for that key. Now let's say you ignore the fact that foo doesn't have a "hello_my_name_is_fred" key and try to look at foo.hello_my_name_is_fred[5], that is index 5 into an imaginary table at key "hello_my_name_is_fred" in table foo. Now you'll get an error like above, because you just tried to examine some index of something that doesn't even exist.

214*:

The asterisk in the trace context means that the error happened on this line.
Amended on Tue 05 Jul 2011 04:07 AM by Fiendish
#4
I know all that stuff, problem is that line is NOT where the error says it is, and I don't know what's drawing the nil.
USA Global Moderator #5
Faedara said:

problem is that line is NOT where the error says it is


It is. The line numbers do not start at the beginning of the file. They start at the beginning of the script block inside the file. You're given the line. You can just search for it. Or find the line number in the file where the script block begins and add that to the given number.
Amended on Tue 05 Jul 2011 04:14 AM by Fiendish
#6
Already did that using the search function. Doesn't help me any to know there's a problem I don't understand sadly.
USA Global Moderator #7
Quote:
I don't know what's drawing the nil.

Try adding
print(k, rift.current, rift.current[k])
above the line where the error happens.
Australia Forum Administrator #8
Quote:



 214*:         rift.current[k][v[item]] = amount



Unfortunately there is a lot if indexing going on in that line. Sounds like one of them is nil.

if "item" nil? Is v [item] nil?
Is "k" nil?
Is rift.current (that is, rift ["current"]) nil?

You might have to throw in some print statements to see what is going on. And even that might not tell you how to fix it.

Fiendish was basically saying the same thing. And yes, the line numbers are from the start of the script, not the start of the plugin. The error context it showed is probably right.

You said you "added a new group" to an existing table. I would look closely at your changes. Perhaps if you explained the before and after some vital step that you omitted might become clearer.
#9
It is the reagent table returning a nil:
reag table: 02E77BE8 nil
#10
Alright! After extensive lazy testing I've figured out that there are TWO tables for each category. Hence the reason the error shows up in K for V in pairs:

  [ 323] bloodroot leaf     [   8] blue ink           [ 102] buffalo horn
herbs table: 023F0808 table: 023DECD0
inks table: 023F0808 table: 023E06C8
reag table: 023F0808 nil


I'm going to continue to work on this myself, but if anyone knows anything feel free to let me know what you know so I can know what I don't know.
USA Global Moderator #11
It looks like the script just assumes that rift.current and rift.table are the same. But they aren't. Clearly rift.current[k] doesn't always exist when rift.table[k] does. Or maybe rift.current doesn't exist at all! Is rift.current supposed to be initialized to contain all the same keys as rift.table at some point? Maybe you should just throw in
rift.current = rift.current or {}

above the loop
and then put
rift.current[k] = rift.current[k] or {}

before the inner assignment
Amended on Thu 07 Jul 2011 03:02 AM by Fiendish
#12
Huh... so there's a problem with the way the script pairs the item and it's... nope, I don't get it. Years of using lua and everything about tables still eludes me, that's kinda sad.

Also, I tried calling the item table itself, and it gave me an entirely new error. Not sure if it helps though:

Run-time error
Plugin: Rift (called from world: Achaea)
Immediate execution
[string "Plugin"]:63: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
        [C]: in function 'pairs'
        [string "Plugin"]:63: in function 'sortedpairs'
        [string "Plugin"]:252: in function 'displaygroup'
        [string "Plugin"]:242: in function 'display'
        [string "Alias: "]:1: in main chunk
Error context in script:
  59 : 
  60 : sortedpairs = function (tbl)
  61 :   local i = 0
  62 :   local tmp = {}
  63*:   for n in pairs (tbl) do table.insert (tmp, n) end
  64 :   table.sort (tmp)
  65 :   return function ()
  66 :     i = i + 1
  67 :     if tmp ~= nil then return tmp, tbl[tmp] end


Also, there are several loops and I'm not sure what an inner assignment is. I feel computer illiterate around you coders.
Amended on Thu 07 Jul 2011 03:05 AM by Faedara
USA Global Moderator #13
bad argument #1 to 'pairs' (table expected, got nil)

pairs() and ipairs() can only be called on a table. Your "tbl" value isn't a table. It is nil.

Quote:
I'm not sure what an inner assignment is

By "inner assignment" I just meant this line:
rift.current[k][v[item]] = amount

It's an assignment inside the loop.

Quote:
there are several loops

There is only one loop in the first error message you posted.
Amended on Thu 07 Jul 2011 03:17 AM by Fiendish
#14
Amazing! After I added that, it created the second reag table when I took the item out of the rift, so it has something to do with how the script tracks item withdrawals I believe. Either way, whatever you did worked like a charm and I can get this posted up once I register all the items into it and build an inkmilling counter (a lot like the tattoo one already on there)

Thank you!!