Creation/Dev/GScript/Constants: Difference between revisions

From Graal Bible
m (Sections)
m (Spelling)
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: it's value is not variable!
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
A constant is only accessible in the script where you have defined it, they

Revision as of 17:26, 7 August 2009

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.

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.