Page 1 of 1

"Attempt to index a nil value"

Posted: Sat Jul 15, 2017 4:55 am
by Gabriel Bras
Can you guys help me?

I don't know how to solve this.

Here's the code

Code: Select all

function love.load( )
	love.graphics.setBackgroundColor(255, 255, 255, 255)

	gridX = 10
	gridY = 18

	inert = {}

	--TEMPORÁRIO
	inert[18][1] = 'A'
	inert[17][2] = 'B'
	inert[16][3] = 'C'
	inert[15][4] = 'D'
	inert[14][5] = 'E'
	inert[13][6] = 'F'
	inert[12][7] = 'G'

	for i=1, gridY do
		inert[i] = {}
		for j=1,gridX do
			inert[i][j] = 'O'
		end
	end
end

Re: "Attempt to index a nil value"

Posted: Sat Jul 15, 2017 5:43 am
by Jasoco
You aren't defining each inert row as a table itself. Basically with that first line in the list you're trying to access item [1] from table [18] to set it to "A" but you can't because you never defined inert[18] as a table first.

You need to do more research into how to create 2+ dimensional tables in Lua so you understand how they work better.

Basically find a better way to store the "TEMPORARIO" stuff. The way you're doing it is wrong. (Well not really wrong. Just not right. See below...)

Try this. Upon inspecting the code better, I bet it would work exactly as you want it to if you move that entire list of stuff under "TEMPORARIO" down to the bottom AFTER the grid code.

Basically try this:

Code: Select all

function love.load( )
	love.graphics.setBackgroundColor(255, 255, 255, 255)

	gridX = 10
	gridY = 18

	inert = {}

	for i=1, gridY do
		inert[i] = {}
		for x=1,gridX do
			inert[i][j] = 'O'
		end
	end
	
	--TEMPORÁRIO
	inert[18][1] = 'A'
	inert[17][2] = 'B'
	inert[16][3] = 'C'
	inert[15][4] = 'D'
	inert[14][5] = 'E'
	inert[13][6] = 'F'
	inert[12][7] = 'G'
end
Also note that you put for i=1, gridY do which is correct, but then you do for x=1,gridX do which is incorrect. The x=1 should be j=1 if you're trying to use i and j. (Though I would always use y and x instead personally.)

I bet that'll work.

Re: "Attempt to index a nil value"

Posted: Sat Jul 15, 2017 1:31 pm
by Gabriel Bras
:awesome:
Oh, My! It was just a stupid mistake. Thanks.
o/