我正在嘗試爲MUD創建一個腳本,該腳本將創建一張表以跟蹤每個暴民的平均xp。我在檢查表中元素是否存在以及是否創建它的語法方面遇到了問題。我想是這樣的,但不斷收到:attempt to index field '?' (a nil value)
在Lua中構建鍵/值表
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
讓我的多個關鍵值的能力來結束。
提供一個簡單的例子,表mobz_buried和mobz。 –
請參見[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。你目前的例子不完整。 –
你的'mobz_buried'表有一個值在'mobz'中沒有對應的元素,所以當你嘗試使用mobz [v]'然後索引到那個值時就沒有值,你會得到這個錯誤。請參閱https://eval.in/private/88f083da483307 –