Magic Variables: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(added thisTrigger, _forEachIndex, _x for count)
No edit summary
Line 7: Line 7:


*The most frequently used magic variable probably is <tt>_[[this]]</tt>, which is used to make arguments of a script call ([[call]], [[exec]], [[execVM]], [[spawn]]) visible and accessible to the script.
*The most frequently used magic variable probably is <tt>_[[this]]</tt>, which is used to make arguments of a script call ([[call]], [[exec]], [[execVM]], [[spawn]]) visible and accessible to the script.
*Other uses of magic variables can be seen in [[forEach]], where the current element can be accessed via <tt>_x</tt> and the current index with <tt>_forEachIndex</tt>.
*Other uses of magic variables can be seen in [[forEach]], where the current element can be accessed via <tt>[[_x]]</tt> and the current index with <tt>[[_forEachIndex]]</tt>.
*In the condition of the extended syntax of [[count]] variable <tt>_x</tt> refers to the current tested element.
*In the condition of the extended syntax of [[count]] variable <tt>_x</tt> refers to the current tested element.
*[[Triggers]] also define <tt>[[thisList]]</tt> and <tt>[[thisTrigger]]</tt>, which is however only accessible within the activation or deactivation script.
*[[Triggers]] also define <tt>[[thisList]]</tt> and <tt>[[thisTrigger]]</tt>, which is however only accessible within the activation or deactivation script.
*[[Exception handling]] using a [[try]]..[[catch]] statement usually declare a magic variable named <tt>_exception</tt>, which contains details about the exception [[throw]]n in the try-block.
*[[Exception handling]] using a [[try]]..[[catch]] statement usually declare a magic variable named <tt>_exception</tt>, which contains details about the exception [[throw]]n in the try-block.

Revision as of 16:44, 20 September 2014

Template:Stub

Introduction

Magic variables are engine maintained variables that serve a specific purpose within a given scope and are often used by the engine to push data to a given script or code fragment. They are usually no longer present after leaving the scope (e.g. after code execution, or after a forEach statement).

Uses

  • The most frequently used magic variable probably is _this, which is used to make arguments of a script call (call, exec, execVM, spawn) visible and accessible to the script.
  • Other uses of magic variables can be seen in forEach, where the current element can be accessed via _x and the current index with _forEachIndex.
  • In the condition of the extended syntax of count variable _x refers to the current tested element.
  • Triggers also define thisList and thisTrigger, which is however only accessible within the activation or deactivation script.
  • Exception handling using a try..catch statement usually declare a magic variable named _exception, which contains details about the exception thrown in the try-block.