How expensive are dispatching events?

Hi,

I order to remove coupling, I wanted to change my game’s architecture to utilize custom events but was not sure if it’s a good idea, especially with HUD.

I have a score text box that if player scores, it will get added continuously over a small amount of time, so if you score 100, it will count from 0 to 100 in like 50 frames and it has to listen to “enterFrame” for that amount of time so it can update itself.

I have other cases like this as well, but since I have no idea how expensive is dispatching, or in this case listening, to an event, being custom or not, please advice accordingly.

Regards,

Aidin.

@Aidin,

I use dispatched events and listeners extensively and have never had a problem.  That said, your question is quite interesting.

So, I put together a quick test to see how expensive “enterFrame” listeners are.  Basically the test adds 1000 new listeners and listener function definitions every time you tap the screen.  I then use RG Super Meter to track memory usage and FPS.  I also set FPS to 30 in config.lua.

Here is a video (on the simulator):

http://www.youtube.com/watch?v=yJdSkzpoBVk

When I ran this on my Nexus 7, I got up to about 20,000 listeners before I had slight degradation in frame rate.  The memory cost was ~1.2 MB.

You can get a copy of this app (Android only) here: https://www.dropbox.com/s/lyjcihnit2yrwxc/dp.apk

Here is the code that adds listeners:

local lc = 0 local lct = display.newText( lc, centerX, 30, native.systemFontBold, 20 ) lct:setTextColor(0,0,0) local function addListeners( num )     for i = 1, num do         local function onEnterFrame( event )             local time = event.time             return false -- Pass on to next listener (i.e. all listners will get the event)         end         Runtime:addEventListener( "enterFrame", onEnterFrame )     end     lc = lc + num     lct.text = lc end local function onTouch( event )     if(event.phase == "ended") then         addListeners(1000)     end     return true end Runtime:addEventListener( "touch", onTouch ) addListeners(1000) 

@Aidin,

I use dispatched events and listeners extensively and have never had a problem.  That said, your question is quite interesting.

So, I put together a quick test to see how expensive “enterFrame” listeners are.  Basically the test adds 1000 new listeners and listener function definitions every time you tap the screen.  I then use RG Super Meter to track memory usage and FPS.  I also set FPS to 30 in config.lua.

Here is a video (on the simulator):

http://www.youtube.com/watch?v=yJdSkzpoBVk

When I ran this on my Nexus 7, I got up to about 20,000 listeners before I had slight degradation in frame rate.  The memory cost was ~1.2 MB.

You can get a copy of this app (Android only) here: https://www.dropbox.com/s/lyjcihnit2yrwxc/dp.apk

Here is the code that adds listeners:

local lc = 0 local lct = display.newText( lc, centerX, 30, native.systemFontBold, 20 ) lct:setTextColor(0,0,0) local function addListeners( num )     for i = 1, num do         local function onEnterFrame( event )             local time = event.time             return false -- Pass on to next listener (i.e. all listners will get the event)         end         Runtime:addEventListener( "enterFrame", onEnterFrame )     end     lc = lc + num     lct.text = lc end local function onTouch( event )     if(event.phase == "ended") then         addListeners(1000)     end     return true end Runtime:addEventListener( "touch", onTouch ) addListeners(1000)