Forum Topic: AS3 project-scope var aka:AS2global

(310 views • 20 replies)

This topic is 1 page long.

<< < > >>
None

adumi

Reply To Post Reply & Quote

Posted at: 9/1/08 11:32 PM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

In AS3 - how to store dynamic information available to different parts of the project ?
In AS2 it was the _global.variable .
I put up a class, called Share. in it I declared a public var called globy and some public functions as methods to store different info within that var globi, and a function to Show whats inside ot if.
I thought that if other classes or Timelines would imporrt that Share class and than call upon it`s methods, they could input and output information from that var Globy.
To my surprise I discovered that different classes that manipulated globy (by calling different methods of that Share class) have different opinions of what globy stores within it. In short : instead of creating a project - scope variable I created a mould for each class to use privately.
What am I doing wrong ? ( Doing wrong is my main thing, I guess)

Here is the code for the Share class:

package {
	public class Share {
		function Share():void {
		}
		public var globy:String;

		public function globyYes():void {
			globy="Yes";
		}
		public function globyNo():void {
			globy="No";
		}
		public function globyShow():void {
			trace(globy);
		}
	}
}

and that is the code of any of the classes that are manipulating the Share class:

package {
	import Share;
	public class User {
		function User():void {
		}
		public var share:Share;
		public function userYes():void {
			share.globyYes();
		}
		public function userNo():void {
			share.globyNo();
		}
		public function userShow():void {
			share.globyShow();

		}
	}
}

and then from a timeline where I instanciated the User class, I`m calling it with: UserInstance.UserMethod so it inputs or outputs info within globy.
Thank you !


None

Pixelwelder

Reply To Post Reply & Quote

Posted at: 9/2/08 07:05 AM

Pixelwelder NEUTRAL LEVEL 01

Sign-Up: 03/14/08

Posts: 129

Here's how your class should look.

: package {
: 	public class Share {
:
: 		public static var globy:String;
: 
: 		public static function globyYes():void {
: 			globy="Yes";
: 		}
: 		public static function globyNo():void {
: 			globy="No";
: 		}
: 		public static function globyShow():void {
: 			trace(globy);
: 		}
: 	}
: }

Now, if you want to access a public method or property of your Share class, you do it like so:

import Share;
Share.globyYes();
trace( Share.globy );
Share.globyNo();
trace( Share.globy );

Notice that you don't create an instance of the Share class. Static methods and properties belong to the class itself and not its instances.

There's also one other way to do it: a Singleton. Your choice.

Flash Game Development Blog: { P I X E L W E L D E R S } | Coming soon: OS Wars: Winvasion!

BBS Signature

None

hesselbom

Reply To Post Reply & Quote

Posted at: 9/2/08 07:24 AM

hesselbom NEUTRAL LEVEL 01

Sign-Up: 07/19/08

Posts: 309

If you want a class to be more or less exactly like AS2's _global you could create a dynamic class.

package
{
	public dynamic class _global {}
}
_global.globy = "Yes";
trace(_global.globy);

If two classes are in the same package you don't have to import them.


None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/2/08 07:44 AM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

I don't think that'll work dude. dynamic seems to only apply to instances of a class, not the class itself.


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 9/2/08 08:09 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

At 9/2/08 07:44 AM, Moonkey wrote: I don't think that'll work dude. dynamic seems to only apply to instances of a class, not the class itself.

That's right. Sorry for bringing it up without testing.

BBS Signature

None

hesselbom

Reply To Post Reply & Quote

Posted at: 9/2/08 08:13 AM

hesselbom NEUTRAL LEVEL 01

Sign-Up: 07/19/08

Posts: 309

Yeah, disregard what I said. I was wrong.

At 9/2/08 08:09 AM, GuyWithHisComp wrote: That's right. Sorry for bringing it up without testing.

What are you doing here?


None

adumi

Reply To Post Reply & Quote

Posted at: 9/2/08 11:40 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

Thank you Pixelwelder, I have embedded your code and it is precisely what I looked for, It even enables me to assign a value directly to the global var without using it`s methods.The link about the singelton helped me fully understand at last how this singelton global works, as opposed to what I could previously find in Google which left me still confused.
As you have`nt recommended either way I`ll probably go with your code. It actually does make sense to ommit the constructor, though I have yet to understand what stands behind that `static` term and what makes it work ( same goes for that `dynamic` term even if not applicable in this case - thanks hesselbom, Moonkey and GuyWithHisComp.
I`m very grateful.


None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/2/08 01:10 PM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

With normal variables, each instance of a class has its own copy of that variable. With a static variable, there's only one copy of it which the class itself has. Since they don't belong to a specific instance, you can access them through the class name directly.

A dynamic class can have properties added at runtime. If a class is not dynamic, you can only access variables that are declared in the class definition, and you'll get an error if you try to access anything else.

For example, MovieClips are dynamic, but Sprites are not

var mc:MovieClip = new MovieClip();
mc.blabla = 1; // no error
var spr:Sprite = new Sprite();
spr.blabla = 1; // error
</core>

None

Pixelwelder

Reply To Post Reply & Quote

Posted at: 9/2/08 04:06 PM

Pixelwelder NEUTRAL LEVEL 01

Sign-Up: 03/14/08

Posts: 129

At 9/2/08 11:40 AM, adumi wrote: Thank you Pixelwelder, I have embedded your code and it is precisely what I looked for, It even enables me to assign a value directly to the global var without using it`s methods.The link about the singelton helped me fully understand at last how this singelton global works, as opposed to what I could previously find in Google which left me still confused.
As you have`nt recommended either way I`ll probably go with your code. It actually does make sense to ommit the constructor, though I have yet to understand what stands behind that `static` term and what makes it work ( same goes for that `dynamic` term even if not applicable in this case - thanks hesselbom, Moonkey and GuyWithHisComp.
I`m very grateful.

No problem. If you're looking for a recommendation, there aren't many reasons to go with a Singleton when you can go with a static class. The static class is nice and clean, too- no extra layers of abstraction to get through.

Flash Game Development Blog: { P I X E L W E L D E R S } | Coming soon: OS Wars: Winvasion!

BBS Signature

None

adumi

Reply To Post Reply & Quote

Posted at: 9/2/08 04:18 PM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

Oh, so `static` means you can`t instatiate it, and dynamic means you can do something like the `extends`, only thing on the fly. I do hope my mind is a dynamic class so all these new AS3 properties and methods won`t be lost .
Thank you, Moonkey !


None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/2/08 05:12 PM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

With dynamic classes, its still better (faster, safer) to declare your properties if you know about them ahead of time.. but it can be quite useful at times.

At 9/2/08 04:18 PM, adumi wrote: Oh, so `static` means you can`t instatiate it

Not exactly. You can still create instances of the class, its only certain variables that are static.

For example, you might have a Dog class

public class Dog {

    public var weight:Number;
    public static var heaviest:Number = 0;

    public function Dog(w:Number) {
        weight = w;
        if (weight > heaviest) heaviest= weight;
    }
}

Each dog has its own weight, but heaviest is static so it is just one variable held by the class itself, which is accessible by all dogs, or other classes.

Then if you ever needed to know what the heaviest Dog you've created was, you could just use Dog.heaviest.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/3/08 11:35 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

Hello Helpers,
I apologize for delaying, but it`s only because every piece of info I get here sends me to a very lengthy cycle of testing and reauthoring in which I attempt to overcome my crushing inaptitude by slowly eliminating all my possible mistakes.
I have a Base.swf loading 3 other SWfs: a.swf, b.swf, and c.swf, by creating for each it`s own display object. into a non-AS instance of a library symbol on the stage of one of these SWF files(b.swf) is loaded another SWF: b2.swf
a.swf, b2.swf and c.swf contain each the same set of 3 buttons, whose purpose is to manipulate and show(by trace) the value a global var: Language residing on class Global. The result is that I have somehow managed to find myself in a situation that each has it`s own instance of the global var. When you hit the show button for each set you get different results.
The reason it took me so long is that I have been forever dwarfing madly in my little tin box of a mind to understand what I misunderstood, and only after being completley sure that I am lost, I dare to post.
I attach here a link to the whole set of files, in case my problem is not obvious and someone is willing to touch this further on.
Here are the files

Pixelwelder wrote:
No problem. If you're looking for a recommendation, there aren't many reasons to go with a Singleton when you can go with a static class. The static class is nice and clean, too- no extra layers of abstraction to get through.
Thank you, It is indeed much better for the likes of me to go for the simpler and cleaner , as that too I will manage to entangle as you`ve probably noticed. Unless of course My environement is such that calls for more complicated solutions, and then I`ll just buckle up.

Thank you Moonkey for this deeper explanations of static and dynamic -it gave me the understanding of the concept and allowed me the confidence to further explore the web for how dynamic functions generally work. Your dog class emphasizes to me again how AS3 demands I change the way I think of action script and how I need to do more to adapt to the object oriented way of thought, and that the basis for making things more efficient is just to think smartly.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 12:36 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

Hopefully someone has a solution...


None

Murudai

Reply To Post Reply & Quote

Posted at: 9/4/08 12:55 AM

Murudai FAB LEVEL 09

Sign-Up: 04/15/08

Posts: 371

For global variables I use a class called 'Global' which has all the variables I want.

It looks like this:

package {
		
	public class Global {
				
		public static var root:*;
		public static var data:XML;
		public static var level:XML;
		public static var game:*;
		public static var cache:Object;
	}
		
}

You could have any number of variables in there. Accessible anywhere in the package (as long as you have it imported) as 'Global.root' or something like that :)

Murudai: Indie Game Developer
I make Xbox 360 games. And the odd Flash game.
Website: http://murudai.com/

BBS Signature

None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/4/08 03:13 AM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

Having things in different swfs complicates things a bit. Each swf has its own version of the classes, and so static variables in one swf don't carry over to the other.

I think the best way to go in this situation would be changing your global class to work more like a singleton. Then set up a LocalConnection in each swf, and pass a reference to the singleton from the main swf to each of the child swfs, so they can all get at the same instance.

That's what I'd try anyway.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 07:27 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

At 9/4/08 12:55 AM, Murudai wrote: For global variables I use a class called 'Global' which has all the variables I want.

It looks like this:

package {

public class Global {

public static var root:*;
public static var data:XML;
public static var level:XML;
public static var game:*;
public static var cache:Object;
}

}

You could have any number of variables in there. Accessible anywhere in the package (as long as you have it imported) as 'Global.root' or something like that :)

Thank you for replying, Murudai. Indeed, it is a package like this that I`ve set up in the example I uploaded in my previous message. (Here it is again)
As can be seen in this attached example, seemingly, these global package static vars are instanciated as different variables when several SWF files are involved and loaded one into the other.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 07:48 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

At 9/4/08 03:13 AM, Moonkey wrote: Having things in different swfs complicates things a bit. Each swf has its own version of the classes, and so static variables in one swf don't carry over to the other.

I think the best way to go in this situation would be changing your global class to work more like a singleton. Then set up a LocalConnection in each swf, and pass a reference to the singleton from the main swf to each of the child swfs, so they can all get at the same instance.

That's what I'd try anyway.

Thank you for getting back to me whit this information, Moonkey.
I see. Then it may be as I suspected that the Multi SWF set up is at the core of this and not my lousy coding.Then, a Singelton does seem like the next option to try, though I do not understand what is the `LocalConnection in each SWF` that you`ve mentioned, nor it`s implications on communication between the Various SWF files.

I would have dumped the SWF loading set up, for the benefit of ease of using these global vars and making things easier, but when building a whole flash site, not being able to divide the download burden is a death blow. And it is an AS2 site that I just have to transform into AS3 as code speed has become a huge factor.


None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/4/08 08:16 AM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

A LocalConnection is a type of Object in as3. It can be used to communicate between different swfs running on the same computer.

If you look up the LocalConnection class in Flash help it'll do a much better job of explaining how to use it than I can.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 08:53 AM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

Thank you Moonkey , I`ll do it, and make some experiments with the singelton global and be back to report.


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 07:11 PM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

I`ve just tried out the AS3 singelton (instead of AS2_global) var in my project and it does not work. I simply can not understand it .It seems to me I must be crazy or I am not getting something right.
Everybody needs to load SWF files on top of others ( what was called _level in AS2) , right ? Otherwise you simply can not build a flash site. It would be way to heavy, right ?
Everyone need Global variables , right ?
Everyone uses them, right ?
Otherwise how could you keep track of values when you move from one Level ( or in AS3 display Object ) to another ?
So how come there is no clear cut way to use global variables in between SWF files who are in the same display list ?
If this is impossible it would be as if Adobe just cut away the whole brunch of flash sites...
They would`nt do such a silly thing, that`s why I`m sure I`m not getting here something...
It`s such a simple request. I do not complain that it is no longer as simple as `_global.Variable = value` as it was in AS2 , I understand that things should be more structured in AS3, but it should at least be possible to achieve , no ?
I`ve tried to check the lead you gave me, Moonkey, about LocalConnection and from what I could gather this is meant for communication in between SWF files playing on Different Players.
Mine is not such a case, I only have a simple display list with various SWF files loaded into it, exactly like you do with different MovieClips( only they are SWF files), there MUST be a simple way to share a global variable in between them.

I attach here the singelton that I copied from The link PixelWelder gave me. I put into it a variable called Language which is the variable that I want to be the global variable to share among all of them just like _global was in AS2, but each SWF keeps a different value of this Language var. here is the singleton code:

package {
	public class Global {
		private static  var _instance:Global;
		public  var Language:String;
		public static function get instance() {
			if ( !_instance ) {
				_instance = new Global();
			}
			return _instance;
		}
		public function Global() {
			if ( _instance ) {
				throw new Error("Singleton already exists- use get instance() to access");
				
			} else {
				trace( "new Global!" );
				
				
			}
			
		}
	}
}

And here is the code for a 3 button set assigning values and calling that `Language` global variable. it works when it`s located in the same SWF, but once I have this 3 button set duplicated to various SWF files all added into the same display list - they all have a different value of `Language`:

import Global;
aYes.addEventListener(MouseEvent.CLICK , Yes);
function Yes(evt:MouseEvent):void {
	trace("a.swf Sets to Yes");
	Global.instance.Language = "Yes";	
}
aNo.addEventListener(MouseEvent.CLICK , No);
function No(evt:MouseEvent):void {
	trace("a.swf Sets to No");
	Global.instance.Language = "No";	
}
aShow.addEventListener(MouseEvent.CLICK , Show);
function Show(evt:MouseEvent):void {
	trace("a.swf shows: "+Global.instance.Language);

}

I also uploaded that whole little project here, so that you can test it.
I`m lost and getting nuts, please assist...


None

adumi

Reply To Post Reply & Quote

Posted at: 9/4/08 08:40 PM

adumi NEUTRAL LEVEL 01

Sign-Up: 08/29/08

Posts: 55

I found another blog post(greenthumb) tackling the global var issue. It offers something along the lines of what I`ve already tried. But, then, Magic:
in the last comment, one- damian stewart, brings up the Singelto method , the one that Pixelwelder recommended to me here. Demian stewart mentions there the exact problem that I have : multiple SWFs loaded - means trouble for a global var. But, It turns out that This Singelton method does work for multiple SWF setups!!! ( Oh, I`m so happy, It`s hard to explain how much) Only, you have to call it for the first time from the primary FLA`s document class.meaning like SingeltonClassName.instance; and that`s it. It does work !!!! probably because if the main Fla calls the singelton first, and for the one and only time then everything it calls relates to it the same Thank you Pixelwelder, and Moonkey, and demian stewart for your help and explanations.


All times are Eastern Standard Time (GMT -5) | Current Time: 10:51 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!