Porting _global in ActionScript from AS2 to AS3

Global variables

The _global variable has been removed in ActionScript 3.0, it is suggested that you use specific objects that are created in the global scope to store your data instead of globing unrelated bits and pieces under one global object. However if you absolutely need a general global object here is how you would do it. Create a file in the root of your class directory called _global.as and drop the following code into it.

//declaration

Code:

package {
// this reinstates the _global object that was removed from from AS3
// however one thing has changed the global object must have a
// category for storing data dynamically
// ex: _global.ply.name
// this is the name of the player stored in the global object

public class _global {
public static var ply:Object = new Object();
public static var gen:Object = new Object();
public static var glob:Object = new Object();
}
}

/*definitions here are automatically accessible to all code in the file where this is being declared. Aka. this is a security risk. Allowing a hacker to insert code into the global object at run time and then they can try and run it from anywhere in your program. */



comments powered by Disqus