VAR
VAR <variable> = <expression>
Define a variable
Please also see Defining Variables.
Arguments
-
<variable>
- A variable name
-
-
<expression>
- A function or other Expression which returns a value
-
Description
The VAR command is a short-hand way of defining a variable. The variable's type is implied by whatever is assigned to it at creation, and then that type becomes its permanent type. As with any variable, the type can never change.
For example, a standard (non-VAR) way of defining a variable is as follows:
bob as NodeRef of Class Thingy = othernoderef
If, however, othernoderef
was already of class Thingy
, then bob
could be defined with the VAR command:
var bob = othernoderef
This would then create bob
, which would automatically be of the type NodeRef of Class Thingy
. This results in less typing. Another major advantage is that if the noderef's type is changed at some point upstream (such as to a different class), then the bob
variable type automatically adapts, without having to modify any variable definitions in the script -- it just needs to be recompiled.
Another very useful aspect of the VAR technique, is when setting a variable equal to a field of a node. This way, if the field's data type is later changed, the associated script variable will automatically change to match the same type, the next time that the script is compiled.
VAR n = returnNode()
Notes:
- Do not confuse VAR with "variant"; this is just a compile-time shorthand way of defining a variable that is still strongly typed and whose type does not change at run-time.
- Defining a noderef does *not* create a node. It only defines a reference to an existing node. To create a node, it is necessary to use a special function such as
CreateNodeFromClass(<class>)
. Please see the section on Built-in_Functions for more information. Even then, a node can have any number of classes on it, though the first one it is created as is its "base class".
Examples
var backpackref = CreateNodeFromClass("container") var a = 7 // declares a variable named "a" of type int var b = 3.5 // declares a variable named "b" of type float var n = GUIref17 n.visible = true var name = "bob" var endTime = SYSTEM.TIME.NOW + 0:0:5 var temp = me.exampleField var vis = playerutils:GetMyVisibleCharacter(playerutils:GetMyChar(me)) // same as vis as NodeRef of Class _CharacterAppearance = ... newNode as noderef of class thingie = ReturnNode() vis = newNode // Will generate an error if the node pointed to by newNode is not of class _CharacterAppearance
For more information, please see the section on Defining Variables.