Creation/Dev/GScript/Constants: Difference between revisions
(Undo revision 8675 by Special:Contributions/Anti-Up (User talk:Anti-Up)) |
No edit summary |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
As one might be able to deduce, a '''constant''' has a single value and will retain that value as long as that constant exists. A constant is ''not'' a variable for one very important reason: | [[Category:Scripting Reference]] | ||
As one might be able to deduce, a '''constant''' has a single value and will retain that value as long as that constant exists. A constant is ''not'' a variable for one very important reason: its value is not variable! | |||
A constant is only accessible in the script where you have defined it, they | |||
are not shared between the server-side and client-side parts either. | |||
The const keyword is more of a directive than a declaration; within a script the identifier (or in C-parlance, lvalue) in the const directive is replaced throughout the script with the value (or rvalue). This leads to limitations on what the value can be, e.g., only a string or number literal can be used as the replacement value. | |||
== Constants defined by keyword 'const' == | |||
const intVar = temp.val; // will not work, temp.val is not constant | |||
const intVar = 3; | const intVar = 3; | ||
intVar = 4; // will not work | intVar = 4; // will not work | ||
player.chat = intVar; // displays 3 | player.chat = intVar; // displays 3 | ||
The value of the constant cannot change even in conjunction with the use of the "=" operator. This is the power of a constant. However, player.variables cannot be defined as a constant (client(r).variables, too, possibly). | The value of the constant cannot change even in conjunction with the use of the "=" operator. This is the power of a constant. However, player.variables cannot be defined as a constant (client(r).variables, too, possibly). Const only allows literals when assigning values. | ||
A constant may be used most likely to keep a variable that is available to any script from changing its value due to any foreign script interfering. It can also be used to give some cryptic value an easy to understand name, and by putting the const definition at the beginning of the script it can easily be configured. | A constant may be used most likely to keep a variable that is available to any script from changing its value due to any foreign script interfering. It can also be used to give some cryptic value an easy to understand name, and by putting the const definition at the beginning of the script it can easily be configured. | ||
== Enumeration == | |||
A special type of constants are enumerators: | A special type of constants are enumerators: | ||
Line 35: | Line 43: | ||
}; | }; | ||
You can assign values to the constants like you do for normal constants. It will automatically enumerate the following entries by adding one to the last value, so enum11 will be 11. You can also assign a | You can assign values to the constants like you do for normal constants. It will automatically enumerate the following entries by adding one to the last value, so enum11 will be 11. You can also assign a string value. |
Latest revision as of 14:42, 5 September 2011
As one might be able to deduce, a constant has a single value and will retain that value as long as that constant exists. A constant is not a variable for one very important reason: its value is not variable!
A constant is only accessible in the script where you have defined it, they are not shared between the server-side and client-side parts either.
The const keyword is more of a directive than a declaration; within a script the identifier (or in C-parlance, lvalue) in the const directive is replaced throughout the script with the value (or rvalue). This leads to limitations on what the value can be, e.g., only a string or number literal can be used as the replacement value.
Constants defined by keyword 'const'
const intVar = temp.val; // will not work, temp.val is not constant const intVar = 3; intVar = 4; // will not work player.chat = intVar; // displays 3
The value of the constant cannot change even in conjunction with the use of the "=" operator. This is the power of a constant. However, player.variables cannot be defined as a constant (client(r).variables, too, possibly). Const only allows literals when assigning values.
A constant may be used most likely to keep a variable that is available to any script from changing its value due to any foreign script interfering. It can also be used to give some cryptic value an easy to understand name, and by putting the const definition at the beginning of the script it can easily be configured.
Enumeration
A special type of constants are enumerators:
enum { enum0, enum1, enum2 };
When you put names in an enumerator list, then it is automatically generating constants by enumarating the names - enum0 will be zero, enum1 will be 1, enum2 will be 2.
enum modes { IDLE, WALK };
By giving a name to the enumerator list you can group enumerations - you can access the constants by modes.IDLE and modes.WALK.
enum { enum10 = 10, enum11, enumstr = "string" };
You can assign values to the constants like you do for normal constants. It will automatically enumerate the following entries by adding one to the last value, so enum11 will be 11. You can also assign a string value.