Are global function ok?

I have been making a game I have made sure to make as much local attribute as possible. But have some global function I am also using composer is it worth the performance enhancements I am only slightly in my game. Also I get some errors when I do something like
– sample 1
local mySampleObj = display.newRect(100, 100,100,100)
local function mySampleObj:touch(event)
–run
end
–sample 2
local function test1()
– trying to run this
end
local function test2()
–stuff happens
test1()
end
test2()
–sample 3
local myTimer
local function test1()
–stuff happens
myTimer = timer.performWithDelay (1000, timerFunction, 1)
end
local function timerFunction()
–run this
end

I would do this in code editor but I am doing this on my iPhone and have no wifi on my computer.
I understand this has to do with scope I was just wondering if there are any easy work arounds to get them to work or just make the functions global.
Thanks Scott

First things first: When you post code, please use the button in the centre of the second row of the rich text editor. It’s on the right of the picture insert and left of the quote buttons. This will format code properly when you post.

Secondly, no you should not use global functions. There is a speed overhead to referencing anything global so you should try to keep as much as possible local. Debug variables are ok, because they should get removed eventually, but don’t create display groups, functions, tables or anything else for long term global use.

Corona posted a great blog entry on just this topic: https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

I have also written a little utility for showing debug values on device, just like they show in the simulator console, which you might find useful: https://code.coronalabs.com/code/device-debug-print-console

Ok, thanks. I don’t think there is a text editor on mobile version of forums if there was I would use that text editor.

The rule is to TRY to avoid globals as much as possible, but don’t worry about using a global once in a while. It is a logical step on the path of learning Lua, and not a big problem. Using a global instead of a local will most like only cause a 0.01% speed hit - unless you do it all over the place.

Performance is all about balance. If your global function is called once in the scene’s entire run, then you won’t have an effective performance hit.  Call it a couple of thousand times, then maybe you will take a performance hit.

But the other problem with global functions is you can’t be sure someone else isn’t using the same name. If you have a function name doSomething() and a library uses a function named doSomething() you stomp on each other.  Its a good practice that the higher up in the code tree a function exists, you need longer, more complex names.  Ideally you would namespace your functions to avoid name collisions. This is one of the things that the no more globals tutorial. By putting your globals into a locally included table, you get global features, name spaced to your data table.

Rob

For the record Rob, due to a typo your example wouldn’t actually cause a function conflict  :stuck_out_tongue:

doSomething ()

doSometiong ()

Thanks, fixed.

First things first: When you post code, please use the button in the centre of the second row of the rich text editor. It’s on the right of the picture insert and left of the quote buttons. This will format code properly when you post.

Secondly, no you should not use global functions. There is a speed overhead to referencing anything global so you should try to keep as much as possible local. Debug variables are ok, because they should get removed eventually, but don’t create display groups, functions, tables or anything else for long term global use.

Corona posted a great blog entry on just this topic: https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

I have also written a little utility for showing debug values on device, just like they show in the simulator console, which you might find useful: https://code.coronalabs.com/code/device-debug-print-console

Ok, thanks. I don’t think there is a text editor on mobile version of forums if there was I would use that text editor.

The rule is to TRY to avoid globals as much as possible, but don’t worry about using a global once in a while. It is a logical step on the path of learning Lua, and not a big problem. Using a global instead of a local will most like only cause a 0.01% speed hit - unless you do it all over the place.

Performance is all about balance. If your global function is called once in the scene’s entire run, then you won’t have an effective performance hit.  Call it a couple of thousand times, then maybe you will take a performance hit.

But the other problem with global functions is you can’t be sure someone else isn’t using the same name. If you have a function name doSomething() and a library uses a function named doSomething() you stomp on each other.  Its a good practice that the higher up in the code tree a function exists, you need longer, more complex names.  Ideally you would namespace your functions to avoid name collisions. This is one of the things that the no more globals tutorial. By putting your globals into a locally included table, you get global features, name spaced to your data table.

Rob

For the record Rob, due to a typo your example wouldn’t actually cause a function conflict  :stuck_out_tongue:

doSomething ()

doSometiong ()

Thanks, fixed.