|   Declaring Script Variables | ![]() |
Basic DeclarationsA variable can be declared using the syntax “<data type> <variable name>;” Use an “=” to assign a value to the variable upon initialization. For example:
<data type> can be any built-in data type or a custom structure, union, enumeration, or typedef. See Data Types, Typedefs, Structs, Unions, and Enums for information on what is valid in a <data type>. Unlike in C, you can not use commas to create multiple variables at once. Variables declared outside of functions are globals. Globals can be declared in any order, and used from anywhere in the script, which is in contrast with C and C++, where globals must be declared before they are used. Variables declared inside functions are locals. Unlike in C, locals can be declared anywhere inside the function, however, like C, they must be declared before they can be used. Like in C and C++, you can declare structures at the same time that you declare the variable. See Data Types, Typedefs, Structs, Unions, and Enums for more information.
ConstantsConstants are values that do not change. They follow the same rules as above, however they can not be modified, and during compilation they are always simplified into their numeric constant values for optimization. To declare a constant value, prefix its declaration with the keyword const. For example:
Because PI will be simplified during compilation, typing “fValue *= PI;” would be exactly the same as typing “fValue *= 3.1415963f;”. Function parameters can be declared as constants, but this has absolutely no meaning to the compiler at all; it is just to inform the user that the value is not modified inside the function (even though the compiler does not enforce this at all). |
Copyright © 2006 Shawn (L. Spiro) Wilcoxen |