Declaring Variables

From HLKitWiki
Jump to: navigation, search

Context: HL KitKit Reference 

Variables can be declared to store data for subsequent use within the script. Variables have the same naming rules as unique ids: maximum of 10 characters, limited to alphanumeric characters and the underscore, and cannot start with a digit.

Variables can be either a number (tracked as a floating point value internally) or a string of text. String variables have a maximum size limitation of 5000 characters, which should be more than adequate for any practical purpose.

Variables are declared with the syntax "var name as type", where "name" is the variable name to declare and "type" is the variable type. Valid values for the type are: "string" for strings and "number" for numeric values.

Examples of legal variable declarations are shown below:

var temp as number
var Word as string
var IN_CAPS as number

Examples of illegal or unwise variable declarations are shown here:

var name_too_long as number (over 10 character limit)
var one.two as string (illegal character used)
var 123456x as number (legal but unwise - starts with a digit)
var NUMBER as number (legal but unwise - don't rely on case to distinguish)

Within a script, variables are simply referenced using their name. Once a specific variable is declared to be of a particular type, it cannot be declared as another type. However, it is valid for the variable "foo" to be declared multiple times with the same type, as long as subsequent declarations are of the same type. If the variable already exists with the same type, the new declaration is simply ignored. This means that the following code is perfectly legal (though redundant):

var foo as number
var foo as number

However, the next block of code is not legal:

var foo as number
var foo as string

Every time a script is evaluated, it starts with a "clean slate". Therefore, values of variables do not persist from one invocation to the next. Once a variable is declared, it is initialized with a default value. For numeric variables, the default value is zero. For strings, the default value is an empty string ("").

Variables can be declared at any time within a script. The only requirement is that a variable must be declared before it is actually used. It is perfectly valid to declare a variable immediately before it is first used.