connectedpixel.com

actionscript, web development

#include for jsfl

Submitted by joelmay on 15 March, 2006 - 12:41pm.

Unlike ActionScript, JSFL does not have an include directive. I've started writing some useful jsfl classes, but having no include means a lot of copying and pasting and duplicated code. This is a bad thing.

I thought fl.runScript() would do it for me, but whatever functions I defined in the "included" file did not survive after the runScript call. Then I found a solution on MeidaSparkles. It turns out that global variables (variable defined without 'var') defined in the "included" jsfl do survive.

Here's my minor variation of the technique described in the article.

Thing.jsfl


IncludeTest.jsfl

Robert Penner (not verified) Says:

A shorter approach

16 March, 2006 - 4:44pm

Thanks for the tip, Joel. I did some testing and found that you can just do this:

Thing = function() { }

Saves you a few steps. On a different note, I discovered that if you trace a function, it prints its source code!

joelmay Says:

I should have realized

16 March, 2006 - 4:56pm

Hey Robert,
I should have realized that. Your way does the same thing without the silly hack at the bottom.

I'll go change my jsfl lib files now.
Joel

françois boukhalfa (not verified) Says:

what about an eval ?

4 July, 2006 - 9:49am

hi i was lookin' for the way to make an include in JSFL.
I've read your solution which not work completely : you can only pass Object or variable not classes and methods.

I think i've found another way

you have to declare constructors like in yours :
[included file]
myConstructor = function()
{
}

myConstructor.prototype.method = function()
{
fl.trace("test method");
}

[/included file]

[main file]

function include (file)
{
var contents = FLfile.read( file );
eval( contents );
}

include( "included.jsfl" );
var t = new myConstructor();
t.method();

[/main file]

hope that helps

François.

~{ zafa }~ (not verified) Says:

to initialize

7 June, 2006 - 1:43pm

I have found that when working with this issue you run into some further issues:

(1) When you are coding in JSFL and using the global variable hack to make classes, you end up with SEVERE cacheing issues. Meaning that if you write the class, and test it, change it and run it again, you will get the 1st versions results. The only way to clear the cache is to close Flash and reopen it, and start coding again.

(2) If you declare your class like this:

Person = function()
{
returnName = function()
{
fl.trace("hello");
};
};

And then call the file like this:
fl.runScript("Person.jsfl");

And then instantiate the class like this:
me = new Person();
me.returnName();

It works just fine. However, if you declare the
class like this:
Person = function()
{
function returnName()
{
fl.trace("hello");
};
};

Where the internal methods has the function
preceeding the function name it won't work.
However this gives an alternate benefit that
the 1st style does not give. When you write
it this way, you end up with the ability to
do this:
fl.runScript("Person.jsfl");
Person.returnName();

Which allows you to treat the Person class
as reserved namespace in jsfl.

(3) And finally, when you are using this version of classing, you have to put an initialize function in your class, like this:
Person = function()
{
var name;
function init()
{
name = "~{ zafa }~";
};
function returnName()
{
fl.trace("name = "+name);
};
};
Person.init();

Then you can call use the Person as a reserved
namespace (sortof) and use it like it's always
been there.
fl.runScript("Person.jsfl");
Person.returnName();

If you tried doing the following it won't work:
Person = function()
{
var name = "~{ zafa }~";
function returnName()
{
fl.trace("name = "+name);
};
};

And then called it from your other jsfl file:
fl.runscript("Person.jsfl");
Person.returnName();

I created my own string class that allowed me
to do things like swap slashes, etc. but what
I found was that it wouldn't let me change
the value of variables declared in var whatever;
statements within the globaly defined class. I
had to use an init function to set the value,
and I couldn't give it a default value.

Hope this helps.

~{ zafa }~

Rostislav Siryk (not verified) Says:

Subfolders used in Commands folder

9 August, 2007 - 8:18am

This trick with omitted 'var' declaration just saved me a couple of days when I worked on the project with nearly a hundred of FLAs! I needed to batch process them with number of JSFLs to fix numerous errors in the sources. So I stored the list of processed files in the one JSFL file and then used it in others.

Joel, I noticed you use the "lib" sub folder in Commands folder. This approach is nice as soon as we get rig of trash heap in the main Commands folder. And there's old question I'm still trying to get answer on: how to make JSFL files in the Commands' sub-folders visible to the Flash IDE Commands menu? It this possible at all?

I use a lot of JSFL for automation of my current projects, and sometimes it is just very hard to find needed one in the huge Commands menu.

I've already wrote my feature request to Adobe, but I wonder are there any existing hack to work this out.