Syntax for Script Files

Script files are written in a very specific way. There are rules to follow for creating the various parts of the file, and for controlling the logic. This rule set is called a syntax.

(i) If you are familiar with most programming languages, you won't find any big changes here. The script syntax is very similar to C / C++. You might want to check out the section on variables to see the changes there.

Operators

Basic Math Operations

*

/

+

-

++

--

   1 
   2   //float a;
   3   //float b;
   4 
   5   //assignment here. We place the values on the right into the variable on the left. 
   6   a = 2;
   7   b = a;        //now b is also equal to 2.
   8 
   9   //multiplication
  10   a = 10 * b;   //a is now 20.
  11 
  12   //division
  13   a = b / 5;    //now a is 4-- following along?
  14 
  15   //addition 
  16   a = a + 10;   //14
  17 
  18   //subtraction
  19   a = a - b;    //12
  20 
  21   //increment
  22   a++;          //++ raises a number's value by 1.
  23 
  24   //decrement
  25   a--;          //and -- does the opposite.
  26 
  27   //look further in the page for the example of 'if'.
  28   //but know here we're comparing a and b. We are not saying that a equals b, that would be =.
  29   if( a == b )  {
  30    sys.println("a and b are the same");
  31   } else {
  32    sys.println("a and b are different");
  33   }
  34 
  35 

Functions

A function is collection of script commands designed around a specific purpose. Usually they're built to do one thing that takes a bunch of smaller steps. You might write a function to activate all the monsters in a room, start up some machinery, or perform any small task.

[return type] [function name] ( [parameter list ]) {

\\function stuff goes here

}

The [return type] is one of the variable types the script system uses.

The [function name] can be any name you like, however it cannot contain any spaces.

The [parameter list] is a collection of variables you want to send into the function. If there are none, leave that section blank.

Example:

   1 
   2  void simpleFunction( float f ) {
   3  
   4   //double the number that was sent in and print it
   5   f = f * 2;
   6   sys.println("You sent in " + f + ", which when doubled is " + f;
   7 
   8  }
   9 
  10  void powerUpAmbush() {
  11 
  12   //open the doors
  13   $door_ambush_1.open();
  14   $door_ambush_2.open();
  15  
  16   //hit the lights
  17   sys.trigger( $light_ambush );
  18 
  19   //monsters!
  20   $monster_osnap.show();
  21   sys.trigger( $ambush_spawner );
  22 
  23  }

Calling functions

Functions can be executed, or called, from the map or from within the script. To call functions from a map, use an entity that supports the call keypair and assign it a function. Typically, triggers are what support the call function, though there are other cases. To call the simpleFunction example above, you could enable a trigger_once with the following keypair:

call   simpleFunction

Triggers that fire more than once will call their functions each time they fire. Be wary of this when you set up your triggers.

To have a function called from within script, see the AdvancedScriptTutorial.

Command Words

There are some command words in the script syntax that are used to perform various logic functions. Looping, decision making, running different function threads, and so on.

for

A for command, or for loop, creates a loop that executes a set number of times based on a condition

for ( [starting condition]; [condition]; [iterator] ) {

}

The starting condition is what happens when the for loop is first reached, it only happens once. The condition is what must be true for the loop to continue. the iterator is what happens at the end of each loop. Check out the example below:

   1 
   2  float doorCounter;
   3  entity tempDoor;
   4 
   5  for( doorCounter = 100; doorCounter < 106; doorCounter++ ){
   6  
   7   tempDoor = sys.getEntity( "intense_door_" + doorCounter );
   8   tempDoor.open();
   9 
  10   //mandatory waitFrame
  11   sys.waitFrame();
  12    
  13   //the loop ends here, so the iterator will execute now. In our case,
  14   //doorCounter will be incremented by 1.
  15  }
  16 

In that example, doors 100 through 105 will open. When doorCounter becomes 106, the loop will end because the condition has turned false.

if

The if statement creates the opportunity for decision making.

if ( [condition] ) {

} else {

}

Here is an example.

   1 
   2  if( numberOfSwitchesHit == 2 ) {
   3 
   4    $door_treasure.open();
   5 
   6  } else {
   7 
   8    sys.trigger( $speaker_fail );
   9 
  10  }
  11 

Note that we use the == comparison operator here, and not the = assignment operator.

while

The while is another loop, similar to the for loop but a bit simpler.

while( [condition] ) {

}

While the [condition] is true, the loop will continue.

   1 
   2 while( $monster_boss.getHealth() < 50 ) {
   3 
   4    sys.trigger( $explosion_backround_fx );
   5    sys.wait(8);
   6 
   7 }

ScriptSyntax (last edited 2005-11-04 21:21:27 by JimShepard)