Global Variables in AS3 · Aug 16, 10:50 AM

Sometimes is just makes sense to create variables that are accessible from anywhere in your project. Prior to AS3 it was really easy… _root.myvar = 'mystring' or _globals.myvar = 'mystring. While these methods were easy, they are considered sloppy from an Object Oriented point of view. Since AS3 is all about OOP, I am not surprised Adobe nixed the AS2 _globals and _root in AS3.

So how do we create variables in AS3 that are globally accessible from anywhere a project? Well, its as easy as building a class with a static variable container. Let’s use an Object as our variable container as they are dynamic meaning we can add properties (our variables of any type) at runtime. Here is the class:

package com.greenethumb.utils 
{
    public class GlobalVarContainer 
    {
        public static var vars:Object = [];
    }
}

To use this global variable container, just import GlobalVarContainer into whatever class you would like to read/write/create your global variables. Here is a sample:

package 
{
    import flash.display.Sprite;
    import com.greenethumb.utils.globalvars.GlobalVarContainer;
    public class Main extends Sprite
    {   
        public function Main()
        {   
            GlobalVarContainer.vars.groupingID  = 1 
            GlobalVarContainer.vars.mediaID     = 4 
            GlobalVarContainer.vars.facultyID   = 5 
            GlobalVarContainer.vars.studentID   = 2 
        }
    }
}

Werd up… globally accessible vars in AS3

EDIT — 11/15/07:

If anyone is looking for a more robust global variable solution, i highly recommend looking at uza’s GlobalObject class. It uses a nice hash map and dictionary to track the variables and dispatches events when variable are modified…. it nice!


Jonathan Greene

comments:

  1. thanks, I’m confused by this problem.

    this post is great help to me.

    syler · Nov 15, 02:38 AM · #

  2. but how do you do access the global variable from inside the fla file.

    atishay · Dec 3, 05:31 AM · #

  3. hey atishay,

    the _globals object no longer exists in AS3. It is not a part of the Actionscript Virtual Machine 2 (Flash player 9’s AS3 runtime). You can use this static GlobalVarContainer class to replicate the functionality of the _globals object. If you need to reference the GlobalVarContainer in your FLA, you do it the same way as you would access it from a class in a package: import the GlobalVarContainer class and read and write properties to its vars property. Hope that clears things up.

    Jon · Dec 4, 12:57 AM · #

  4. Thanks Jon, this was really helpful!

    Johan Nyberg · Dec 10, 08:58 AM · #

  5. One question – where is the document class instanciated? Is there a way for other objects in the fla to access public properties of the document class?

    Can’t seem to find your email on the site, so it you care to drop me a line I’d be really grateful. :-)

    Johan Nyberg · Dec 10, 09:15 AM · #

  6. Hey Johan, the Document Class is instantiated when your SWF is initialized. The Flash Player will automatically call the constructor function of your Document Class when your SWF initializes. If you do not specify a Document Class for your project, the compiler will automatically create a MovieClip instance an assign it as your project’s document class.

    It is possible to reference members of the your Document class from other elements in your FLA. For more details on that have a look at http://greenethumb.com/article/23/understanding-root-and-the-document-class-in-as3.

    Jon · Dec 11, 01:03 PM · #

  7. hey, just a heads-up re AS3’s ‘static’ keyword. i’m not sure whether this is a bug or a deliberate design decision, but the ‘static’ness of a variable does not propagate up the document tree to the root.

    let me give an example. suppose you have a document class MyDocumentClass that loads as two children movie_a.swf and movie_b.swf. suppose also you have class MultiLangManager, with a static function GetInstance() that fetches the static singleton instance, constructing a new one if necessary. like this:

    class MultiLangManager() { private static var instance:MultiLangManager = null; public static function GetInstance() : MultiLangManager() { if ( ! instance ) { trace(“constructing singleton instance”); instance = new MultiLangManager(); } return instance; } }

    (ps your blog commenting system’s ‘@’ code modifier is @broken@)

    now, at some point movie_a.swf calls MultiLangManager.GetInstance(). MultiLangManager goes ahead and constructs the singleton. sometime later, movie_b.swf calls MultiLangManager.GetInstance(). you’d expect MultiLangManager to return the existing instance. it is static, after all, right? wrong. what you get is another singleton, that is also static, but only ‘locally’ static. you know this, because the console says “constructing singleton instance” not once, but twice.

    thanks Adobe. you suck. when on earth this could be useful, i have absolutely no idea.

    the way around this is, if you add to the MyDocumentClass constructer, the line
    MultiLangManager.GetInstance()
    it will construct a properly singleton properly static instance that is then visible by both movie_a.swf and movie_b.swf.

    which means, of course, that MyDocumentClass has to know which static things both movie_a.swf and movie_b.swf are going to access. considering the fact that ‘static’ is supposed to be an elegant work-around to the problem of sharing data between instances without requiring some tight coupling in the middle, Adobe’s implementation is somewhat broken, to say the least, since it brings the coupling issue right back to the foreground.

    damian stewart · Jul 31, 07:42 AM · #

  8. Meh, so much work for global variables.
    Why the heck couldn’t they add a global var myvar=1;
    to make things simple.
    >_< I hate Adobe.

    ex · Aug 26, 06:43 AM · #

  9. Anyone who needs a cross project (multi SWF file) please read carefully the comment above me by damian Stewartws. Only his fix makes it work cross-SWF.

    Barak Siman Ov · Sep 4, 08:00 PM · #

  10. thanks for the tips guys. i have not run into this prob yet… could it be when you are loading in your child swfs they are not given access to the DocumentClass’s application domain? Not sure… just wondering…

    and yeah, posting code in comments on this blog is pretty broken :)

    Jonathan Greene · Sep 4, 09:14 PM · #

  11. Im confused… So how is this set up in th FLA?

    I put the as code above with the GlobalVarContainer statements in an as file and import it yes? Then what in the fla?

    Jim Smitze · Sep 30, 11:00 PM · #

Name
Email
Website
Message
  Textile Help