#include for jsfl
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
// Example class for demonstration purposes.
// Note: Changed per Robert Penner's comment below.
// The class is a global variable (no 'var').
//function Thing(gamma){
Thing = function(gamma){
fl.trace("Thing constructor");
this.gamma = gamma;
}
new Thing(0); // Create and discard an initial instance.
Thing.prototype.func = function(val){
return val * this.gamma;
}
Thing.staticFunc = function(val){
return val + 100;
}
// Hack is no longer needed per comment below.
// // Hack to make class visible outside this file.
// // 'Thing' will now be a global variable (no 'var')
// // that points to a class definition formerly called
// // 'Thing'.
//
// var tmp = Thing;
// delete Thing;
// Thing = tmp;
IncludeTest.jsfl
// Note: I put Thing.jsfl in a 'lib' subdirectory in Commands.
// This prevents my Command menu from getting cluttered.
fl.runScript( fl.configURI + "Commands/libs/Thing.jsfl" );
var myThing = new Thing(2.5);
fl.trace("myThing func result: " + myThing.func(7));
fl.trace("Thing staticFunc result: " + Thing.staticFunc(2));




Comments
A shorter approach
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!
to initialize
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 }~
I should have realized
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
what about an eval ?
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.