Flash AS3 Global Variables?

45,140

Solution 1

If you absolutely positively have to have a global variable in as3, you could always create a file in the top-level of your source folder like this:

MULTIPLIER.as

package
{
    public var MULTIPLIER:int = 3;
}

Then, whenever you need your multiplier you could reference wherever you need it like this:

DoSomeMultiplying.as

package multiplying
{
    public class DoSomeMultiplying
    {
        public function multiplyMe(n:int):int
        {
            var m:int = n * MULTIPLIER;
            MULTIPLIER = m;
            return m;
        }
    }
}

However, I would strongly recommend that you do not do this! it is horribly bad practice, horribly slow and, well, just horrible!

But there it is, it is possible to create a global variable or constant in the default package to act as a global constant or variable.

Declaring Global Functions in AS3

Note that you can also create global functions in the same way, and you don't need to use an import statement for (similar to the built-in trace function):

greet.as

package {
  public function greet():String { return "Hello World" }
}

Similar to the global variable, this global function is accessible from anywhere without an import statement:

package bar {
    public class foo
    {
        public function foo():void
        {
            trace("New foo says: "+greet()+", no import necessary");
            // New foo says: Hello World, no import necessary
        }
    }
}

Solution 2

testGlobal is not a global variable, it is a public instance variable of the Main class. You cannot access it without using a Main class object, because without an object there is no existence for a property. Instance variables are tied to objects.

If you want to access create a property that is not tied to any particular object instance, declare it as static.

//Main.as
package {
    public class Main {
        public static var testGlobal:string = "testValue";
    }
}


//Pop.as
package {
    public class Pop {
        function pop():void {
                trace("testGloabl from Main.as" + Main.testGlobal);
        }
    }
}

Solution 3

Just do this from any level you want to call the main timeline from:

//Define your global variable
var myGlobalVariable:String = "Get Up!";
//call it from anywhere
var Root:MovieClip = root as MovieClip;
trace(Root.myGlobalVar);

That should work anytime.

Share:
45,140
Bojan Muvrin
Author by

Bojan Muvrin

please delete me

Updated on February 25, 2020

Comments

  • Bojan Muvrin
    Bojan Muvrin about 4 years

    HI i have a main class

    //main.as
    
    package {
        public class main {
            public var testGlobal:string = "testValue";
    
        }
    }
    
    
    //pop.as
    
    package {
        public class pop {
            function pop():void {
                trace("testGloabl from main.as" + testGlobal);
            }
        }
    }
    

    How can i get the testGlobal value on pop.as width out using a main class Object. Is there any method of Global variables??

    How to use global variables in AS3 .

  • Bojan Muvrin
    Bojan Muvrin over 14 years
    ok That i know, but i want a gloabl variable to set value from one class and get or access from diffrent class.. how is it possible??
  • Amarghosh
    Amarghosh over 14 years
    You can get/set the public static variable Main.testGlobal from any class.
  • Bojan Muvrin
    Bojan Muvrin over 14 years
    then how can i set a global variable in as3
  • Amarghosh
    Amarghosh over 14 years
    If by set you mean to modify the value, this is how you do it Main.testGlobal = "some other value" - If you are looking for global variables in general, again, making a variable public static is the way to go.
  • Bojan Muvrin
    Bojan Muvrin over 14 years
    how can i create a Global variable in AS3, is AS3 support Global variables like in AS2 ??
  • Amarghosh
    Amarghosh over 14 years
    I am not familiar with AS2, but in AS3 public static variables are the closest you can get to global vars.
  • alecmce
    alecmce over 14 years
    Not quite Amarghosh, my solution is closest to a global variable, albeit a weird one :)
  • alecmce
    alecmce over 14 years
    @webdreamer, yes there are, if you must have them. I refer you to my post, which you appear to have ignored!
  • webdreamer
    webdreamer over 14 years
    Well I said I don't think there are... That's because I'm not sure... All I wanted to do is to refer to the Singleton pattern as a pattern that is sometimes used to circumvent the problems of using global variables... I don't remember the specific day this was (10 days ago!) though I do remember your answer. I probably only noticed it after I posted mine.
  • Lucas Gabriel Sánchez
    Lucas Gabriel Sánchez over 13 years
    I agree with alecmce THIS IS A HORRIBLE PRACTICE!
  • Jonathan Graef
    Jonathan Graef over 10 years
    In case anyone is wondering, the reason this is A HORRIBLE PRACTICE is because another variable somewhere else might have the same name. Then you wouldn't be able to access the global variable from there (the scope where the other variable is), because it would override the global one.
  • Jeff Ward
    Jeff Ward over 10 years
    Thanks for this. I wanted a global debug function that I could use quickly and not have to add an import statement (basically like trace), and this is indeed the solution (although in my case, it's public function() as opposed to public var) Of course you don't want to build code this way, but it's nice to know for certain circumstances. To ensure these functions aren't included in a production build, you could put them in a separate -source-path and update your build scripts accordingly.