Example broken, or do I fail to grasp Lua scoping?

I’m trying to learn the Corona SDK by studying examples. In this example the collision handler (objectCollide()) contains a local function to be passed to a timer. That local function code starts off like this:

local function onDelay( event ) local action="" if( action == "makeJoint" ) then

I kinda got stuck right there. action is set to the empty string and then immediately compared to a non-empty string. Would the comparison not always fail?  I would assume it’s just non-working code, but being new to Lua makes me want to double-check that assumption.

Hi @ronburk,

Yes, in this case ‘action’ is a local variable inside the scope of the local function ‘onDelay’, so it’s not visible anywhere outside or before the ‘local action=""’ line. And yes, where you check if ‘action’ == ‘makeJoint’, that will always fail (unless of course, you state ‘local action = “makeJoint”’ on the line before :slight_smile: ).

Best regards,

Brent

Thanks!

Hi @ronburk,

Yes, in this case ‘action’ is a local variable inside the scope of the local function ‘onDelay’, so it’s not visible anywhere outside or before the ‘local action=""’ line. And yes, where you check if ‘action’ == ‘makeJoint’, that will always fail (unless of course, you state ‘local action = “makeJoint”’ on the line before :slight_smile: ).

Best regards,

Brent

Thanks!