[SOLVED] Table referenced between scripts is "nil". Why?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
4KbShort
Prole
Posts: 15
Joined: Thu Jun 04, 2020 4:21 pm

[SOLVED] Table referenced between scripts is "nil". Why?

Post by 4KbShort »

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.

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
Game code that looks at the table and is supposed to render something with it.

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
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)
Last edited by 4KbShort on Sun Jul 05, 2020 3:38 am, edited 1 time in total.
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: Table referenced between scripts is "nil". Why?

Post by hoistbypetard »

floorplan is local and not returned anywhere, so it's not accessible outside the scope where it's declared.

You need to add

Code: Select all

home.floorplan = floorplan
before you return the home table.
User avatar
ThatBonk
Prole
Posts: 11
Joined: Thu May 23, 2019 6:11 pm

Re: Table referenced between scripts is "nil". Why?

Post by ThatBonk »

hoistbypetard wrote: Thu Jun 04, 2020 8:27 pm floorplan is local and not returned anywhere, so it's not accessible outside the scope where it's declared.

You need to add

Code: Select all

home.floorplan = floorplan
before you return the home table.
And if he did it somewhere in case, there is also a syntax error. There is an r missing in 'home.flooplan' on line 4.
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: Table referenced between scripts is "nil". Why?

Post by hoistbypetard »

Good eye. I never spot things like that in code I can't run.
4KbShort
Prole
Posts: 15
Joined: Thu Jun 04, 2020 4:21 pm

Re: Table referenced between scripts is "nil". Why?

Post by 4KbShort »

ThatBonk wrote: Mon Jun 08, 2020 3:06 am
hoistbypetard wrote: Thu Jun 04, 2020 8:27 pm floorplan is local and not returned anywhere, so it's not accessible outside the scope where it's declared.

You need to add

Code: Select all

home.floorplan = floorplan
before you return the home table.
And if he did it somewhere in case, there is also a syntax error. There is an r missing in 'home.flooplan' on line 4.
Thanks guys! The typo is only in the forum post as I retyped it by hand, but nice eyes.
Also thanks for the feedback. Shortly after I posted this and before it was approved I found a workaround by using a function to return the floorplan externally. Not certain if my way is now so integrated I can change it (probably not), but I like @hoistbypetard 's single line solution better so I'll give it a shot.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests