Access Global Function

I have a “class” which is like a factory class with nested functions inside of it that controls the instantiated element. I need to call a global function in order to set a variable from within that class. Anyone know how this can be done?

Example:

-- Class  
function new(params)  
  
 local item = (create image rect)  
  
 local item:touch(e)  
 calltoexternalfunction(e.target.num);  
 end  
  
 item:addEventListener("touch",item);  
  
end  
  
-- Main.lua  
local item = require('class');  
local myitem = item:new();  
  
function calltoexternalfunction(itemnum)  
 print("success");  
end  

Thanks in advance. =) [import]uid: 63800 topic_id: 12598 reply_id: 312598[/import]

When you use addEventListener, the first parameter should be a string with the event name you are watching for, and the second parameter should be the function that is to be called. Your example is very close to correct. Change line 10 to item:addEventListener(“touch”,touch) and I think you’ll be ok, since the function inside class is called touch.

(I’d name the function something different than touch, so it’s less confusing) [import]uid: 70391 topic_id: 12598 reply_id: 46168[/import]

What I’m trying to do is access variables and functions outside of a loaded (required) “class” So if I have a class that creates a weapon I want a function within that weapon class to be able to update a variable in my main.lua file or even access a function in the main.lua class. Anyone know how I can do this? [import]uid: 63800 topic_id: 12598 reply_id: 46177[/import]

Ah, I see where your problem is now. You need the line:

module(…, package.seeall)

at the top of your class. Everything else you are doing is correct. [import]uid: 70391 topic_id: 12598 reply_id: 46353[/import]

Yeah, I have that at the top of all my modules. Still can not access variables or functions outside of itself. I have decided to do it a different way for now but would still be interested if anyone else had any ideas.

Thanks for taking the time to help me. [import]uid: 63800 topic_id: 12598 reply_id: 46355[/import]