2

我正在尝试为我玩的 MUD 构建一个脚本,该脚本将创建一个表格来跟踪每个暴徒的平均 xp。我在检查表中的元素是否存在以及是否创建它时遇到了问题。我尝试过这样的事情,但不断得到: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

我正在尝试结束,mobz.troll = {kc, xp}, mobz.warrior = {kc, xp}, mobz.wizard = {kc, xp}并且能够根据名称添加更多键值mobz_buried给了我。

4

1 回答 1

0

根据您评论中的额外信息,听起来您没有为mobz. 试试这个:

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
于 2015-01-08T04:14:32.003 回答