love.physics.newBody() error

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

love.physics.newBody() error

Post 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)
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: love.physics.newBody() error

Post 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
intensity
Prole
Posts: 8
Joined: Wed Sep 29, 2010 8:26 pm

Re: love.physics.newBody() error

Post by intensity »

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

Re: love.physics.newBody() error

Post 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
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: love.physics.newBody() error

Post 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.
:|
Hello, I am not dead.
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: love.physics.newBody() error

Post 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.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: love.physics.newBody() error

Post 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
Hello, I am not dead.
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: love.physics.newBody() error

Post by leiradel »

zac352 wrote:That's an interpreter.
Yeah, that's debatable. I was just pointing that Lua actually compiles things.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 12 guests