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

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

问题描述

以下代码片段:

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 不会在传递给 for 的第一个值上调用对,如果它不可调用,尽管有些衍生工具会这样做.

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

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

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