Assigning Values to Script Variables

The most fundamental of the Script element’s statements is the Variable Assignment, in which you assign a value (or condition) to a variable (typically in the form of a mathematical expression). 

Selecting “Variable Assignment” from the Script element’s Insert menu (or pressing Ctrl+A when in the Script dialog) inserts the statement in the script (below the statement that was selected when this is done), and brings up the following dialog:

It is first necessary to specify which variable is being assigned a value (the Variable ID).  By default, in a new script, there is only one variable that exists: the primary output.  Within a script, this primary output is always referenced as Result. Hence, for a new script, this is the only choice in the drop-list:

After selecting the Variable ID, GoldSim displays the attributes of the variable (in the example below, the attributes of the primary output of the Script element):

You then specify how the variable is assigned (i.e., in terms of an equation, you are defining the right-hand side of the equation).  The Definition field is the equivalent of the Equation field in an Expression element; you can enter any mathematical expression using all of the functions and operators you can use in other GoldSim input fields.

Three different kinds of variables can be referenced in this field:

1.  You can reference outputs that are external to the Script element;

2.  You can reference the primary output itself (i.e., Result); and/or

3.  You can reference other local variables that you define in the script.

   Note: If the Variable you are assigning is an array, the Definition portion of the dialog expands to provide some additional options for defining the array.

In the example below, two outputs that are external to the Script element (A and B) are referenced:

This single statement Script element (which simply adds two numbers) would then look like this:

   Note:  Outside of the Script element, the primary output (Result) would be referenced by the Script element’s name.  It is only referenced as Result inside the Script element.

In the example below, the primary output itself is referenced:

The Script element would then look like this:

In this simple Script element, every time the Script element is updated, it would increment the primary output.

Note that when Result is referenced in a statement (i.e., in the right-hand side of the equation), it is referenced using the ~ operator.  This is because Result is a locally available property. Locally available properties are only available in specific (local) parts of the model.  In this case, Result can only be referenced inside the Script element, and has no meaning outside the Script element.

When inserting a link while assigning a statement in a script, you will note that Result (and any other script variables that you may have defined) are accessible only under Available Properties, and are not listed in the same manner as other outputs:

Finally, in the example below, Result is defined in terms of a (previously defined) local script variable (Local2) and an output external to the Script element (A):

The statement in the Script element would then look like this:

(The next topic describes how local variables can be defined and added to a script).

Note that like the Result variable, when a local script variable is referenced in a statement, it must be referenced using the ~ operator, since it is a locally available property.

Of course, you can assign values to the local variables in your script (not just the primary output Result).  For example, in the example below, there are two previously defined local script variables (X and Local2), and the variable X is selected as the Variable ID for the statement:

It is then defined in terms of another local script variable (Local2) and an output external to the Script element (A):

The statement in the Script element would then look like this:

Related Topics…

Learn more about: