_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.

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.


kevin

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?

Shang

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.

kevin

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.

Shang

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.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License