Make Function Usable Anytime

So I have a menu buttons that directly use functions to change the game state ie. restart, reset, zoom etc. These functions are also used as eventListener functions for touch events. But because they are used for two seperate purpose I am being forced to play follow the leader with rearranging all these different functions and objects. Is there is way to make the functions all simultaneous available like in other languages?

Also if I make these functions table listeners like:
[lua]function box:touch( event ) print(“hello”); end;
box:addEventListener(“touch”, box)[/lua]
Is it possible to access these functions from another function:
[lua]local function MagicallyAccessOtherfunction()
box:touch({randomEventinfo=""})
end[/lua] [import]uid: 54716 topic_id: 10567 reply_id: 310567[/import]

Absolutely.

But in your example:

local function MagicallyAccessOtherfunction()  
box:touch({randomEventinfo=""})  
end  

‘box’ needs to be visible to the function. box would need to be global, an upvalue, or you should pass it in as a parameter.

Also, if you have functions you want to be accessible across files or modules, you should make them global instead of local. [import]uid: 7563 topic_id: 10567 reply_id: 38462[/import]

Thanks ewing.
Whats an upvalue? And how would I make box visible to the function? And aren’t global function less efficient, rather what would type of touch function that made global would constitute as inefficient? [import]uid: 54716 topic_id: 10567 reply_id: 38466[/import]

I have a really big question. If I make it global then this function would be available across all files, thus across the entire game. I am using the Director Class. If I change scenes, would that function still be taking up resources? Could I overwrite it with a new function with the same name? [import]uid: 54716 topic_id: 10567 reply_id: 38469[/import]

Upvalue example:

local myUpvalue = 42  
function DoSomething()  
 print("myUpvalue", myUpvalue)   
end  

Upvalue just means a variable created outside your current scope but is still accessible. myUpvalue is created outside the function DoSomething, but DoSomething can still access it. Because I declared myUpvalue as local and not global, we call it an upvalue since it is not a global variable.

Instead of upvalues or globals, you can just pass values as parameters to the function:

function DoSomething(myparam)  
 print("myparam", myparam)  
end  
  
local myBox = GetBoxSomehow()  
DoSomething(myBox)  

[import]uid: 7563 topic_id: 10567 reply_id: 38474[/import]

A function is going to take up resources regardless of whether it is local or global. The amount of resources for a function is usually negligible. Calling a global function might be slightly slower than a local function do to implementation details in the standard Lua interpreter, but generally this is not going to be a perceivable performance problem in 97% of the cases (only tight inner loops with large iterations).

There is a saying coined by a distinguished computer scientist, “Premature optimization is the root of all evil.”

[import]uid: 7563 topic_id: 10567 reply_id: 38475[/import]

Thanks [import]uid: 54716 topic_id: 10567 reply_id: 38476[/import]

Hello guys,

I am sorry for bringing this up again, but I having a problme:

I am trying to remove an event listener that was set on a different module (separate file)

Essentially I have this function ( well, let’s call it class ) that I am requiring on my main. lua file. Inside the class, I am adding two event listeners, but now, on my main.lua file I am trying to use

object:removeEventListener("touch", aFunction) and it’s not working. I am getting this:

[code]

Runtime error
ERROR: nil key supplied for property lookup.
stack traceback:
[C]: ?
[C]: ?
?: in function ‘removeEventListener’
?: in function ‘removeEventListener’

[/code] error. Help please? [import]uid: 75258 topic_id: 10567 reply_id: 96786[/import]