Scripting School
Would you like to react to this message? Create an account in a few clicks or log in to continue.

New to scripting? We want you!

Official Ventrilo: Host:Apollo.MaxFrag.net Port:3840
TestBed needs our help! Please donate a few dollars to keep it running here!
Have you mastered a specific part of GS2? Create a guide and help others!

You are not connected. Please login or register

B. Variables and Constants

Go down  Message [Page 1 of 1]

1B. Variables and Constants Empty B. Variables and Constants Tue May 10, 2011 7:49 pm

David K?



GS2 is different from many other programming languages, Graal Scripts variables are 'variant' and are objects that hold values of different types. When the value is assigned the variable assigns it to the right type.

Numeric (floating point / decimal)
Code:
var = 1;

String (text)
Code:
var = "stuff";

Object link
Code:
var = player;

Array (list of objects)
Code:
var = {1,"stuff",player};

var[0] = 1;
var[1] = "stuff";
var[2] = player;

Also, using the type() function "variable.type()" returns 0,1,2,3 for numeric, string, object, or array. This will give -1 for constants.



You can also put arrays into arrays.
Code:
var = {
  {1, 2, 3, 4, 5},
  {6, 7, 8, 9, 10},
  {11,12,13,14,15}
};

var[0][3] = 4;
var[1][1] = 7;
var[2] = 11,12,13,14,15;



There are also built in variables here:
http://www.graal.net/index.php/Creation/Dev/Script/Client








Constants are defined by the keyword 'const'

Code:
const intVar = temp.val;    //Bad
const intVar = 22;        //Good

Because temp.val is not constant it will not work.


Code:
intVar = 21;                //Bad
player.chat = intVar;      //Good
Because intVar is already a constant of 22 it won't change.

However, to use const intVar, you don't need the const in front of it any longer. Also, player.variables cannot be defined as a constant, nor can client(r).variables.

Constants can be used to keep a variable from changing due to foreign script interference. They can also be used to give cryptic values an easy to understand name by putting a const at the beginning of the script.







Enumerators are another 'special' type of constants.
Code:
enum {
  test1,
  test2,
  test5
};
When you put names in an enumerator list the name is automatically assigned 0, and each name after increases by 1.
test1 = 0;
test2 = 1;
test5 = 2;




Code:
enum testStuff {
  test1,
  test2,
  test5
};
By giving a name to the enumerator list you can group enumerations. This means in order to use enum testStuff, test1-test5 you'd have to use test. in front of it.
testStuff.test1 = 0;
testStuff.test2 = 1;
testStuff.test5 = 2;





Code:
enum {
  enum0  = 10,
  enum1,
  enum2 = "string",
  enum4
};
You can assign values to the constant like you do for normal constants. It will again add 1 to the next name. However, you can also add a string. Following the string it will resume flow of the adding 1.
enum0 = 10;
enum1 = 11;
enum2 = "string";
enum4 = 12;






http://www.graal.net/index.php/Creation/Dev/GScript/Constants
http://www.graal.net/index.php/Creation/Dev/Script/Starting_Guide#Standard_functions

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum