AS3 + DocumentClass + SetVariable...?

In previous AS versions, the Javascript in an HTML page could communicate directly with variables in the Flash by using SetVariable(). However, with AS3, this appears to be different.

I am well aware of ExternalInterface, and the goodies it provides for function calling in both directions. However, from a stylistic programming perspective, I have some cases where in the Javascript, I’d rather have a variable that it could set, rather than a function call. YES, I know that in reality, it’s just a function call anyway, with “SetVariable”, but ideally, I’d like to be able to set a variable directly, and if that can’t be achieved, I want to back up little by little until I get the closest solution I can to that ideal. Using a single function called SetVariable seems to be a better compromise than having a whole slew of separate functions to call from Javascript, one for each variable.

My ideal case was something like:

document.myFlashObj.varname = “blah”;

I was then hoping that I could have an implicit setter function in the DocumentClass object like:

public function set varname(val:String):void { … }

I then tried:

ExternalInterface.addCallback(“varname”,varname);

I got a compiler error about it not recognizing the varname function to add.


So, once I decided that this probably wasn’t going to work (the assignment to varname in the Javascript, specifically), I switched to the SetVariable() approach.

I was hoping that:

document.myFlashObj.SetVariable(“varname”,“blah”);

would call my setter function. it did not.

moreover, even trying to use the SetVariable to set an ACTUAL public string variable in my DocumentClass object still didn’t work. I get no errors or exceptions, but the value doesn’t get put into my DocumentClass’s variable. I don’t know if it’s failing, or getting put in some other scope I’m not aware of, but it’s definitely not where I wanted it to be.

So, I figured that maybe SetVariable() was no longer available to AS3 (even though the online docs seem to indicate it is). So, I tried this:

ExternalInterface.addCallback(“SetVariable”,mySetVariable);

and

public function mySetVariable(varname:String,varvalue:String):void { … }

the call from javascript STILL didn’t execute my function.

so, what gives? Apparently SetVariable() is still around, and pseudo-reserved in that it cannot be reused or overridden. But it doesn’t function as expected or documented.

My next best option is to have a callback name like “SetVariable2” or whatever. This btw DOES WORK. but now my javascript is using an ugly third-string quarterback (“document.myFlashObj.SetVariable2(…)”) to do its business, and I don’t like that.

help?