Page 1 of 1

love.physics.newBody() error

Posted: Mon Oct 04, 2010 1:27 am
by intensity
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)

Re: love.physics.newBody() error

Posted: Mon Oct 04, 2010 1:39 am
by leiradel
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

Re: love.physics.newBody() error

Posted: Mon Oct 04, 2010 7:12 pm
by intensity
That's probably it. blocks[1][1] would screw up if blocks[1] had no value?

Re: love.physics.newBody() error

Posted: Mon Oct 04, 2010 7:23 pm
by leiradel
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

Re: love.physics.newBody() error

Posted: Tue Oct 05, 2010 9:53 am
by zac352
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.
:|

Re: love.physics.newBody() error

Posted: Tue Oct 05, 2010 12:43 pm
by leiradel
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.

Re: love.physics.newBody() error

Posted: Tue Oct 05, 2010 1:24 pm
by zac352
leiradel wrote:
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. :P

Re: love.physics.newBody() error

Posted: Tue Oct 05, 2010 1:42 pm
by leiradel
zac352 wrote:That's an interpreter.
Yeah, that's debatable. I was just pointing that Lua actually compiles things.