lua - Trying to access table value within the table -


i'm trying this

local ball = {         width = 20,         height = 20,          position = {             x = (game.width / 2) - (width / 2), -- place ball @ center of screen             y = (game.height / 2) - (height / 2)         },          velocity = 200,         color = { 255, 255, 255 }     } 

but love2d me attempt perform arithmetic on global 'width' (a nil value). how can fix it?
tried replace width / 2 ball.width / 2 got attempt index global 'ball' (a nil value).

remember local some_name = expression equivalent to:

local some_name some_name = expression 

this allows some_name appear in expression. in particular, allows recursion local functions. however, until expression finished being evaluated, value of some_name still nil.

so within table initialization, ball nil value. there no way access members of table while table being initialized. can afterwards:

local ball = {     width = 20,     height = 20,       velocity = 200,     color = { 255, 255, 255 } }  ball.position = {     x = (game.width / 2) - (ball.width / 2), -- place ball @ center of screen     y = (game.height / 2) - (ball.height / 2) } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -