0
votes

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.

1
Possible typo: in lua, functions are defined like this: function() ... end, not like this: function(){ ... } (replace the brackets with an "end" at the end)kikito

1 Answers

1
votes

--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.