[AS3] - Send a string value to an external SWF while loading it - possible or not?

Hey guys, was wondering if that’s doable - basically I have main.SWF into which I’m loading external.SWF and would need to pass the path to an XML file to external.SWF, so that external.SWF knows which XML to use.

/** code in main.SWF **/

import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;

var ldr:Loader = new Loader();
var externalFullPath:String = "external.swf?xmlFile=different.xml";//here I'm specifying the path to the SWF file along with the XML that it should use
var rq:URLRequest = new URLRequest(externalFullPath);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
ldr.load(rq);

function done(e:Event):void
{
	addChild(e.target.content);
}

Would that be achievable?

Thanks! :slight_smile:

sometimes it is easy to make a class which should hold values needed to be access from multiple classes. So you can make something like:

package {
	public class Data extends Object {
		public static var dataObject:Object = new Object();
	}
}

then before loading the swf file you can set a key pair value to the dataObject like:

Data.dataObject.externalFullPath = "myxml.xml";

which you can then access for the swf file as:

var urlLoader:URLLoader = new URLLoader(Data.dataObject.externalFullPath);
DaniMun said

Would that be achievable?

Thanks! :slight_smile:

Yes, as usual you can receive the value in the external swf with loader info:

loaderInfo.parameters.xmlFile

if your loaded swf has variables and functions you can actually access them if you cast the loaded content to a movieclip

i.e.

function done(e:Event):void
{
    var externalSWF:MovieClip = MovieClip(e.currentTarget.content);
    externalSWF.variableName = asd;
    externalSWF.callFunctionName();
    addChild(externalSWF);
}

Hope that helps :slight_smile:

If I’m understanding you correctly, what I’ve done in this situation is to make the init function of the external swf accept an optional xml file path string as a parameter.

This way, once loaded, you just add the external swf to the stage and immediately call the init function passing it the xml file’s path.

Alternatively you can listen for the INIT event when loading the external swf and then you can set a public variable in the external swf file which holds the xml file’s path.

a.fla ------> main

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;

var loader:Loader = new Loader();
loader.load(new URLRequest("b.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInit);
function loaderInit (event:Event):void {
	loader.content["runFunction"](">>> --- init")
}
function loaderComplete (event:Event):void {
	loader.content["runFunction"](">>> --- complete")
}

b.fla ------> external

runFunction(">>> asdasd")
function runFunction (id:String):void {
	trace(id)
}

The above code is written in the timeline directly. Of course, you can write by Class.

Thanks, guys, your help is much appreciated! :slight_smile:

The solution that Ramesh provided seems to apply to my project, however I’m having some difficulties trying to figure out the right way to pass the extra parameter from the string to the external SWF.

Let me describe the specific situation I’m facing:

The customer asked me to add some additional functionality to an existing SWF template, i.e. they need to load an external SWF into the template, but would like the ability to send the XML-path parameter to the external SWF via the path to the SWF, i.e.

external.swf

to become

external.swf?=xmlFile=different.xml

However, when doing so, the template refuses to load the SWF and throws an URL Not Found error

Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

As specified on my 1st post at the beginning of the thread, I’ve also tried loading the external SWF into a simple main.swf file that uses the code below

import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;

var ldr:Loader = new Loader();
var externalFullPath:String = "external.swf?xmlFile=different.xml";//here I'm specifying the path to the SWF file along with the XML that it should use
var rq:URLRequest = new URLRequest(externalFullPath);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
ldr.load(rq);

function done(e:Event):void
{
    addChild(e.target.content);
}

but still getting the URL Not Found error. Any thoughts? :slight_smile:

I think the path rules change, it should be either absolute or relative to the loader SWF. I’d suggest you to try with absolute path first to see if the problem persists

As far as I know, that would only work if you sent the query string

“?xmlfile=bla.xml”

using URLVariables method of the URLLoader. Also the swf which is being loaded would need to be expecting such a flashvar parameter. But all seem a little overly complicated for just sending an xml path into the swf being loaded.

The other solutions in this thread are easier from your point of view, and they would mean just the same amount of work from the client (i.e entering the external.swf xml file’s path into the main template’s xml file somewhere.)

komapeb said

I think the path rules change, it should be either absolute or relative to the loader SWF. I’d suggest you to try with absolute path first to see if the problem persists

That’s exactly what I thought, and placed all the files within the same folder - to no avail whatsoever :frowning:

iamdok said

As far as I know, that would only work if you sent the query string

“?xmlfile=bla.xml”

using URLVariables method of the URLLoader. Also the swf which is being loaded would need to be expecting such a flashvar parameter. But all seem a little overly complicated for just sending an xml path into the swf being loaded.

The other solutions in this thread are easier from your point of view, and they would mean just the same amount of work from the client (i.e entering the external.swf xml file’s path into the main template’s xml file somewhere.)

That makes perfect sense, Donagh! Thanks for the headsup! :slight_smile:

Hey Dani… if you have control over the xml loading you can simply split the externalswf node value:

external.swf?=xmlFile=different.xml

the swf path would be:

bla bla bla.externalswf.split(’?xmlFile=’)[0]

and the parameter:

bla bla bla.externalswf.split(’?xmlFile=’)[1]

With these 2 values then you can do whatever you want, like the other guys have suggested.

Obviously this is wrong:

external.swf?=xmlFile=different.xml

the “=” after the “?” shouldn’t be there:

external.swf?xmlFile=different.xml
ParkerAndKent said

Hey Dani… if you have control over the xml loading you can simply split the externalswf node value:

external.swf?=xmlFile=different.xml

the swf path would be:

bla bla bla.externalswf.split(’?xmlFile=’)[0]

and the parameter:

bla bla bla.externalswf.split(’?xmlFile=’)[1]

With these 2 values then you can do whatever you want, likethe other guys have suggested.

Obviously this is wrong:

external.swf?=xmlFile=different.xml

the “=” after the “?” shouldn’t be there:

external.swf?xmlFile=different.xml

Hey Nico,

That looks like a great idea as well! :slight_smile: I think I’m gonna give it a whirl :wink: Thanks buddy!

Regarding the duplicate “=” honestly I have no idea where I came up with it from :slight_smile: since my code inside the FLA doesn’t include it :smiley:

Personally I assign the xml path to a var in the main.swf and access it from the loaded.swf

MovieClip(stage.getChildAt(0)).xmlPath

But that’s probably a noob way of doing it :stuck_out_tongue:

DaniMun said

Hey guys, was wondering if that’s doable - basically I have main.SWF into which I’m loading external.SWF and would need to pass the path to an XML file to external.SWF, so that external.SWF knows which XML to use.

/** code in main.SWF **/

import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;

var ldr:Loader = new Loader();
var externalFullPath:String = "external.swf?xmlFile=different.xml";//here I'm specifying the path to the SWF file along with the XML that it should use
var rq:URLRequest = new URLRequest(externalFullPath);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
ldr.load(rq);

function done(e:Event):void
{
	addChild(e.target.content);
}

Would that be achievable?

Thanks! :slight_smile:

Actually that works just fine as you have written it; all you need is something like :

var xmlFile:String;
function loaderComplete(myEvent:Event)
{
  var queryStrings=this.loaderInfo.parameters;
  xmlFile = queryStrings.xmlFile;
}

this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

in the external swf.

But it will not work when you run the main swf in Flash … when running in Flash it does not handle querystrings appended to URLs correctly, hence the “URL Not Found” error

are both these swfs within the same directory?

when a swf is loaded into another swf and it trys to load content the directory path must written as if the parent swf is loading it…

i.e

SwfA is in the parent directory -"/"

SwfB is in the directory “/swf/” and loads an image with the path “/image.jpg”

When you load SwfB into SwfA and it tries to load the image it wont be able to find it since the SwfB is now at the parent directory… it instead needs to load the image using “/swf/image.jpg”

double post

DaniMun, did you check it with browser / localhost or webdomain? I just tried with exact same code of your first post and it works.

komapeb said

I think the path rules change, it should be either absolute or relative to the loader SWF. I’d suggest you to try with absolute path first to see if the problem persists

All relative URLs in a Flash application must be relative to the folder containing the HTML file that loaded the main swf, even if the main swf is not in the same folder as the HTML. That applies to the URLs of swfs or other external assets defined in the either main swf, or any externally loaded swfs.

So if the files are set up as follows:

html is in any folder

main.swf is in any folder (same as html, or any other folder, provided the html loads it correctly)

external.swf is in a folder off the html folder called “swf”

different.xml is in a folder off the html folder called “xml”



then the URLRequest for main.swf to load the external swf, passing it the parameter of the xml file, all using relative URLs, would be :

URLRequest("swf/external.swf?xmlFile=xml/different.xml"); 
MSFX said

are both these swfs within the same directory?

when a swf is loaded into another swf and it trys to load content the directory path must written as if the parent swf is loading it…

i.e

SwfA is in the parent directory -"/"

SwfB is in the directory “/swf/” and loads an image with the path “/image.jpg”

When you load SwfB into SwfA and it tries to load the image it wont be able to find it since the SwfB is now at the parent directory… it instead needs to load the image using “/swf/image.jpg”

It makes sense, Matt. Indeed both SWFs are in the same directory.