2

I'm trying to build a script for a MUD I play that will create a table to keep track of average xp for each mob. I'm having trouble with the syntax of checking whether an element in a table exists and if not creating it. I tried something like this but keep getting: attempt to index field '?' (a nil value)

mobz_buried = {
{mob = "troll", quantity = 2}
{mob = "warrior", quantity = 1}
{mob = "wizard", quantity = 1}} -- sample data

number_of_mobz_buried = 4 

xp_from_bury = 2000 -- another script generates these values, these are all just examples

xp_per_corpse = xp_from_bury / number_of_mobz_buried

for _, v in ipairs(mobz_buried) do
    if type(mobz[v].kc) == "variable" then -- kc for 'kill count', number of times killed 
            mobz[v].kc = mobz[v].kc + 1 -- if it exists increment kc
    else
        mobz[v].kc = 1 -- if it doesn't exist create a key value that matches the mobs name and make the kc 1
    end
    if type(mobz[v].xp) == "variable" then -- xp for average experience points
        mobz[v].xp = (((mobz[v].kc - 1) * mobz[v].xp + xp_per_corpse)/mobz[v].kc) -- just my formula to find the average xp over a range of differant buries
    else
            mobz[v].xp = xp_per_corpse -- if it doesn't exist create the table just like before
    end
end

I'm trying to end up with mobz.troll = {kc, xp}, mobz.warrior = {kc, xp}, mobz.wizard = {kc, xp} and the ability to add more key values based off of the names mobz_buried gives me.

Eli Bell
  • 227
  • 1
  • 9
  • Provide a simple example of the table `mobz_buried` and `mobz`. – Yu Hao Jan 08 '15 at 02:36
  • 1
    See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Your current example isn't complete. – Yu Hao Jan 08 '15 at 02:52
  • Your `mobz_buried` table has a value that doesn't have a corresponding element in `mobz` so when you try to use `mobz[v]` and then index into that value there isn't a value and you get this error. See https://eval.in/private/88f083da483307 – Etan Reisner Jan 08 '15 at 02:54
  • Okay, I edited my question so the example and objective should be much more clear now! – Eli Bell Jan 08 '15 at 03:38
  • Please post the full stacktrace error and include the surrounding code of the line it refers to. – greatwolf Jan 08 '15 at 03:53
  • Immediate execution [string "Immediate"]:13: attempt to index field '?' (a nil value) stack traceback: [string "Immediate"]:13: in main chunk – Eli Bell Jan 08 '15 at 03:58
  • And where is line 13 in your code posted above? – greatwolf Jan 08 '15 at 04:02
  • if type(mobz[v].kc) == "variable" then -- kc for 'kill count' – Eli Bell Jan 08 '15 at 04:05
  • I tried `if type(mobz[mobz_buried[_].mob].kc) == "variable" then` that didn't work either – Eli Bell Jan 08 '15 at 04:06

1 Answers1

0

Based on extra info from your comments, it sounds like you didn't construct a table for mobz. Try this:

local mobz = {}
for _, v in ipairs(mobz_buried) do
    mobz[v.mob] = mobz[v.mob] or {}
    mobz[v.mob].kc = (mobz[v.mob].kc or 0) + 1

    -- etc...
end
greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • I'm trying to understand this. Line 3 says: 'if this element exists than leave it as is, if not create it as a blank element?' – Eli Bell Jan 08 '15 at 04:31
  • @EliBell I don't see that comment in your posted code anywhere. It sounds more like, try to increment element by one or if it doesn't exist create and initialize it to one. – greatwolf Jan 08 '15 at 04:35
  • Thanks for your patience, took me a while to understand. But I see now the use of 'or' in assigning table values was what I was missing – Eli Bell Jan 09 '15 at 01:32