Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Question about overriding dispatchEvent in Sprite class — Gideros Forum

Question about overriding dispatchEvent in Sprite class

krl3000krl3000 Member
edited October 2013 in General questions
Does the dispatchEvent method in the Sprite class do anything other than just fall through to the EventDispatcher.dispatchEvent method? I'm asking because I'm thinking about overriding the Sprite's dispatchEvent method. My overridden method will call EventDispatcher.dispatchEvent(self, event) after it's done doing its thing (because I couldn't figure out a way to make it call back to the original Sprite.dispatchEvent) and I want to know if I'll be losing any functionality by doing so. If so, I can create my own class, "MySprite", and use that wherever I'd currently create a Sprite then in MySprite.dispatchEvent do my custom code and call Sprite.dispatchEvent(self, event), but I'd like to avoid that if it's not necessary. Also, I'd lose the override code in anything that's based on Sprite, such as the stage object.

In case you're wondering WHY I'm thinking of doing this, here's the background:

I'm new to Gideros and lua and trying to implement some general error-handling in my (first) Gideros program. I'm wondering what the best way to do this might be or what other Gideros users are doing. I saw the excellent article by bowerhaus ("Proper Log Crash Logging for Gideros") but that's iOS-specific and I'm writing for Android right now (no Mac to compile on). So, until I'm up to trying to write my own Gideros plug-in, I'm trying to come up with an alternate method.

One thing I'm considering is using pcall() to catch errors and implementing my own logging (similar to bowerhaus' use of freopen() to write errors to a file, but internally in lua). I found through experimentation that this works through method chains so only the topmost method in the chain needs to be surrounded by pcall() (although you lose the stack trace, something I'm trying to figure out a way around). However, events start their own method chain so just starting the application with a single pcall() surrounding a method that kicks everything off won't work. I wrote a general "try()" global method that takes a function (with no arguments) as an argument and calls it using pcall() and does basic error-handling on the result, but putting that around the code inside EVERY event handler seems a bit awkward.

My thought is that I could override Sprite:dispatchEvent and put my try() call around the base dispatchEvent call to catch errors, like this:

function Sprite:dispatchEvent(event)
try(
function()
EventDispatcher.dispatchEvent(self, event)
end
)
end

I can provide more code for try() and the supporting methods if it helps you understand what I'm asking or if anyone is curious.

It seems to work fine, but I'm worried that maybe Sprite:dispatchEvent is doing something other than just calling EventDispatch.dispatchEvent and I'm losing some essential functionality with this approach.

Anyone have any info or feedback on this or general error-handling advice?

Thanks!

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Currently I'm not sure if there is any difference, but just in case, you can do it like this:
    Sprite._dispatchEvent = Sprite.dispatchEvent
     
    function Sprite:dispatchEvent()
        try(
            function()
                Sprite._dispatchEvent(self, event)
            end
        )
    end
  • krl3000krl3000 Member
    edited October 2013
    Thanks. However, I just tried it after accepting your answer and copying your line of code and I got the following error:

    attempt to call field '_dispatchEvent' (a nil value)"

    I looked at the table for Sprite and I see "dispatchEvent" but no "_dispatchEvent".
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    yes, but you create it with this line:
    Sprite._dispatchEvent = Sprite.dispatchEvent
  • Oops, missed that line. Interesting trick. Thanks!
Sign In or Register to comment.