Creation/Dev/GScript/Constants

From Graal Bible
Revision as of 03:53, 1 July 2007 by Twinny (talk | contribs) (Undo revision 8675 by Special:Contributions/Anti-Up (User talk:Anti-Up))

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: it's value is not variable!

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).

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 is only accessible in the script where you have defined it, they are not shared between the server-side and client-side parts either.

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 sring value.