## "Attempt to index a nil value"

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Gabriel Bras
Prole
Posts: 3
Joined: Sat Jul 15, 2017 4:44 am

### "Attempt to index a nil value"

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
Last edited by Gabriel Bras on Sat Jul 15, 2017 1:27 pm, edited 1 time in total.

Jasoco
Inner party member
Posts: 3642
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: "Attempt to index a nil value"

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.

Gabriel Bras
Prole
Posts: 3
Joined: Sat Jul 15, 2017 4:44 am

### Re: "Attempt to index a nil value"

Oh, My! It was just a stupid mistake. Thanks.
o/

### Who is online

Users browsing this forum: No registered users and 7 guests