drarem Posted May 11, 2016 Share Posted May 11, 2016 After I've initialized the array using a function, I can do this: globals.myarray[1][1] = 0 and it works, but if I do this: local fx=globals.reti local fy=globals.retj System:Print(...) .. fx, fy are now equal to 1,1 This won't work: globals.myarray[fx][fy] = 0 I get this error: 253 : attempt to index a nil value What is going on? thanks Quote Link to comment Share on other sites More sharing options...
shadmar Posted May 11, 2016 Share Posted May 11, 2016 This will create a multi-dimensional array of 10 x 10 with every value set to zero: myarray = {} for f=1, 10 do myarray[f] = {} for g=1, 10 do myarray[f][g] = 0 end end 1 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
drarem Posted May 11, 2016 Author Share Posted May 11, 2016 It's odd, that's what I have but when I access it in another function (after it has been initialized and called), then crashes with a 'nil' like it has gone out of scope. BUT if I hardcode the numbers, it works. myarray[fx][fy] = 0 -- nil error myarray[3][1] = 0 -- works Quote Link to comment Share on other sites More sharing options...
macklebee Posted May 11, 2016 Share Posted May 11, 2016 We will probably need to see all of the code that initializes the array and then accesses it in another function to determine the problem. But if I had to guess is that you are setting the 'fx' & 'fy' variables local to a function other than the function you are accessing the array with so those are not defined in the access function. Again without seeing your full code example, its hard to troubleshoot. Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Genebris Posted May 12, 2016 Share Posted May 12, 2016 fx or fy is a nil value. Make sure you set them to numbers you need. Quote Link to comment Share on other sites More sharing options...
drarem Posted May 12, 2016 Author Share Posted May 12, 2016 Many thanks, after thinking about the 'fx and fy' variables and some testing, I realized those were a string value instead of numeric. I converted them to a number using tonumber(fx) and tonumber(fy), and it works as expected. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.