Variables in Scripting

In your scripts, you will often want to store and modify data. Changing enemy health, adjusting rotation and movement values for objects, and displaying new information to players all involves the use of data. To do this, we will use variables.

A variable is a method of storing information. A little piece of memory you can fill with whatever you like. Here's an example:

   1 
   2  //we declare a variable here
   3  string messageForYou;
   4 
   5  //put some information into it
   6  messageForYou = "Hello my people!";
   7 
   8  //then do something with it.
   9  sys.println( messageForYou );
  10 

To declare a variable, you have to first enter it's type, followed by it's name.

float foo;

string cheese;

vector stroggMissileDirection;

The name is unimportant, you can use whatever you like. This will allow you to keep a cohesive naming scheme in your scripts.

There are four different variable types in script, the String, Float, Vector and Entity types. Take a look at them below.


String

Usage: Strings are used to hold lines of text. The word "string" comes from the concept of a string of single characters

 string dudeName;
 dudeName = $monster_guy.getName();

 string newName;
 newName = "Josef the Red";
 $monster_guy.setName( newName );


Float

Usage: Floats are used to hold numbers. The word "float" is derived from the concept of the floating point number. Floats can hold decimal numbers as well as whole numbers.

 float myNumber;
 myNumber = $monster_guy.getHealth();
 myNumber = myNumber * 10;
 //power up
 $monster_guy.setHealth( myNumber );

   1 
   2  string monsterName;
   3  float monsterNumber;
   4  
   5  monsterNumber = getRandomMonster();
   6  
   7  // here the number is converted to a string for you automatically.
   8  monsterName = "uncrushable_boss_" + monsterNumber;
   9 
  10  // so, if the number was 67, monsterName would be "uncrushable_boss_67"
  11 

   1  
   2  string numberOne = "1";
   3 
   4  //no.
   5  if( numberOne == 1 ) { ... 
   6 


Vector

Usage: A vector is a collection of three floats, organized into an x, y, and z format. They are primarily used for storing locations, or determining directions.

/!\ Access to a vector's three values is gained by using the _x, _y or _z operator. Not the . dot operator.

   1 
   2  vector newMotion;
   3 
   4  //set the z value to be positive so the unit moves up.
   5  newMotion_z = 300;
   6 
   7  $mover_rocket.setLinearVelocity( newMotion );
   8 

   1 
   2  vector vFail;
   3 
   4  //neither of these compile.
   5  vFail = 10;
   6  sys.println("I am at location " + vFail);
   7 


Entity

Usage: Entity variables are used to store handles to entities. When an entity is spawned via script, or referenced via entity name, you gain access to his handle.

   1 
   2  entity monster1;
   3  entity monster2;
   4 
   5  monster1 = $monster_grunt_1;
   6  monster2 = sys.spawn("monster_gunner");
   7 

/!\ Check here for a more detailed example.

ScriptVariable (last edited 2005-11-02 23:23:04 by JimShepard)