Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
feature request: pass self at addEventListener — Gideros Forum

feature request: pass self at addEventListener

keszeghkeszegh Member
edited July 2013 in Suggestions & requests
i'm quite frustrated about this, it would be so much better if we could set 'self' as well for the called function in addEventListener. i'm struggling a lot how to pass self and one other parameter to the called function, somehow there is always some error. and this is not the first time this happens.

Comments

  • simply 'self' could be a 4th optional parameter to addeventlistener.
  • MellsMells Guru
    edited July 2013
    @keszegh can you be more specific with :
    • what you are trying to achieve
    • a code sample of what you currently do
    • and ideally what would the code look like to you, if it was available in Gideros
    I think that would help understand your point better.
    simply 'self' could be a 4th optional parameter to addeventlistener.
    Isn't it achievable with the third parameter?
    stage:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    or in the case you seem to describe

    Case 1

    local enemy = Enemy.new()
    stage:addEventListener(Event.TOUCHES_BEGIN, enemy.onTouchesBegin, enemy)
    and
    function Enemy:onTouchesBegin()
         -- do something here
    end
    or

    Case 2

    local enemy = Enemy.new()
    stage:addEventListener(Event.TOUCHES_BEGIN, 
    function()  enemy:onTouchesBegin(param1, param2, param3)
    end)
    and
    function Enemy:onTouchesBegin(param1, param2, param3)
         -- do something here
    end
    With more informations I believe we'll be able to understand better.

    Didn't test the case 2 but you get the idea
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • keszeghkeszegh Member
    edited July 2013
    Hi @Mells, thanks for the detailed answer,
    i think case2 is what i needed, so it is indeed achievable relatively nicely, just one dummy function is needed which can be defined right there as in case2. thanks for the help.

    yet i can try to explain myself better, as my proposition still makes some sense.

    my proposed feature would be to able to add:

    instead of :
    stage:addEventListener(Event.TOUCHES_BEGIN,
    function() enemy:onTouchesBegin(param1)
    end)

    have:
    i)stage:addEventListener(Event.TOUCHES_BEGIN, enemy:onTouchesBegin, param1)
    or
    ii)stage:addEventListener(Event.TOUCHES_BEGIN, enemy.onTouchesBegin, param1)
    or worst case (with 4 parameters, that was my initial proposition, but it seems redundant and less intuitive):
    iii)stage:addEventListener(Event.TOUCHES_BEGIN, enemy.onTouchesBegin, param1, enemy)
    iiib)stage:addEventListener(Event.TOUCHES_BEGIN, Enemy.onTouchesBegin, param1, enemy)

    (for me i) seems to be the most intuitive)

    of course it solves the problem only in case of one parameter but anyway the current setup also allows to add one parameter nicely and additionals only with a dummy function as in case2 you say. so the change would be that one parameter+self would be possible to add nicely and additional parameters would need to be done as in case2.
  • perhaps i have too many times the unnecessary urge to solve something with a 'new feature', instead of finding a clever and relatively simple workaround (like yours).
    at least propositions cannot do much harm.
    thanks again
  • actually i'm still confused, now with how to pass 'event' additionally,
    so how would one modify case2 to receive the 'event' object dispatched by the dispatcher (for a custom event) like this:

    local event=Event.new("changed")
    event.color=100
    self:dispatchEvent(event)

    so how to do if i want that the function receives this event, the proper 'self' and also custom parameters.
    thanks
  • ar2rsawseenar2rsawseen Maintainer
    You could also do something like this:
    local enemy = Enemy.new()
    enemy.param1 = param1
    enemy.param2 = param2
    enemy.param3 = param3
    stage:addEventListener(Event.TOUCHES_BEGIN, enemy.onTouchesBegin, enemy)
    and
    function Enemy:onTouchesBegin(e)
         --you can access those here through self
         print(self.param1, self.param2, self.param3)
         --and even assign new property like
         self.color = e.color
    end
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Or modifying @Mells case it could be like:
    local enemy = Enemy.new()
    stage:addEventListener(Event.TOUCHES_BEGIN, 
    function(e)  enemy:onTouchesBegin(e, param1, param2, param3)
    end)
    and then
    function Enemy:onTouchesBegin(e, param1, param2, param3)
         -- do something here
         print(e.color)
    end
    Really possibilities are almost unlimited :)
  • Or you could add your custom parameters to event like this:
    local event=Event.new("changed")
    event.params = {}
    event.params.var1 = something
    event.params.var2 = something else
    event.params.var3 = another param
    event.color=100
    self:dispatchEvent(event)
  • thanks for the many fast answers,
    i think that ar2rsawseen's

    stage:addEventListener(Event.TOUCHES_BEGIN,
    function(e) enemy:onTouchesBegin(e, param1, param2, param3)
    end)

    is closest in look, length and function to what i wanted to do. together with the other suggestions probably i can get all the details of how all this event stuff works.
  • atilimatilim Maintainer
    Also I have another recommendation like:
    function Enemy.onTouchesBegin(data, event)
        local self = data[1]
        local data1 = data[2]
        local data2 = data[3]
        ----
    end
     
    self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, {self, data1, data2})
    +1 -1 (+1 / -0 )Share on Facebook
  • @atilim, i got it, so second argument passed is the event, which is passed automatically.
    on the other hand a line like
    local data2 = data[3] (2=3, hmm)
    illustrates well why perhaps passing self separately as a third parameter (second in addeventlistener, as 'event' is passed automatically then) would be more logical/intuitive.
    in any case i'm happy with the above list of solutions.
Sign In or Register to comment.