How to trigger a function when a boolean becomes true?

Hey, 

I know this sound extremely silly, but im still getting a hang of Lua language

So, I know how to trigger an event using the addEventListener, but I know only how to trigger it for touch and orientation… 

What I would like to know is how to call a function or trigger an event when a boolean is being set to true

To be more specific, 

I have a timer, and its changing a boolean to true after 4 seconds

Then in my object I want to animate it (move 100 down) once that boolean is true…

Thanks in advance to all the helpers

Roy.

Hi Roy,

You can’t ‘listen’ to changes in a variable without using a somewhat fancier Lua feature called metatables, which I don’t think is the right way for you to go if you’re still new to Lua.

Instead, you could consider two other options:

  • You mentioned that the boolean is set to true after 4 seconds by a timer.  In that same part of the code, you could just call a function to cause the object to animate

  • Calling a function from the timer to start the animation runs the risk of a’ spaghetti code".  It’s up to you whether that’ll be a problem.  An alternative is, instead of your timer calling an animation function directly, it would instead dispatch a custom event.  Your object would listen to that event and start animating in response.  Here’s a blog post about custom events: http://www.coronalabs.com/blog/2012/06/26/how-to-use-custom-events-in-corona/

Hope this gets you on the right track.

  • Andrew

Andrew,
You helped me a lot!
I’ll spend the day learning costume events and how to use them cause it seems like they are exactly what I need right now
Later on ill try to learn the metatables subject.
Many thanks for the link mate!
Back to studying now :slight_smile:

Roy.

Why not simply call the function when you set the variable?  You’re there, call the function.

@Rob Miracle

Can I? its a local function for an object,

I spawn 10 balls on the top of the screen and then(after 4 seconds), when a boolean is true , I want them all to move 100 pixels down

Please let me know if there is a better way

Roy.

yes it can be done. if you show some code it would be easier to show how to do it

Ok thanks for trying to help

[lua]

– dispatch event to the initial drop

local tmr= timer.performWithDelay(2000,gem:dispatchEvent({name=“initDrop_listener” , state=“on”}),1)

–spawn the gems

for i=0 ,8  do

        local gem = display.newSprite( gemSheet, sequenceData )

        gem.width = 40

        gem.height = 40

        gem.x = 20

        gem.y = 424-40*i

        gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions

        gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions

        gem.MyGemID = board[gem.MyRow][gem.MyCol]

        gem:setSequence( “gem” … gem.MyGemID ) 

        gem:play() 

        gem.xScale=0.5

        gem.yScale=0.5

        gem.y = (-gem.MyRow*gemDimentions)

        – start the initial drop function

        local function initDrop_listener( event )

            if event.state == “on” then

                print(“drop success”) – debug that its firing

                gem:removeEventListener( “initDrop”, initDrop_listener )

            end

        end

        gem:addEventListener( “initDrop”, initDrop_listener )

    end

end

[/lua]

Obviously im doing something wrong :slight_smile:

Roy.

first line 2 name should be “initDrop”

second cause of the way your creating the balls this will only affect the last one created cause your over writing each one as the new one is created

try this

local gems = {} local function createBall(i)         local gem = display.newSprite( gemSheet, sequenceData )         gem.width = 40         gem.height = 40         gem.x = 20         gem.y = 424-40\*i         gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions         gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions         gem.MyGemID = board[gem.MyRow][gem.MyCol]         gem:setSequence( "gem" .. gem.MyGemID )          gem:play()          gem.xScale=0.5         gem.yScale=0.5         gem.y = (-gem.MyRow\*gemDimentions)         local function initDrop(  )                 print("drop success") -- debug that its firing         end         gem.tmr= timer.performWithDelay(2000, initDrop, 1)         return gem     end for i = 1, 8 do       gems[i] = createBall(i) end  

do away with the eventListener

But doesnt that mean that each gem has its own timer? cause the line

[lua]gem.tmr= timer.performWithDelay(2000, initDrop, 1)[/lua]

Is inside the spawned gems… I want the timer to be outside, is it possible?

Roy.

try this

local gems = {}     local function createBall(i)                  local gem = display.newSprite( gemSheet, sequenceData )                  gem.width = 40                  gem.height = 40                  gem.x = 20                  gem.y = 424-40\*i                  gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions                  gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions                  gem.MyGemID = board[gem.MyRow][gem.MyCol]                  gem:setSequence( "gem" .. gem.MyGemID )                   gem:play()                   gem.xScale=0.5                  gem.yScale=0.5                  gem.y = (-gem.MyRow\*gemDimentions)             local function initDrop( event )                     print("drop success") -- debug that its firing             end                  return gem              end      local function drop()   for i = 1,8 do     gems[i]:initDrop()   end end     for i = 1, 8 do           gems[i] = createBall(i)     end tmr = timer.performWithDelay( 2000, drop, 1 )

sorry had to edit should’ve tested first

First I want to thank you for your patient with helping me!

 

I get an error when I try this,

The error is - Attempt to index global ‘gem’ (a nil value)

And its referenced to the ‘addEventListener’ inside the loop…

Ive attached the project file, do you mind take a look at it?

The relevant rows are 64-95

 

Roy.

made edits to code above

will look at file but will be a while its 3am here gotta go bed

I think I got it to work

Many thank @jstrahan , good night :wink:

Roy.

great

nite

Hi Roy,

You can’t ‘listen’ to changes in a variable without using a somewhat fancier Lua feature called metatables, which I don’t think is the right way for you to go if you’re still new to Lua.

Instead, you could consider two other options:

  • You mentioned that the boolean is set to true after 4 seconds by a timer.  In that same part of the code, you could just call a function to cause the object to animate

  • Calling a function from the timer to start the animation runs the risk of a’ spaghetti code".  It’s up to you whether that’ll be a problem.  An alternative is, instead of your timer calling an animation function directly, it would instead dispatch a custom event.  Your object would listen to that event and start animating in response.  Here’s a blog post about custom events: http://www.coronalabs.com/blog/2012/06/26/how-to-use-custom-events-in-corona/

Hope this gets you on the right track.

  • Andrew

Andrew,
You helped me a lot!
I’ll spend the day learning costume events and how to use them cause it seems like they are exactly what I need right now
Later on ill try to learn the metatables subject.
Many thanks for the link mate!
Back to studying now :slight_smile:

Roy.

Why not simply call the function when you set the variable?  You’re there, call the function.