dispatch/custom event listener

I have a game where when something happens, it will make dead = true…I know that changing dead to true works, but I need when dead = true for it to go to another function…

I have this but it doesnt work:

[lua]

function updateGameDone()

director:changeScene( “ending”, “moveFromLeft”)

print(“scene changing to ending”)

return true    

end

local event = {

name = “dead”,

dead == true

}

Runtime:dispatchEvent( event )

Runtime:addEventListener(“dead”, updateGameDone)

[/lua]

Thanks in advance

You just have to change the order of your script I think.

First declare the eventListener and AFTER that dispatch the event.

It doesnt work…My “Dead” is not in a table, could that be why? Is there another way to do it to recognize a function and not a table?

dead == true in your table, I’m pretty sure that it should just be dead = true :) 

Why don’t you just call your function directly instead of trying to dispatch an event to it?  You can pass any parameter you like in that case.

I tried constantly checking if the player was dead through a timer.performWithDelay that would go on forever and if the player was dead I called the function, but when I did this it called the function every millisecond after the player was dead and the scene was changing constantly every millisecond 

You don’t need to check it all the time.  When you execute the code where you intend to dispatch the event, just call the function there.

Unless there is some reason why you need an event.

I fixed it… Instead I made another function that constantly looks for when dead == true, then I changed dead to false so that it wouldn’t repeat the function again, and then i called my code to change the scene…Thanks for your help!

You just have to change the order of your script I think.

First declare the eventListener and AFTER that dispatch the event.

It doesnt work…My “Dead” is not in a table, could that be why? Is there another way to do it to recognize a function and not a table?

dead == true in your table, I’m pretty sure that it should just be dead = true :) 

Why don’t you just call your function directly instead of trying to dispatch an event to it?  You can pass any parameter you like in that case.

I tried constantly checking if the player was dead through a timer.performWithDelay that would go on forever and if the player was dead I called the function, but when I did this it called the function every millisecond after the player was dead and the scene was changing constantly every millisecond 

You don’t need to check it all the time.  When you execute the code where you intend to dispatch the event, just call the function there.

Unless there is some reason why you need an event.

I fixed it… Instead I made another function that constantly looks for when dead == true, then I changed dead to false so that it wouldn’t repeat the function again, and then i called my code to change the scene…Thanks for your help!