Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can't store a reference to Event:getTarget/getType() for later use? — Gideros Forum

Can't store a reference to Event:getTarget/getType() for later use?

MellsMells Guru
edited December 2013 in General questions
Hi,

I'm trying to store a reference to a Touch event for later use.

pseudo code
 
local function pressTimer(self)
    print (self.myEvent) -- ok
    print (self.myEvent.x) -- ok
    print (self.myEvent.y) -- ok
    print (self.myEvent:getTarget()) -- #### NIL ####
    print (self.myEvent:getType()) -- #### NIL ####
end
self.myEvent = e -- e is my Event
local myTimer:addEventListener(Event.TIMER, pressTimer, self)
for now I'm using this workaround, but this is not clean and I still can not get the type :
 
local function pressTimer(self)
    self.myEvent.__target = self.myEvent.target
    self.myEvent.__type = self.myEvent.type
    self.myEvent.params = nil
    print (self.myEvent) -- ok
    print (self.myEvent.x) -- ok
    print (self.myEvent.y) -- ok
    print (self.myEvent:getTarget()) -- #### NIL ####
    print (self.myEvent:getType()) -- #### OK ####
end
self.myEvent = e -- e is my Event
self.myEvent.target = e:getTarget()
self.myEvent.type = e:getType())
local myTimer:addEventListener(Event.TIMER, pressTimer, self)
is that expected, or not?
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • Most probably due to optimization of input events, the reference to objects and probably whole event objects are passed as weak reference. Meaning the source may be garbage collected even if you still hold this reference.
    Most probably it speeds up the garbage collection (imagine mouse moving event where each event object does not get collected as soon as possible)

    That said, you should then better keep references to the values you need yourself, rather than keeping weakly referenced object.
    Like this:
    self.myEvent = {}
    self.myEvent.x = e.x
    self.myEvent.y = e.y
    self.myEvent.target = e:getTarget()
    self.myEvent.type = e:getType()
     
    local function pressTimer(self)
        print (self.myEvent) -- ok
        print (self.myEvent.x) -- ok
        print (self.myEvent.y) -- ok
        print (self.myEvent.target)
        print (self.myEvent.type)
    end
  • Got it, thanks.

    The thing is that I use a events handler (for several events) later and there I use getType() and getTarget() which is why I was wondering :
    I found that getTarget() accesses .__target, how is stored type for getType()?

    It seems it's not .__type.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Mells, I think that getType is simply .type as in
    print(e.type)
  • MellsMells Guru
    edited December 2013
    Just wondering : is there a specific reason why it's not consistent with .__x ?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Most probably for historical reasons. At first it probably did not had a getter, thus was simply accessed as property. But then was remodeled with weak reference and getter, leaving property for backwards compatibility. Just a guess :)
Sign In or Register to comment.