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 TestFunktion()
{
  Log("Die Test-Funktion wurde aufgerufen!");
}

func Aufruf()
{
  TestFunktion();
}
When function Aufruf() is executed, it will also call the function TestFunktion() 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 AAAA):
func Activate()
{
  Log("Activate wurde in Objekt A aufgerufen!");
  Explode(20);
}
Script Object B:
func Activate()
{
  var obj = FindObject(AAAA, -1, -1, -1, -1);
  obj->Activate();
}	
The function Activate() in the script of object B will first search for the closest object of type A (definition ID AAAA) and store a pointer 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 operator, 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.

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(AAAA, -1, -1, -1, -1)->Activate();
}
To make sure that an object function is called from a given definition script you can also specify the definition's ID just before the function name. In some cases this may be necessary to resolve ambiguities if functions of the same name override each other.
func Activate()
{
  var obj = FindObject(AAAA, -1, -1, -1, -1);
  obj->AAAA::Activate();
}

Calling global functions without object or definition context

Since LC [336] it is possible to call globally defined functions without any context using global->Function(), provided #strict 3 or higher is enabled. global->~ is possible as well. Though, it is important to note that there must not be any space between "global" and "->". With this it is for example possible to call functions which expect local coordinates with global coordinates, without the need of first subtracting the object's own coordinates.
The following script creates a flint at the global coordinates 10/10 even if it is executed in an object context:
global->CreateObject(FLNT, 10, 10, NO_OWNER);

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 TestFunktion()
{
  Log("Die Test-Funktion wurde aufgerufen!");
}

func Aufruf()
{
  Call("TestFunktion");
}
Usually, indirect calls are a bit slower than direct calls, since they cannot be precompiled and the function will have to be searched by name at runtime.
On the other hand, with indirect calls you can "decide" at runtime which function to call as is done in the following example:
func TestFunktion()
{
  Call(Format("Aufruf%d", Random(3)));
}

func Aufruf0() { Log("Die erste Funktion wurde aufgerufen!"); }
func Aufruf1() { Log("Die zweite Funktion wurde aufgerufen!"); }
func Aufruf2() { Log("Die dritte Funktion wurde aufgerufen!"); }
In TestFunktion() a random function is called (using Random and Format one of the three strings "Aufruf1", "Aufruf2", and "Aufruf3" 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 ObjectCall and GameCall. Also see PrivateCall and ProtectedCall. They allow for bypassing the calling permission and only exist as indirect calls.
PeterW, Mai 2003