[SOLVED] Table referenced between scripts is "nil". Why?
Posted: Thu Jun 04, 2020 4:40 pm
Hey guys,
Been all over Google and Stack and the forums and I'm not sure why this is happening. Could use a pointer in the right direction.
So I have two scripts, one has a table with data in it, the other has a function that uses that table's data.
Now, regardless of how I reference the table in the other script whether it be directly (x = require X to x.table or require x to tablename etc) or if I reference the table to a local table (tableA = tableB etc) it always comes up NIL and errors out with:
"attempt to get length of value gameGrid" or directly referenced it says "attempt to index field floorplan (a nil value)"
Now if I use the table in a function in the script in which the table is originally written it works 100%. The problem is I need the table split from the referencing script so I can swap tables dynamically.
Everything else works, all references to all other variables, all references to all other functions, etc. So I know the "requires" parameter is working and I know that the rest of my code is working, but for some reason the data from the table script is not being pushed to memory OR there's a reference being dropped somewhere from one script to the other. Any help?
Since people will ask for it here is some code:
Here is the coded script with the table. Very basic.
Game code that looks at the table and is supposed to render something with it.
Note: I've stripped out all the code you don't need to see and "GAME" is under my "MAIN" Lua code script that runs most everything else (I build modularly)
Been all over Google and Stack and the forums and I'm not sure why this is happening. Could use a pointer in the right direction.
So I have two scripts, one has a table with data in it, the other has a function that uses that table's data.
Now, regardless of how I reference the table in the other script whether it be directly (x = require X to x.table or require x to tablename etc) or if I reference the table to a local table (tableA = tableB etc) it always comes up NIL and errors out with:
"attempt to get length of value gameGrid" or directly referenced it says "attempt to index field floorplan (a nil value)"
Now if I use the table in a function in the script in which the table is originally written it works 100%. The problem is I need the table split from the referencing script so I can swap tables dynamically.
Everything else works, all references to all other variables, all references to all other functions, etc. So I know the "requires" parameter is working and I know that the rest of my code is working, but for some reason the data from the table script is not being pushed to memory OR there's a reference being dropped somewhere from one script to the other. Any help?
Since people will ask for it here is some code:
Here is the coded script with the table. Very basic.
Code: Select all
local home = {}
local floorplan = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0}, --Center
{0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
}
return home
Code: Select all
local game = {}
local home = require "game/home"
local gameGrid = home.flooplan
local tileQ = lg.newQuad(0,0,32,21)
function game:drawGame()
for y=1, #gameGrid do
for x=1, #gameGrid[y] do
if gameGrid[y][x] == 1 then
if (#gameGrid[y] % 2 == 0) then
lg.draw(gameTiles,tileQ,(x*32)-32,(y*8))
else
lg.draw(gameTiles,tileQ,(x*32)-48,(y*8))
end
end
end
end
end
return game