TGML Script Context

A Script context can be seen as a sandbox in which the script is executed. Script contexts can be either Local (access within the current Script element) or Global (access across all Script Elements within the same TGML-file). This scope is regulated with the "UseGlobalScripts"-attribute on the TGML element.

By default, the "UseGlobalScripts" attribute is "False" and it is omitted from the TGML element. Adding it to the TGML element and explicitly setting it to "False" has the same result. The script can only access variables within the current Script element. All variables and function pointers that are declared in a Script element are only accessible within that context:

Copy
<TGML UseGlobalScripts="False"/>

Changing the "UseGlobalScripts" attribute to "True" makes all Script elements use one global JavaScript context. This enables variables and function pointers to be shared between all the Script elements.

To make a variable or a function pointer global and shared between different Script elements it needs to be declared without "var" and "UseGlobalScripts" needs to be set to "True":

Copy
<TGML UseGlobalScripts="True"/>

Remarks

This will decrease load time and memory usage greatly in the HTML5 graphics viewer. It is highly recommended to switch to True. You should modify your existing scripts if they unintentionally leak local values. The Statistics view enables you to see your current global variables and verify that they are not defined as a standard property in a Web browser by clicking on the Validate button.