In this post I’d like to talk about what I call ‘custom’ Runtime Events.
This is an example of what I mean:
local coinsLabel = display.newText( "0", display.contentCenterX, 100 ) function coinsLabel.onCoins( self, event ) self.text = event.coins end; Runtime:addEventListener( "onCoins", coinsLabel ) function coinsLabel.finalize(self) Runtime:removeEventListener("onCoins", self) end; coinsLabel:addEventListener("finalize")
Later, in any module or un-connected piece of code, I can do this to update the ‘coinsLabel’:
Runtime:dispatchEvent( { name = "onCoins", coins = 20 } )
After this code runs, the ‘coinsLabel’ will show 20.
Note: The ‘finalize’ listener is used to ensure the Runtime event listener is removed when the label is destroyed. Otherwise, the ‘dead’ object might receive ‘onCoins’ events incorrectly at a later time causing a crash or error.
SSK2 Sidenote
Please note, I don’t actually write my code as shown above. Instead, I use SSK2 helpers:
https://roaminggamer.github.io/RGDocs/pages/SSK2/globals/#runtime-improvements
Using SSK2, my code really looks like this:
local coinsLabel = display.newText( "0", display.contentCenterX, 100 ) function coinsLabel.onCoins( self, event ) self.text = event.coins end; listen( "onCoins", coinsLabel ) function coinsLabel.finalize(self) -- SSK2's "ignoreList()" removes all listed events at once and safely ignoreList( { "onCoins", "enterFrame" }, self ) end; coinsLabel:addEventListener("finalize") ... then later post( "onCoins", { coins = 20 } ) -- shorthand for "Runtime:dispatchEvent()"
This code is not significantly different, but I find it much easier to type and easier to read. Additionally, the use of SSK2’s ignoreList() function is safer and better than a series of calls to Runtime:removeEventListener().
Why Do I Use Custom Events?
So, what is the big deal?
The big deal (for me) is this allows me to:
- Make much of my non-core code ‘event’ based.
- Core code is basically game logic.
- Non-core is stuff like scoring, playing sounds, dispatching ads requests, etc.
- Keep my code separated.
The last reason is most important. As I noted above, once the event listener is set up, code anywhere else in my game can dispatch an event and it doesn’t have to know about the label. There is no need for a reference to it or any such ugliness. Simply dispatch the event and ‘Violla!’ the label is updated.
While the application of this concept to a ‘score label’ is vaguely interesting, it becomes much more interesting when applied to complex pieces of code like your ad manager, the sound manager, etc.
I will talk more about ads later, but for completeness I’ll give a more interesting example of sound code now.
SSK2 Sound Manager Example
SSK2 includes a basic “Sound Manager”. It automatically listens for the event ‘onSound’
I generally use it like this in my games:
-- Register sounds; Uusually in main.lua ssk.soundMgr.addEffect( "coin", "sounds/sfx/coin.wav") ssk.soundMgr.addEffect( "gate", "sounds/sfx/gate.wav") ssk.soundMgr.addEffect( "died", "sounds/sfx/died.wav") ...
Later, I can dispatch sound events anywhere I need them:
post( "onSound", { sound = "coin" } )
As you can probably tell, this equates to super easy sound management and coding.
What About You?
I’d like to close this (long) post with some questions for you, the readers:
-
Do you use ‘custom’ events?
-
If, “Yes”, what do you use them for?
-
If, “No”, are you thinking about using them now?
Please answer if you can and share any other thoughts you may have.