localizing variables in script UI

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
cardeiro
Posts: 31
Joined: March 27th, 2006, 2:03 pm
Location: philadelphia, PA
Contact:

Hi

I have just started playing around with script ui to make dockable panels. The biggest problem I see is the fact that variables in scripts are global to all scripts.

My scripts all have a 'VERSION' and 'scriptName' variable; which is fine when run using file->scripts, the problem is with script ui if I load script a.jsx with var scriptName = 'a' ; then I load script b.jsx with var scriptName = 'b'; when script a.jsx reads the scriptName variable it is 'b', but it should be 'a'.

is there a way to localize variables or are we stuck with globals (globals can get very messy, it is nice that you can share functions and stuff, but I really think that this should not be the default, you should have to work to make variables global...or am I missing something?)
Mike Cardeiro
------------------------------
Animator - Designer
D4 Creative Group
Philadelphia, PA
------------------------------
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Hey Mike

What I do on advice from Jeff Almasol when I asked the same question was to create an object to store global variables and make the object name unique to each script.

Code: Select all

var myScript_Data = new Object ();

myScript_Data.version = 1.0;
myScript_Data.scriptName = "myScript";
Lloyd
cardeiro
Posts: 31
Joined: March 27th, 2006, 2:03 pm
Location: philadelphia, PA
Contact:

Hey Lloyd,

I was trying to avoid doing this as I have functions that say look at the variable VERSION in conjunction with scriptName to see if there's an update.

I could feed the function an object, but its just cleaner (for me) having the function look at these variables (that need to be global to the script but not After Effects).

The bigger issue to me is that I may have a function parseData in my script, and you may have a function of the same name in your script that works completely differently. If somebody where to have both scripts open in panels at the same time, one of them is not going to work properly.

There needs to be a way to localize functions and variables. Having the ability to share functions and variables between scripts is very powerful, but there also needs to be a way to localize them...in fact, this should be the default. Making variables and functions available to other scripts should be something you declare in the script, as I think most people assume their script is an island (even though its in the manual).
Mike Cardeiro
------------------------------
Animator - Designer
D4 Creative Group
Philadelphia, PA
------------------------------
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Hey Mike,

You can do exactly what you describe by doing what I was saying. Just declare the object in the main body of the script and it will be a global variable to your script. No need to pass it explicitly to your functions. Of course it will be a global variable across all scriptUI scripts like you describe but by making the object name unique you avoid the problems you describe. Functions can also be part of the object:

Code: Select all

var myScript_Data = new Object();

myScript_Data.version = 1.0;
myScript_Data.scriptName = "myScript";


myScript_Data.addVersion = function (myVar) {
    return myVar+myScript_Data.version;
    }

alert (myScript_Data.addVersion (2) );

One thing Nab does to shorten the amount of typing is he makes his global object "this". Then he just uses "this" to define properties of the object:


Code: Select all

var myScript_Data = this;

this.version = 1.0;
this.scriptName = "myScript";


this.addVersion = function (myVar) {
    return myVar+this.version;
    }

alert (this.addVersion (2) );
I recently picked up the Definitive Javascript Book and I learned alot of stuff like this that I never really knew existed so I highly recommend it.

-Lloyd
cardeiro
Posts: 31
Joined: March 27th, 2006, 2:03 pm
Location: philadelphia, PA
Contact:

lloydalvarez wrote:...Of course it will be a global variable across all scriptUI scripts like you describe but by making the object name unique you avoid the problems you describe. Functions can also be part of the object:

Code: Select all

var myScript_Data = new Object();

myScript_Data.version = 1.0;
myScript_Data.scriptName = "myScript";


myScript_Data.addVersion = function (myVar) {
    return myVar+myScript_Data.version;
    }

alert (myScript_Data.addVersion (2) );
but this is exactly whay I am trying to avoid, now if I want to reuse the addVersion function in a different script, I need to change the name of the variable 2 times in this 2 line snippet

Code: Select all

myScript_Data.addVersion = function (myVar) {
    return myVar+myScript_Data.version;
}

It just seems like a lot of work to get it localised, and adding 'this.' before everything makes the code harder to read (at least for me).
Mike Cardeiro
------------------------------
Animator - Designer
D4 Creative Group
Philadelphia, PA
------------------------------
Post Reply