开发者

Can not change value of "global" lua table

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.


--When I output the size of #myTable now I receive 1 as result...

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜