Posts RSS Comments RSS 34 Posts and 64 Comments till now

_global in ActionScript 3

There’s no more “_global” in ActionScript 3. I wrote this class to bring it back. Here’s an example of exchanging values between two .swf files. example.swf assigns value into “_global” and external.swf watches it. The source file is here.

This movie requires Flash Player 9

There are other solutions. AS3 Global Object is a powerful package that goes beyond “_global”. Jonathan Greene’s solution is very neat and easy, too. The basic of all solutions is to use static variables and functions. Using static variables and functions is the easiest method to communicate between classes and movie clips. I’ll explain that in future posts. Meanwhile, this entry in help manual helps to understand what “static” means.

9 Responses to “_global in ActionScript 3”

  1. on 11 Mar 2008 at 9:20 amkevin

    Hmm,

    personally, i never really liked using “_global” in AS 2,
    as I felt it made it harder for others to keep track of the variable due to the variable scope, and that the variable could be accessed without. “_global.” prefix.

    Just curious, what is the difference between the example implementation above, and the singleton pattern used in AS 3?

  2. on 11 Mar 2008 at 10:32 pmShang

    I think there’s nothing wrong with _global in AS2. It was just misused in AS2. But in AS3, the story is different. Because it’s more strict, _global variables are more reliable. I didn’t use much _global variables in AS2 because I can always reference the main file as “_root”. But in AS3, “root” is only available for DisplayObject. It’s hard to reference something without using something similar to _global.

    It has not to be _global/Global or a separate class. Any static variable in any class can serve the purpose. Since people have already accepted the concept of _global means you can access it everywhere, it would be easier to reuse the concept to elaborate the idea of a static variable.

    The example I implemented uses singleton pattern to make sure there’s only one instance of Global. I think it makes sense because it won’t be reliable if there are multiple instances.

  3. on 12 Mar 2008 at 2:36 amkevin

    hmm,

    I do understand the need for the global class to be a singleton,
    but I dont quite get the benefit of making a dynamic singleton class.

    From what i know, this implementation goes in the opposite direction of encapsulation

    examples of possible problems i see when 2 mutually exclusive subsystems use this class is,
    - overwriting of a variable which both subsystems use the same name, but are for different intents
    - knowledge of the other subsystem, which by right are exclusive to each other [to avoid the first senario]

    your implementation of the global class is interesting in its direction, and I would like to better understand when and why you would use this class instead of a normal singleton.

    which is why I asked what “what is the difference between the example implementation above, and the singleton pattern used in AS 3″

    I apologise for being unclear in my first post.

  4. on 12 Mar 2008 at 3:41 amShang

    The reason of why it’s a dynamic class because it can add on any variable. It offers more flexibility than a customized class (be it a singleton class or not). This is designed to be as similar to the “_global” in AS2 as possible. The class is meant to share information, so it should be in the opposite direction of encapsulation.

    I feel the overwriting problem is inevitable because the intention of _global class is to allow both read and write access to some information in anywhere of the system. It’s a feature of the class to allow variables to be overwritten and there are scenarios overwriting a variable is a why of solving some problems.

  5. on 12 Mar 2008 at 4:39 amkevin

    Thank you for taking the time to share your views

    Althrough I cant totally agree with the trade off required for the additional flexibility that this class offers

    I found this discussion with you refreshing, and I look forward to having more such discussions with you in future

  6. on 12 Mar 2008 at 5:46 amShang

    Yup, I agree there’s some trade off when using _global. So, it’s the developer’s responsibility of evaluating which method suits the project best.

    It’s my pleasure to discuss with you, too!

  7. on 31 Jul 2008 at 8:55 pmdamian stewart

    hey,

    i posted something about this over here
    http://greenethumb.com/article/11/global-variables-in-as3

    in short, ’static’ is only ’static’ to the current swf and its children; it does not propagate up to the root document. you cannot lazily construct a static (singleton) class instance in a child swf you are dynamically loading: it has to be constructed in the root, otherwise you end up with several different ’static’ instances that don’t know about each other.

    more details on my comment here.

  8. on 31 Jul 2008 at 8:57 pmdamian stewart

    sorry, the link should be to
    http://greenethumb.com/article/11/global-variables-in-as3#c000070

    i only wrote the comment, not the whole article ;-)
    d

Trackback this post | Feed on Comments to this post

Leave a Reply

For spam filtering purposes, please copy the number 7558 to the field below:

Pingbacks/Trackbacks

  1. […] you can read more about this here. http://flashrevolution.net/_global-in-actionscript-3/ […]