Calling Script Functions

By calling a script function you will execute that function's code. The calling script may pass parameter values to the function and receive a return value from the function.

Calling a local function

If the function to be called was declared in the same script as the calling script it this is considered a "local call". The function can be called directly by its function name only.

Example:

func TestFunction()
{
  Log("The test function has been called!");
}

func MyCall()
{
  TestFunction();
}
When function MyCall() is executed, it will also call the function TestFunction() which will output a message to the log.

Calling a function in another object

To call a function in another object you have to specify that object first, then the function name.

Example:

Script Object A (ID TestObjectA):
func Activate()
{
  Log("Activate has been called in Object A!");
  Explode(20);
}
Script Object B:
func Activate()
{
  var obj = FindObject(Find_Id(TestObjectA), Sort_Distance());
  obj->Activate();
}	
The function Activate() in the script of object B will first search for the closest object of type A (definition ID TestObjectA) and store a reference to this object in the variable "obj". Then the function Activate() is called in the script of object A. To do this we first specifiy the variable containing the pointer to the object, followed by an arrow, then the function name including the parameter list (no parameters in this case).
Calling Activate() in object B will cause the closest object of type A to be blown up. You may consider this a very primitive type of remote control. By the way, this script will cause an error if no object of type A can be found. To prevent this, first check whether obj is not nil before calling Activate().

Remarks

You don't have to store the object pointer in a variable as done in this example. You could also continue calling the function directly on the search result as in this case:
func Activate()
{
  FindObject(Find_Id(TestObjectA), Sort_Distance())->Activate();
}

Indirect call of script functions

In some cases you may want somebody else to call a script function that you have specified. The engine function Call can do such "indirect" calls. You simply pass the function name of the function to be called as string.

Example:

func TestFunction()
{
  Log("The test-function has been called!");
}
	
func MyCall()
{
  Call("TestFunction");
}
With indirect calls you can "decide" at runtime which function to call as is done in the following example:
func TestFunction()
{
  Call(Format("MyCall%d", Random(3)));
}

func MyCall0() { Log("Die erste Funktion wurde aufgerufen!"); }
func MyCall1() { Log("Die zweite Funktion wurde aufgerufen!"); }
func MyCall2() { Log("Die dritte Funktion wurde aufgerufen!"); }
In TestFunction() a random function is called (using Random and Format one of the three strings "MyCall1", "MyCall2", and "MyCall3" is randomly composed and passed to Call).
This process has to be done with indirect calls as only at runtime we can know which function is to be called.
There are also some other methods of indirect calls. See GameCall.
PeterW, 2003-05
G√ľnther, 2010-08