The $ Symbol, Entities and You

Any time you want to reference an entity in script, you need to use it's handle. Handles can be accessed directly, via the $ operator, or through entity variables. Check out the examples

Using $ to grab handles

Use a dollar sign $ before the name of an entity to tell the scripting system that this is an entity. If you don’t use the $, the game won’t know what you’re talking about.

   1 
   2  //open the door and show the monster
   3  $door_100.open();
   4  $monster_grunt_leftroom.show();
   5 

/!\ Again, If you're going to reference the objects by map name, you must use the $ operator. The following example won't work.

   1 
   2  //this line will cause the script to not compile.
   3  door_100.open();
   4 

If you reference something in script that isn't a variable, syntax, or a map entity preceded by the $ operator, you'll get an unknown value error.

Storing entity handles in variables

Another way to reference entities in script is via the entity variable. This allows you to write all purpose functions that get around hard coding. If you're familiar with C++, think of them as pointers. Otherwise, check out our example below.

   1 
   2  entity crazyDoor;
   3 
   4  //Put the handle from door_100 into the crazyDoor variable.
   5  crazyDoor = $door_100;
   6 
   7  //both of these calls affect the same entity!
   8 
   9  //notice-- no $ sign
  10  crazyDoor.open();
  11  sys.wait(5);
  12 
  13  //return of $
  14  $door_100.close();
  15 

Why would we do this? Let imagine we want to create a function that opens a door and reveals a monster. Let's also say you're going to do this multiple times in your map, so instead of scripting it over and over again, you'll write a function for it.

   1 
   2 //this will open a door and reveal a monster!
   3 void ohNoMonsterCloset( entity theDoor, entity theMonster ) {
   4  
   5  theDoor.open();
   6  theMonster.show();
   7 
   8 }
   9 
  10 //now, in the rest of your script, you can toss this around:
  11 void sendInTheBeasts()  {
  12 
  13  ohNoMonsterCloset( $door1, $monster_grunt_1);
  14  ohNoMonsterCloset( $door_left, $monster_gladiator_left );
  15  ohNoMonsterCloset( $backdoor, $cheapmonster );
  16 
  17 }

Learn more about writing your own functions.

Tutorial ScriptEntities (last edited 2005-11-02 20:36:21 by JimShepard)