Lua Table Indexing

by Simimi

Back to Mechanic's Corner.

Simimi2007-07-16 13:52:19
Still working on that old system... I ran into a new problem the first time I tried to work with actual afflictions. I have lookup tables for various afflictions and such in my script. Here is the issue currently.

When I am hit with an affliction I get this error.
CODE
un-time error
World: Lusternia
Immediate execution
:1: table index is nil
stack traceback:
        :1: in main chunk


The trigger it is referring to is...
CODE
^cursed by manabarbs\\.$

This is supposed to send the affliction to a table of current afflictions called "affs" such that;
CODE
affs = true OR addaff (manabarbs, true)

adds the aff to
CODE
affs = {}



Now, I said OR because I tried them both, as I was not sure which was the proper way to set the affliction in the table. The affliction source I am using was given to be to use by Zarquan and Demetrios in my old thread, entitled Prompt Trigger Question. This is it.

CODE
function addaff(aff, val)
  -- If it's not a valid affliction name, leave the
  -- function early to avoid crashing
  if not aff then
    return
  end

  -- This will default "val" to true if it's omitted from
  -- the arguments passed in
  val = val or true

  -- You can pass in a table of afflictions to save calls,
  -- or you can omit this as it's really just extra fun
  if type(aff) == "table" then
    for _,v in pairs(aff) do
      addaff(v, val)
    end
    return
  end

  -- The actual setting of the affliction in the table
  affs = val

  -- This is where I set flags on which healing
  -- queues to check on the next prompt, but
  -- you do your curing however you like...
end



I am clearly doing something simple, incorrectly. I am at a loss at the moment, any ideas?

EDIT: Ok, so I have an alias such that
CODE
affs
SEND: print(affs)


If I have a paralysis trigger send affs.paralysis = true to the script, I get no error return, but on printing the table to see what affs I have, I get this...
CODE
table: 015CA350


so now I can not tell if anything is working or not...
Unknown2007-07-16 14:37:38
I don't see anything wrong so quickly.

However, you might want to take a look at http://lua-users.org/wiki/TableUtils for some ways to print tables to text.

I usually just do something like:

for a,b in pairs(TABLE) do echo( tostring(a) .. "-" .. tostring( b )) end


Edit:
Oh, and by the way:

I saw two things

affs.paralysis = true
and
affs = true

Which are really different things. In the second case you take the value of the variable manabarbs and set that field in the affs table to true.
So if manabarbs = "^Blabla$", then it means: affs.^Blabla$ = true

More edit:
affs = true OR addaff (manabarbs, true)
Actually what does this mean? You set a variable, and if it does not work then you want to call addaff??
I think you want to replace this with:
affs == true OR addaff("manabarbs", true)
And using a IF construct might be nicer, but that is semantics.

Well, I might be wrong in my assumptions what it is supposed to do, but I hope it helps. And now I will stop editing my own post.
Simimi2007-07-16 14:57:43
After reading this http://lua-users.org/wiki/TablesTutorial

I am even more confused than before...

I now see why the print function does not work, though.

I wanted to have an empty table that afflictions I have could be thrown into. According to the above...
CODE
The table.key = value format is shorthand for table = value when the key

So therefor, affs.paralysis = true should add paralysis to the table, right?

NO NO NO. The above OR means I tried the trigger with both lines independently, but because my table wold not print, I had no way of knowing if it worked since I have no "You have blah affliction" variables.
Unknown2007-07-16 15:11:32
QUOTE(Simimi @ Jul 16 2007, 04:57 PM) 426228
So therefor, affs.paralysis = true should add paralysis to the table, right?


Yes.
affs.paralysis is the same as affs but not the same as affs. Notice the " " !!!

Lua expects a string with those . When you pass it a variable, which contains a string, it will assume that you want to use the string in the variable, and not the name of the variable.



But settings affs.paralysis = true, should work, as should addaffs("paralysis", true), I think.
Simimi2007-07-16 15:16:59
Ok well, I have gotten the print function to work properly, and now that I can print the contents of the pairs in the affs table, I can see that affs.blah = state works just fine to add and remove things from the table.

I'm going to test it with a few other afflictions and see what can be done with it.

Ok so now I can print my affs table to see what afflictions I have. I suppose now I should just start adding in affliction lines...

Thanks for everything, I hope this is the end of this one.

EDIT: This is the alias I am using to print the table...
CODE
affs:
for k,v in pairs(affs) do print(k,v) end

How do I make it only return those values which are "true"?
Unknown2007-07-16 15:27:36
QUOTE(Simimi @ Jul 16 2007, 05:16 PM) 426233
CODE
affs:
for k,v in pairs(affs) do print(k,v) end

How do I make it only return those values which are "true"?


It depends a bit.
You could just delete the items that are in the table, by setting them to nil. The following code will actually remove paralysis from the table:
CODE
affs.paralysis = nil


Or if you do not like that, you could change your print to:
CODE
affs:
for k,v in pairs(affs) do
  if v == true then
    print(k,v)
  end
end


I distributed the code over multiple lines, but for lua newlines do not matter.
Simimi2007-07-16 15:34:39
Which do you think would be safer, considering this is a table to keep track of my currently had afflictions, making it nil when it heals? Or making it false? I have not thought of a way to make it a third setting, so it can be healed, had, or in the process of healing...
Simimi2007-07-16 15:35:59
Which do you think would be safer, considering this is a table to keep track of my currently had afflictions, making it nil when it heals? Or making it false? I have not thought of a way to make it a third setting, so it can be healed, had, or in the process of healing...
Unknown2007-07-16 15:52:22
QUOTE(Simimi @ Jul 16 2007, 05:34 PM) 426237
Which do you think would be safer, considering this is a table to keep track of my currently had afflictions, making it nil when it heals? Or making it false? I have not thought of a way to make it a third setting, so it can be healed, had, or in the process of healing...


Personally, I would make it nil, once you get the message that it is cured, since I cannot think of a reason to keep in memory which afflictions you have had in the past, and are currently healed.
However, that is more a personal preference then anything else. Both routes are more or less the same.
By using false, you will have to do extra checks on other places, and by using nil you lose information about which afflictions you have already healed.

*shrug*