Bizzare“尝试调用表值"在卢阿 [英] Bizzare "attempt to call a table value" in Lua

查看:88
本文介绍了Bizzare“尝试调用表值"在卢阿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段:

for weight, item in itemlist do
    weight_total=weight_total+weight
end

在该代码段的第一行中导致错误试图调用表值".为什么?

is causing the error "attempt to call table value" on the first line in that snippet. Why?

Itemlist是重量表和字符串表的表,例如:

Itemlist is a table of tables of weights and strings, like such:

local itemlist = {
                        {4,"weapon_pistol"},
                        {2,"weapon_357"},
                        ...

据我所知,什么也没有被呼唤.为什么会出现此错误?

Nothing is being called as far as I can tell; why is this error coming up?

推荐答案

通用for需要3个参数:一个可调用的值,一些重复传递给它的值以及迭代开始的键.
如果无法调用,则股票lua不会在传递给的第一个值上调用对,尽管有些派生函数会这样做.

The generic for expects 3 arguments: a callable value, some value which is repeatedly passed to it, and the key where the iteration shall start.
Stock lua does not call pairs on the first value passed to for if that's not callable, though some derivatives do.

因此,您必须使用ipairs(itemlist)pairs(itemlist)next, itemlist或任何您想要的名称(最后两个具有相同的行为,并且大多数派生子都这样做).

Thus, you must use ipairs(itemlist), pairs(itemlist), next, itemlist or whatever you want (the last two have identical behavior, and are what most derivatives do).

例如,一个迭代器将值序列拆包:

As an example, an iterator unpacking the value sequence:

function awesome_next(t, k)
    k, t = next(t, k)
    if not t then return end
    return k, table.unpack(t)
end

for k, a, b, c, d in awesome_next, t do
end

这篇关于Bizzare“尝试调用表值"在卢阿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆