Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Getting at class data inside an event listener — Gideros Forum

Getting at class data inside an event listener

DudDud Member
edited May 2012 in General questions
I have a question about getting access to class data from within an event listener function...

I have successfully set up an event listener function inside my class and am passing extra event data through to it without problems.

However in addition to the data I am passing through to the listener function, I would also like to access data from the class itself using the self. qualifier however I get the common error 'attempt to index field 'xxx' (a nil value).

Am I wrong to assume that inside an event listener function I would have access to class properties? Do I only have access to event data specifically passed to the listener function?

I can provide a code sample if the above explanation is confusing :)

Thanks

Likes: gmarinov

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • OZAppsOZApps Guru
    edited May 2012
    you can pass the class as an extra parameter as
    obj:addEventListener(Event_Name, Event_Handler, obj)
    and in the handler, you need to get that as
    function Event_Handler(theObj, theEvent)
    end
    or am I missing something in your question?
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • DudDud Member
    Thanks, I did read that thread but couldn't see an example that seemed to match what I was trying to do. I will re-read as I am new to Lua so am still learning :)


    OzApps - I am already passing data as an extra parameter, my problem is that I want to ALSO get access to data from my class as well. It does not seem possible to pass data from TWO sources through to the event handler.

    I've seen several examples where an event handler is declared like so:



    function Classname:Event_Handler(eventdata)

    local buttonInfo = eventData:getTarget() -- This gives me access to event data

    self.xxxx -- But trying to access a property of the class results in nil
    end


    I suppose my question is - whats the purpose of qualifying an event handler with the name of a class if I can't access class data from within the event handler?


    I have already thought of a way around my problem but I am interested to know the answer to the above question.

  • ar2rsawseenar2rsawseen Maintainer
    This is the right example for you:
    --let's create our own class based on Sprite
    Scene = Core.class(Sprite)
     
    function Scene:init()
    	--variable self is referencing to instance of the class scene
    	--so if we do this
    	self.somevar = "somevalue"
    end
     
    --we will be able to access it like this:
    local scene = new Scene()
    print(scene.somevar)
     
    --now if we add a system event to this scene like this
    scene:addEventListener(Event.MOUSE_DOWN, function()
    	--we won't be able to access somevar
    	print(self.somevar) -- results nil
    	--because function we defined has it's own scope
    	--and variable self is now referencing to it's scope
    	--and note scene's scope
    end)
     
    --but if we pass scene as data parameter
    scene:addEventListener(Event.MOUSE_DOWN, function()
    	--we can access somevar
    	print(self.somevar) -- results "somevalue"
    	--because self is now referencing to whatever we passed as data parameter
    	--which was scene
    end, scene)
    We define class and in first example we can't access class parameters and in second example we can, all only because we pass self or class instance as a third parameter to addEventListener function
  • DudDud Member
    Thanks for the example - I shall work on this tonight and see how I do :)
  • @Dud, if you can pass only one item and you want to access more than one, simple pass a structure (a table or an object)
     local myObj={}
     myObj.myClass = theObj -- your class
     myObj.myVar1 = 100        --Some var you wanted
     myObj.myVar2 = 200       --Some other var you wanted
     
    theObj:addEventListener(Event.Name, Event_Handler, myObj)
    do you think that could help you?

    and if you are using this in a theObj:func type function, then you can replace the theObj with self as it is referencing to the same.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • DudDud Member
    OzApps - yes thanks thats exactly what I did in the end. Re-reading the API docs I saw the example of constructing a more complex event object and that gave me exactly what I needed.
Sign In or Register to comment.