Skip to main content
Byron Nash - INSP
Inspiring
August 23, 2022
Question

How to handle global variables for scriptUI scripts

  • August 23, 2022
  • 1 reply
  • 563 views

I'm working on a script that will eventually be a dockable panel in AE. I'm used to working on scripts that don't have much UI so I'm getting my head around how to handle the persistent nature of a docked script. If my code is all in functions for the creation of the dialog and the running of various components of the script, how do I handle variables that need to exist on a global scope? For example, I need to have an array defined at some point that one function may need to have access to at different times. But if I define it in one of my other functions, it's not accessible to the function that needs it. However that function runs repeatedly throughout the process so it would also not work to have that variable defined in that function. Is the only way to add more values that get passed into the function from outside?

This topic has been closed for replies.

1 reply

Dan Ebberts
Community Expert
Community Expert
August 23, 2022

It's probably not the best practice, but I generally wrap everything in an outer function (with a unique name so as to minimize cluttering of AE's name space) and just inside that function I declare a global object for things that need to be accessible to all the UI callback functions or just need to be easily maintainable:

 

function myOuterFunction(theObj){

	// globals

 	var G = new Object();
	G.scriptName = "myScript";
	G.version = "1.0";
	G.uiPal = null;  // UI palette object ends up here
	G.helpText = "Blah, blah, blah...";
	// etc ...

}
myOuterFunction(this);

 

Byron Nash - INSP
Inspiring
August 23, 2022

I think I understand the concept of placing those high level variables at the top level. However, let say I have a dialog that is created with a function. Then another function that is run when a button is clicked. I'm getting tripped up by the scope. I can't put the variables outside the run function because it needs to generate those each time it's run. But then the supporting functions can't access the variables if I tuck those into the run function. Here's a rough overview...

function outerFunction(){
    //top level variables here

    function dialogCreation(){
    //I need to collect some information from the dialog to pass to runTheScript()
    }

    function runTheScript(){
    //main bulk of work is here using data from the dialog and calling supportFunction() 
       as needed
    }

    function supportFunction(){
    //may need to access variables defined in any of the functions
    }
}

 

Dan Ebberts
Community Expert
Community Expert
August 23, 2022

>I can't put the variables outside the run function because it needs to generate those each time it's run.

I'm having trouble understanding what the issue would be with using variables defined at the top level. Can you give an example?