I’m starting a new category on my blog called “Why it Works that Way” (WIWTW). I was chatting with some of the user/community groups leaders yesterday and was saying that my favorite part of working on the Flash Player team is that I get to ask all of the questions I’ve wondered about but didn’t know who to ask. Now I just stroll down the hall and ask the person that wrote the feature something to the effect of “Why is the sky blue?”
Normally, I get an answer that makes a great wooshing sound as it goes over my head. Then in realization that I am a mere mortal, I get a really great answer that explains so much about how Flash works in general. You’d be amazed about how a question about transparency has big ramifications on accessibility. Realizing how useful this would have been when I was writing my own code for a living, I want to share some of the answers I’m learning.
So, today’s topic: Object.watch. Watch is a way of monitoring the value of a variable and if something tried to change it, a callback is invocked with the old value, new value, and property name. You can then validate, modify, or otherwise cleanup any values BEFORE they are made into values and any other part of the application can use the values. Anything you returned from the callback would become the new value of the variable.
I LOVED this method in ActionScript 2. I was horrified the first time I got a compiler error in AS3 and discovered my buddy was no longer there. Thinking that it was an oversight, I went and gave my plea/whine to many engineers. Surely everyone would recognize the inherent value of watch! (ok, so I am playing this up just a whee bit). I was really surprised to see that it wasn’t an oversight but a conscious decision and there was very strong feeling that it should never ever come back. I think the exact phrase was “over my dead body.”
Here’s why. Object.watch has a huge performance overhead.
My first reaction was, “well, what if I want to take on this performance overhead in my application to get its usefulness???” Then it was patiently explained to me that it wasn’t just my SWF that would slow down, it was player was always slower just because the capability was there. Removing Object.watch sped up overall Flash Player performance a few percentage points. Every time you set a veariable value, Flash Player was having to check to see if the variable was being watched. As that realization dawned on me I realized that my Object.watch buddy was really more of a little devil sitting on my shoulder tempting me into laziness.
Solution: The way in AS2 or AS3 to do the exact same thing in a more performance friendly way is to set up getters and setters for the variable and then do whatever operations you want. It is a little more work (typing), but actually does have some practical performance benefits. This does have the downside of not working for people that still use frame actions, but I think that is a minor trade off for an across the board speed improvement.
Future WIWTWs
If there is a question you want me to ask about the inner workings of Flash Player or ActionScript, go to this page and submit a comment. I’d like to keep comments on this post relevant to the post itself.
Digg this!
June 13th, 2007 at 1:56 pm
Yeah, I have missed it in AS3 a couple times, but I honestly don’t use it that much. However, when I did use it, I abused the hell out of it. I figured out a way to watch a function on a movieclip, and if it changed, I would rewrite the function to inject my code back into the function. After crashing Flash a few hundred times, I finally got it to be stable. It was kind of eerie and virus-like, but I felt like I had wrestled Flash to the ground.
So, godspeed Object.watch! You were a crazy little function.
June 13th, 2007 at 2:01 pm
I think I might have a solution for you.
Not long ago I made a globally accessible class that can store variables, functions, etc. Something like _global in AS2.
Later I also added similar functionality to watch() by utilizing event listeners - now the “property changed” event is fired every time you change a property of global object.
See for yourself - http://www.uza.lt/codex/as3-global-object/
June 13th, 2007 at 10:38 pm
Great post, Justin. I always wondered what the tradeoff was for listeners. Thanks for getting it in the record like this!
jd
June 14th, 2007 at 7:39 am
Actually I always tried to avoid Object.watch because of all the fuss people made about how it would affect performance, and yes I absolutely agree that avoiding it at all costs is not such a bad thing…
If you ever find yourself in a situation where you have to use it, the best thing to do is extend the object and use getter / setter methods to either fire an event or perform some additional task.
June 14th, 2007 at 6:55 pm
Why It Works That Way: You ask, Justin answers
Justin is starting a great new series on his blog called “Why It Works That Way.” He’s your man on the inside, with direct access and real answers. This is a great opportunity to ask those questions you’ve always wanted…
June 15th, 2007 at 12:58 pm
Please PLEASE, blog more about the player, this stuff is damn useful, for better performance code!!
I knew about the object.watch but i never used it, I always suspected there must be real overhead in using it
July 24th, 2007 at 9:46 am
Object Watch sounds like something everyone should have. Thanks
August 28th, 2007 at 8:30 pm
So I was building out a change aware object from parsed XML in AS2, now as I start to port the code from AS2 to AS3, I need to change the way I build out the object. Any suggestions on how to dynamically build out getter / setters so I can track which values are changed? I’ve started looking at the ChangeWatcher, but not sure it will do what I need. Any Ideas, suggestion would be appreciated. Thanks
September 13th, 2007 at 4:54 pm
Good post Justin! Object.watch has been very valuable day to day for me. I didn’t realize it had a significant affect on performance.
November 7th, 2007 at 6:46 am
Great post, Justin.
The Object watch is very interesting and is a must have for everyone
November 19th, 2007 at 8:16 pm
thanks for the info on Object.watch, very ineresting stuff justin
November 24th, 2007 at 11:51 am
thanksss
December 4th, 2007 at 3:01 pm
If you can ask some engineer in Adobe, I would like to know why Director Shockwave can connect peer-to-peer to other host (MultiUser xtra) while Flash cannot. If there is a security concern, it should be the same in both plugins. Having a way to connect peer-to-peer in flash would be great for online multiplayer games. It would be also better if we can establish peer-to-peer connections through routers, like hamachi does.
December 29th, 2007 at 6:18 am
Interesting article. Thanks for share this
Many Gracias from Germany, Webdesigner, Harun Isik (MedienStern)
January 2nd, 2008 at 11:01 pm
[…] er (’/outgoing/justin.everett-church.com/index.php/2007/06/13/wiwtw-so-lo ng-objectwatch/’);” href=”http://justin.everett-church.com/index.php/2007/06/13/wiwtw-so-l ong-objectwatch/” target=”_blank”>Justin Everett-Church’s blog for reasons w […]
January 24th, 2008 at 8:48 am
Great article. Adobe is one of my favorite used programs… I like it to learn more about “Adobe” Thanks for share this article.
January 28th, 2008 at 11:25 pm
Thanks for great article..
January 29th, 2008 at 1:54 am
object.watch is something similar to setInterval(). The only difference is the value of variable is triggered at every “time” event. While on the other hand object.watch is triggered at every “assignment” event.
So one can trace it in place of object.watch. However the repeatitive trace window sometimes become very annoying.
So setInterval() too should be deprecated ?
January 30th, 2008 at 11:10 am
If you can ask some engineer in Adobe, I would like to know why Director Shockwave can connect peer-to-peer to other host (MultiUser xtra) while Flash cannot. If there is a security concern, it should be the same in both plugins. Having a way to connect peer-to-peer in flash would be great for online multiplayer games. It would be also better if we can establish peer-to-peer connections through routers, like hamachi does