help with function

I have seen numerous times that functions, timers and transitions are used within functions. but I want to know if I’m doing things right

local function showObject( event )   local alphaTmr   if alphaTmr ~= nil then     timer.cancel( alphaTmr )     alphaTmr = nil   end   local function objectAlpa()     object.alpha = 1   end   --------------------   if (someValue == 0) then     --do something   elseif (someValue == 1) then --do something   else     --blink object when number is equal to 2     if( someValue == 2 ) then       alphaTmr = timer.performWithDelay(1050, function()         transition.blink( object, { time=1000, onStart=objectAlpa } ) end )     end   end   ---------------------   transition.fadeIn( object, { time=1000 } ) end

I’m canceling all the transitions in the scene hide.

Function purpose: I have a display oibject created with alpha = 0. The function is to change its fill image with some values and if the value is equal to 2 make it blink. Every time I enter the scene the object fades in.

Thanks in advance

DoDI

Just commenting on the timers and transitions.

When you declare “local alphaTmr” it is nil until you assign a value to it. On the very next row, you perform a check to see if it is not nil, but with your current code alphaTmr will always be nil. If you declare alphaTmr outside of the function, then the function can still read it as long as it is within the function’s scope and then alphaTmr may not be nil when you load your function.

When you are working with timers and transitions, the reason why you may want to assign them to variables, like your alphaTmr, is that it allows you to easily control them after you’ve started them. With transitions, you have other means of pausing or cancelling them, like the transition.cancel() that you do at scene hide, but with timers, you won’t be able to control them anymore if you create a timer locally inside a function as you’d need an object to reference to.

local function showObject( event ) local alphaTmr ... end

Because of the way scope works, the variable “alphaTmr” is only visible to the function showObject(). If you try to cancel it in scene:hide(), you won’t be able to access that timer.

Rob

Will there be any way to use and cancel that timer within the same function?

Thanks in advance
DoDi

Well, I did say how to do it already. :stuck_out_tongue:

Simply declare it before the function, i.e.
 

local alphaTmr local function showObject( event ) ... end

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

@XeduR @Spyric

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

It really helps if you take the time and develop a solid understanding of “scope”. We have a very good tutorial that helps you understand this:

http://docs.coronalabs.com/tutorial/basics/scope/index.html

You need to understand the concept of what a code block is. Along with that an understanding of why in general nesting is bad. It really helps to have solid code indention and variable naming.

While nesting has some value and you should use it when it makes sense, it generally makes for more complex and harder to manage code. If you’re concerned about upValue limits and max # of locals, you can modularize your code and use tables as objects.

Rob

Thanks @Rob

As Rob says, upvalue limits need never be an issue, because you could put all your functions in a table, and all your variables in another, and that would only count as two against your limit.

Thanks for your help, you’re right. What I was trying to do was to avoid having to re-write 10 levels of my game but I realize that it needs to be done to avoid problems in the future.

Just commenting on the timers and transitions.

When you declare “local alphaTmr” it is nil until you assign a value to it. On the very next row, you perform a check to see if it is not nil, but with your current code alphaTmr will always be nil. If you declare alphaTmr outside of the function, then the function can still read it as long as it is within the function’s scope and then alphaTmr may not be nil when you load your function.

When you are working with timers and transitions, the reason why you may want to assign them to variables, like your alphaTmr, is that it allows you to easily control them after you’ve started them. With transitions, you have other means of pausing or cancelling them, like the transition.cancel() that you do at scene hide, but with timers, you won’t be able to control them anymore if you create a timer locally inside a function as you’d need an object to reference to.

local function showObject( event ) local alphaTmr ... end

Because of the way scope works, the variable “alphaTmr” is only visible to the function showObject(). If you try to cancel it in scene:hide(), you won’t be able to access that timer.

Rob

Will there be any way to use and cancel that timer within the same function?

Thanks in advance
DoDi

Well, I did say how to do it already. :stuck_out_tongue:

Simply declare it before the function, i.e.
 

local alphaTmr local function showObject( event ) ... end

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

@XeduR @Spyric

Thank you and sorry for not having seen it before, I was trying to do everything within the function because I plan to do many functions and I wanted to avoid reaching 60 up values.

It really helps if you take the time and develop a solid understanding of “scope”. We have a very good tutorial that helps you understand this:

http://docs.coronalabs.com/tutorial/basics/scope/index.html

You need to understand the concept of what a code block is. Along with that an understanding of why in general nesting is bad. It really helps to have solid code indention and variable naming.

While nesting has some value and you should use it when it makes sense, it generally makes for more complex and harder to manage code. If you’re concerned about upValue limits and max # of locals, you can modularize your code and use tables as objects.

Rob

Thanks @Rob

As Rob says, upvalue limits need never be an issue, because you could put all your functions in a table, and all your variables in another, and that would only count as two against your limit.