## love.physics.newBody() error

General discussion about LÖVE, Lua, game development, puns, and unicorns.
intensity
Prole
Posts: 8
Joined: Wed Sep 29, 2010 8:26 pm

### love.physics.newBody() error

What's wrong with this line? The compiler says "attempt to index field '?' (a nil value)".

blocks[a] = love.physics.newBody(world, 50 + (20 * a), 50 + (20 * b), 0, 0)
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

### Re: love.physics.newBody() error

intensity wrote:blocks[a] = love.physics.newBody(world, 50 + (20 * a), 50 + (20 * b), 0, 0)

It could be that you've created the blocks table but not the blocks[a] tables. Example:

Code: Select all

blocks = {} -- create the first array dimension
for a = 1, 10 do
blocks[ a ] = {} -- create the second array dimension for each a
for b = 1, 10 do
blocks[ a ][ b ] = love.physics.newBody(world, 50 + (20 * a), 50 + (20 * b), 0, 0)
end
end
intensity
Prole
Posts: 8
Joined: Wed Sep 29, 2010 8:26 pm

### Re: love.physics.newBody() error

That's probably it. blocks[1][1] would screw up if blocks[1] had no value?
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

### Re: love.physics.newBody() error

intensity wrote:blocks[1][1] would screw up if blocks[1] had no value?
Yes. Lua doesn't have multidimensional arrays, just arrays of arrays, i.e.

Code: Select all

blocks = {} -- blocks can be used as an array but blocks[1] has no value
blocks[1][1] = 1 -- errors, blocks[1] is nil
blocks[1] = {} -- now blocks[1] can also be used as an array
blocks[1][1] = 1 -- works
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: love.physics.newBody() error

intensity wrote:What's wrong with this line? The compiler says "attempt to index field '?' (a nil value)".

blocks[a] = love.physics.newBody(world, 50 + (20 * a), 50 + (20 * b), 0, 0)
Not a compiler. Interpreter.
HUGE DIFFERENCE.
Hello, I am not dead.
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

### Re: love.physics.newBody() error

zac352 wrote:Not a compiler. Interpreter.
Actually Lua is both, the compiler translates source code into bytecode and the VM interprets the bytecode to carry on the operations defined in the source code.
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: love.physics.newBody() error

zac352 wrote:Not a compiler. Interpreter.
Actually Lua is both, the compiler translates source code into bytecode and the VM interprets the bytecode to carry on the operations defined in the source code.
That's an interpreter. Note: I know that lua has a compilable file for making a lua compiler. so yeah.
Hello, I am not dead.