我不知道如何处理nils我的sort函数获取.
当我检查它时,table.sort在一些电话后崩溃.
if a == nil then
    return false
elseif b == nil then
    return true
end
出现此错误:无效的排序功能.但根据文档,sort函数应该返回false,如果a在b之后.否则是真的.
如果我删除删除该代码,它当然会崩溃索引nils.
我很快就调试了一些东西,并编写了以下函数:
function dumpTable(t)
    for i,v in pairs(t) do
        if type(v) == "table" then
            dumpTable(v)
        else
            print(i..":", v)
        end
    end
end
现在,出于某种原因
dumpTable({[1]="hello??", [2]="two", {[132]="something", [3.2]="else"}})
输出
132:    something
3.2:    else
2:  two
注意第一个字符串是如何丢失的?但如果我改变它的钥匙..
dumpTable({["one"]="hello??", [2]="two", {[132]="something", [3.2]="else"}})
它输出
132:    something
3.2:    else
one:    hello??
2:  two
这是如此不直观,我几乎觉得自己是个白痴,没有看到错误..
(顺便说一句.我知道如果表包含递归引用,我的函数将溢出堆栈,稍后将修复它)
我在为游戏编写代码时遇到了一个问题.似乎我不能在语句中使用变量;
local Username = "Cranavvo"
game.Players.Username:BreakJoints() -- Kills the player
输出告诉我"没有这样的用户''用户名'"应该是"Cranavvo".
如果我有一张颜色表:
colour["red"] = 1
colour["blue"] = 4
colour["purple"] = 5
并且我想将红色添加到蓝色,我可以很容易地得到红色和蓝色的数值,但是然后使用值5,我可以让它返回"紫色"而不扫描整个表吗?
我有下一个结构
self.modules = {
    ["Announcements"] = {
        priority = 0,
        -- Tons of other attributes
    },
    ["Healthbar"] = {
        priority = 40,
        -- Tons of other attributes
    },
    ["Powerbar"] = {
        priority = 35,
        -- Tons of other attributes
    },
}
我需要通过priorty DESC对此表进行排序,其他值无关紧要.例如Healthbar,然后是Powerbar,然后去其他所有人.
//编辑
必须保留密钥.
//编辑#2
找到了解决方案,谢谢大家.
local function pairsByPriority(t)
    local registry = {}
    for k, v in pairs(t) do
        tinsert(registry, {k, v.priority})
    end
    tsort(registry, function(a, b) return a[2] > b[2] end)
    local i = 0
    local iter = function() …Lua有#运算符来计算用作数组的表的"长度".在诸如C之类的语言中,在计算出某事物的长度之后,通常不再计算它.例如int len = strlen(string);
这在Lua中有什么不同吗?一个效率低于另一个吗?
(显然,对于相当小的表格,这可能不会显示出明显的差异,但知道这一点并不坏.)
我将用C语言实现一个函数,它将由Lua脚本调用.
这个函数应该接收一个lua表作为参数,所以我应该读取表中的字段.我尝试如下所示,但是当我运行它时我的函数崩溃了.任何人都可以帮助我找到问题吗?
/*
 function findImage(options)
    imagePath = options.imagePath
    fuzzy = options.fuzzy
    ignoreColor = options.ignoreColor;
 end
 Call Example:
  findImage {imagePath="/var/image.png", fuzzy=0.5, ignoreColor=0xffffff}
 */
// implement the function by C language
static int findImgProxy(lua_State *L)
{
    luaL_checktype(L, 1, LUA_TTABLE);
    lua_getfield(L, -1, "imagePath");
    if (!lua_isstring(L, -1)) {
        error();
    }
    const char * imagePath = lua_tostring(L, -2);
    lua_pop(L, 1);
    lua_getfield(L, -1, "fuzzy");
    if (!lua_isnumber(L, -1)) {
        error();
    }
    float fuzzy = lua_tonumber(L, -2);
    lua_getfield(L, -1, "ignoreColor");
    if (!lua_isnumber(L, -2)) {
        error();
    }
    float ignoreColor …Run Code Online (Sandbox Code Playgroud) 假设以下lua代码:
local FooTable={ ["FooKey"]="FooValue" }
索引"FooValue"是"FooKey".所以我可以像这样访问它而没有任何问题(假设FooTable位于堆栈顶部.):
lua_getfield(L, -1, "FooKey");
当我尝试这样的事情时:
local FooTable={ "FooValue" }
我会假设索引"FooValue"是"1".但以下给了我一个nil回报.
lua_getfield(L, -1, "1");
是否有一种特殊的方法来访问表中的数字键?
我正在学习Lua中的代码并遇到了我的代码问题,这是我第一次使用表,每当我在另一个表中调用一个特定的表时,我的类型为nil,打印表不显示表: xxxxx,因为它通常会(它只是打印一个空白),我假设这意味着我在定义表时做错了什么?
码:
local craft = { sword = { "cobble", stick = {} } }
print(type(craft.sword[1]))
print(craft.sword[1])
print(type(craft.sword[2]))
print(craft.sword[2])
print(craft)
(这是表的简化版本,用于测试此错误,但仍会在此处出现)
输出:
string
cobble
nil
table: 8a3b983
我有一张桌子:
Table = {
    button = {},
    window = {},
    label = {},
    edit = {},
    error = {}
}
如何获取表的键和值?
我试图得到:
for key, value in ipairs(Table) do
    for k, v in ipairs(key) do
       print(k, v)
    end
end
但这不起作用。