By default, a Script element has a single primary output. However, it is possible to define additional outputs for the element (with their own attributes and dimensions) when defining local variables within a script.
There is an important difference in the behavior of the primary output and any other exposed outputs. The primary output of a Script element is a state variable. A state variable provides inertia or “memory” to a system because its value is computed based on the historical value of the element’s inputs (as opposed to only being a function of the current value of the element’s inputs). All state variables have, by definition, an initial value. This allows the output to be computed when there are no historical inputs available (e.g., at the start of a simulation).
This has an important implication for how the variables can be used in a script. In particular, the value of the primary output is “remembered” between updates of the element. As a result, in a script you could create a Variable Assignment such as “Result = ~Result + 1” as the very first statement. The expression would use the previous value of the Result when it was evaluated. This is not possible for any of the other script variables, since they require a Variable Definition statement which initializes the variable every time the Script element is updated.
If you would like additional outputs of the Script element to be able to “remember” their previous update (and hence behave like state variables with initial conditions), you can do so by referencing the Script element’s output in a Previous Value element outside of the Script element (and then referencing the Previous Value inside the script.
This very simple example illustrates how the Script element can be used in conjunction with a Previous Value element. It can be found in a file (Script.gsm) that is located in the General Examples folder of your GoldSim directory (accessed by selecting File | Open Example... from the main menu).
The script simply updates the primary output (Result) and a secondary variable (Exposed) every time the Script element is updated (in this case, every timestep). In order for the defined variable (Exposed) to be “remembered” between timesteps, it is stored in a Previous Value element, and it is that element that is referenced by the script):
As can be seen, the script references the Previous Value element in a loop:
Learn more about: