How to arrange for a dispatchEvent listener to have access to my class instance scope? (see code attached)

Question: If I’m using a “Runtime:dispatchEvent” to pass a message, how can I arrange such the listenering that picks up the message would have access to my class instance?  

That is, in the code below, if I want my File 2 to pickup the event dispatched, but then in the listener code I want this code to have access to the MyWidget instance (i.e. the “new_inst” variable in the code) how would I do this?    

The code below doesn’t work…That is want the “new_inst:zoom(event” function to be called, and then after the listner function is then called, that it can access “self.myVariable”.

File 1 (say a scene)

local event = { name="zoom", value = newZ, phase = event.phase} Runtime:dispatchEvent(event)  

File 2 (in a class used within the scene somewhere - pseudocode)

MyWidget = {} function MyWidget:new()     local new\_inst = {}    -- the new instance\\     new\_inst.myVariable = 123     new\_inst.moveableView = display.newGroup() -- \*\*\* DOES NOT GET CALLED \*\*\*     function new\_inst:zoom(event)           local moveableView = event.target -- \*\* TRYING TO GET ACCESS TO INSTANCE \*\*     print("Instance's variable", self.myVariable)           return true     end     new\_inst.moveableView:addEventListener( "zoom", new\_inst )     return new\_inst end return MyWidget

Close, but not quite.  You’re mixing object and runtime listeners

Do this:

--new\_inst.moveableView:addEventListener( "zoom", new\_inst ) Runtime:addEventListner( "zoom", new\_inst )

The object ‘new_inst’ will catch the event.

actually it still didn’t fix the issue…so I have now (see below) but get:

         Attempt to call method ‘addEventListner’ (a nil value)

MyWidget = {} function MyWidget:new()     local new\_inst = {}    -- the new instance\\     new\_inst.myVariable = 123     new\_inst.moveableView = display.newGroup() -- \*\*\* DOES NOT GET CALLED \*\*\*     function new\_inst.zoom(event)         local moveableView = event.target -- \*\* TRYING TO GET ACCESS TO INSTANCE \*\*     print("Instance's variable", self.myVariable)           return true     end     Runtime:addEventListner( "zoom", new\_inst.zoom )     return new\_inst end return MyWidget

I typoed the function name…  

addEventList e ner  (I commonly flub this when I’m typing quickly.)

Note: Most of us who answer here may occasionally misspell or make syntax errors in our replies, so be sure to refer the the API docs and think about what the error message is telling you.

http://docs.coronalabs.com/daily/api/

I couldn’t get it to work trying to follow the pseudo-OO code, but the following does seem to work ok…the positioning of the local zoomListener function seems to pick up the “new_inst” ok…

MyWidget = {} function MyWidget:new()     local new\_inst = {}    -- the new instance\\     new\_inst.myVariable = 123     new\_inst.moveableView = display.newGroup()     local function zoomListener(event)         local moveableView = new\_inst.moveableView     end     Runtime:addEventListener ("zoom", zoomListener)        return new\_inst end return MyWidget

I think you’re mixing up two different issues.  You may have a scope or visibility issue in your code.

This example does basically the same thing (object catches event), and has been tested:

local function zoomListener( self, event ) print("Zoom event caught by ", self.myName ) end local billy = { myName = "Billy" } billy.zoom = zoomListener Runtime:addEventListener( "zoom", billy ) local bob = display.newCircle( 100, 100, 10 ) bob.myName = "Bob" bob.zoom = zoomListener Runtime:addEventListener( "zoom", bob ) Runtime:dispatchEvent( { name = "zoom" } ) 

Prints:

Zoom event caught by Billy Zoom event caught by Bob

ok nice - thanks for the clarification roaminggamer

At the end of the day, the implementation can vary, just remember that for ‘dispatchEvent’, the source must be the same.  

i.e.  Dispatched via Runtime == caught by Runtime

It is worth noting, this is one of the more powerful features of Corona that I don’t see a lot of people using.  While it has a few drawbacks (cleaning up listeners) it can help make your code very clean.

Close, but not quite.  You’re mixing object and runtime listeners

Do this:

--new\_inst.moveableView:addEventListener( "zoom", new\_inst ) Runtime:addEventListner( "zoom", new\_inst )

The object ‘new_inst’ will catch the event.

actually it still didn’t fix the issue…so I have now (see below) but get:

         Attempt to call method ‘addEventListner’ (a nil value)

MyWidget = {} function MyWidget:new()     local new\_inst = {}    -- the new instance\\     new\_inst.myVariable = 123     new\_inst.moveableView = display.newGroup() -- \*\*\* DOES NOT GET CALLED \*\*\*     function new\_inst.zoom(event)         local moveableView = event.target -- \*\* TRYING TO GET ACCESS TO INSTANCE \*\*     print("Instance's variable", self.myVariable)           return true     end     Runtime:addEventListner( "zoom", new\_inst.zoom )     return new\_inst end return MyWidget

I typoed the function name…  

addEventList e ner  (I commonly flub this when I’m typing quickly.)

Note: Most of us who answer here may occasionally misspell or make syntax errors in our replies, so be sure to refer the the API docs and think about what the error message is telling you.

http://docs.coronalabs.com/daily/api/

I couldn’t get it to work trying to follow the pseudo-OO code, but the following does seem to work ok…the positioning of the local zoomListener function seems to pick up the “new_inst” ok…

MyWidget = {} function MyWidget:new()     local new\_inst = {}    -- the new instance\\     new\_inst.myVariable = 123     new\_inst.moveableView = display.newGroup()     local function zoomListener(event)         local moveableView = new\_inst.moveableView     end     Runtime:addEventListener ("zoom", zoomListener)        return new\_inst end return MyWidget

I think you’re mixing up two different issues.  You may have a scope or visibility issue in your code.

This example does basically the same thing (object catches event), and has been tested:

local function zoomListener( self, event ) print("Zoom event caught by ", self.myName ) end local billy = { myName = "Billy" } billy.zoom = zoomListener Runtime:addEventListener( "zoom", billy ) local bob = display.newCircle( 100, 100, 10 ) bob.myName = "Bob" bob.zoom = zoomListener Runtime:addEventListener( "zoom", bob ) Runtime:dispatchEvent( { name = "zoom" } ) 

Prints:

Zoom event caught by Billy Zoom event caught by Bob

ok nice - thanks for the clarification roaminggamer

At the end of the day, the implementation can vary, just remember that for ‘dispatchEvent’, the source must be the same.  

i.e.  Dispatched via Runtime == caught by Runtime

It is worth noting, this is one of the more powerful features of Corona that I don’t see a lot of people using.  While it has a few drawbacks (cleaning up listeners) it can help make your code very clean.