2014-11-06 61 views
2

我得到一个非常奇怪的索引一个零值错误,我无法弄清楚如何解决我的生活。这里是代码:Lua尝试索引? (一个零值)

local COLONYNUMBER = players[0].getColony() 
print(COLONYNUMBER) <--- prints 0 
print(colonies[0].getName()) <---- prints New Brussels 
print(colonies[COLONYNUMBER].getName()) <---- ERROR HERE 
+0

在COLONYNUMBER中它是数字“0”还是字符串“0”? – 2014-11-06 21:19:08

回答

3

在黑暗中拍摄,但播放器[0] .getColony()返回字符串'0'?因为它会在lua解释器中打印为0,但肯定不会将索引表作为零。我在下面谈论的例子:

local t = '0' 
print(t) 
-- below prints exactly the same as variable t above 
local u = 0 
print(u) 

local temp = { [0] = true } 
-- try to index into the temp table with '0' 
print(temp[t]) -- undefined 
+1

我添加了tonumber(COLONYNUMBER)它,它工作完美,谢谢 – user3753698 2014-11-06 21:30:06

相关问题