无法更改“global”的值lua表
我有一个 .lua 脚本文件,但遇到了类似的问题:
myTable = {}
function changeMyTable(index,value){
myTable[index] = value
--When I output the size of #myTable now I receive 1 as result...
}
function checkMyTableSize(){
--when i output #myTable here. I receive 0 as result
}
有人知道如何在全局“myTable”表上创建索引吗?
我也尝试过使用 table.insert(myTable,index,value) 。
I have a .lua script file and i'm having a problem in something like that:
myTable = {}
function changeMyTable(index,value){
myTable[index] = value
--When I output the size of #myTable now I receive 1 as result...
}
function checkMyTableSize(){
--when i output #myTable here. I receive 0 as result
}
Anyone knows how to create a index on the global 'myTable' table?
I've tryed to use table.insert(myTable,index,value) too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
index
值恰好为 1,则仅会发生这种情况。否则不会发生。 # 运算符仅计算表中数组值的数量,并且计算到第一个 NIL。所以它检查表[1],然后表[2],然后......直到达到NIL。它返回了这一点。请注意,这是对其背后概念的解释。实现可能不会像那样循环。
无论如何,您的不一致可能是由于多次运行脚本而不是多次调用全局函数造成的。
This should only happen if the
index
value was exactly 1. Otherwise it doesn't. The # operator only counts the number of values in the table that are array values, and it counts to the first NIL. So it checks table[1], then table[2], then... until it reaches NIL. And it returns that.Note that this is an explanation of the concept behind it. The implementation probably doesn't loop like that.
In any case, your inconsistency may be due to running the script multiple times instead of calling the global functions multiple times.