代码之家  ›  专栏  ›  技术社区  ›  localhorst

索引处的铁路超高访问表条目

  •  0
  • localhorst  · 技术社区  · 1 年前

    我的 local players 表使用PlayerServerId作为索引。

    当我现在想访问索引1(我的PlayerServerId)中的玩家数据时,我得到 nil 因此

    但是,当我现在遍历players表并打印出迭代的索引时,我会得到索引1(就像我所期望的那样)

    调试代码:

    print("PlayerServerId: ".. GetPlayerServerId(PlayerId())) -- --> Output: "PlayerServerId: 1"
    
    print(players[1]) -- --> Output "nil"
    
    for i, player in pairs(players) do
      print("Index: " .. i) -- --> Output: "Index: 1"
    end
    

    输出 image of output

    这怎么可能?我做错了什么,或者我该监督什么?

    我试着调试每一个步骤,以确保我想要访问的数据可用。

    1 回复  |  直到 1 年前
        1
  •  0
  •   Luatic    1 年前

    i 可能是绳子 "1" .

    1 ~= "1" 因为不同类型的事物在Lua中永远不平等。所以 t[1] 可以是 nil 虽然 t["1"] 可以是其他东西。

    使用迭代时 pairs ,还可以迭代字符串键。 print("Index: " .. i) 然后打印 Index: 1 如果 是字符串 1. 。如果 是数字 1 .

    实际上,您的 print 调试是有缺陷的,因为它执行数字到字符串的强制。为了正确调试,您还需要告诉类型的东西,例如通过引用字符串。

    我建议使用以下内容 inspect.lua 或者写你自己的同类文章。目前,打印 type(i) 也应该告诉你你需要知道的一切。