Variable Scope

<< Click to Display Table of Contents >>

Manual > Script Guide > Script Ver2 (SC2 syntax) >

Variable Scope

What is a scope?

Scope refers to the range within which a variable is valid. Where you declare a variable in a script determines its scope.

 

Broadly speaking, scope can be divided into whether the target variable is within a range enclosed in brackets ("{" and "}") or not; this determines whether the variable can be accessed from script code (in scope) or not (out of scope).

 

Here, variables defined within a blanket are called "local variables".

Also, variables defined outside the scope of a blanket, that is, outside of any function or event, are called "global variables."

 

 

About local variables

A local variable is a variable declared within an event or user-defined function, and is only available within the scope enclosed in brackets ("{" and "}").

 

In the following case, the variable "a" is executed in the OnInitialize event, and when the event ends, "a" will no longer exist. Therefore, it is not possible to set or retrieve a value for "a" from an external object.

 

event OnInitialize()
{
	var a = "Hello"; 
}

 

In the following example, the variable "b" is declared inside the if statement. In this case, the scope is only inside the if statement, and "b" does not exist outside the if statement.

Therefore, a reference error will occur if it is outside the scope, even if it is within the same event.

 

event OnInitialize()
{
	var a;

	if (a == 1)
	{
		var b = 123;
		b = b + 1; // Valid
	}

	b = b + 5; // Error
}

 

As in the above example, variables declared within a scope are only valid within that scope. Also, you cannot declare duplicate variable names within a single scope.

 

 

hint

For more information about if statements, see "Conditional Branching/Repetition (Statement)".

 

 

 

About Global Variables

Any variable declared outside of an event or user-defined function blanket is a global variable, a variable that can be accessed by other objects.

 

Example 1) Example of declaring a global variable in the Control1 script of Form01

 

var a = 1; // Declaration of global variable

event OnMouseUp(button)
{
	::OpenMsg(a) // Displays 1
}

 

To access a global variable from another object, write it as follows:

 

 

Example 2) When accessing the global variable in Example 1 above from the script in Control2 of Form01

 

event OnMouseUp(button)
{
	parent.Control1.a = 123; // Set 123 to the global variable a of Control1
}

 

 

Example 3) When accessing the global variable in Example 1 above from a script in Form01

 

event OnMouseUp(button)
{
	this.Control1.a = 123; 
}

 

 

Example 4) When accessing the global variable in Example 1 above from a script in the root object

 

event OnMouseUp(button)
{
	::Form01.Control1.a = 123;
}

 

 

hint

When you declare a global variable for a control or form, keep in mind that when the form or control for which the global variable is defined is closed and no longer exists, the value of the global variable is lost, and when you display the form again, its previous value is lost.

If you want to keep a value in a variable regardless of whether the form is displayed or not, declare a global variable in the root object or background form. The root object will continue to exist as long as Panel Browser is not closed or another settings file is not reopened, so the value will continue to be held. If you want to keep the value even if Panel Browser is closed or another settings file is reopened, use the Panel Server memory device tag or something similar to hold the value on the server side.