Global and local variables

 
 
 

Examples of Global Variable Use

Below are some code examples that use global variables.

Example 1

The following code uses the $globalText global variable in the myProcedure procedure; the globalText global variable was defined outside of procedures:

global string $globalText;
global proc myProcedure()
{
global string $globalText;
print($globalText);
} 

Example 2

In this example, MEL makes a window that can be used to create more child windows. The $chosenOne global variable tracks which child window the user has selected. Within each newly created child window, there is a chooseMe button that the you can click to set that specific window as the $chosenOne. When you click Delete Chosen Window in the main, parent window, the $chosenOne gets deleted.

In the image below, window4 is the $chosenOne, and will be deleted when Delete Chosen Window is clicked.

The code is as follows:

global proc int pressMe(string $thisOne) { 
	global string $chosenOne;
	print ("was " + $chosenOne + "\n");
	$chosenOne = $thisOne;
	print ("nowIs " + $chosenOne + "\n");
	return 1; 
} ;
global proc int satWindow(){
             string $temp = `window -width 150`;
             button -label "Choose Me" -command ("pressMe " + $temp);
             showWindow;
             return 1;
}
global proc int baseWindow() { 
	global string $chosenOne;
	
	window -width 150;
	columnLayout -adjustableColumn true;
	button -label "Make another Window" -command "satWindow";
	button -label "Delete Chosen Window" -command "deleteUI -window $chosenOne";
	
	showWindow;
	return 1; 
} 
baseWindow()