Functions

Functions

Each script set comes with a large number of built-in functions, including most of the standard-library functions, though with capital letters to maintain consistent styling. Functions are called using the typical C syntax “<function name> ( <argument list> )”. For example,

char * pcDefinitionOfAwesome = "L. Spiro";
int iLength = StrLen( pcDefinitionOfAwesome );

would set iLength to 8.

Some functions have a variable number of arguments, as denoted by a ... parameter definition. For example:

PrintF( "%u of your base are belong to %s.", i, pcDefinitionOfAwesome );

These follow the same rules as in standard C/C++. Custom script functions, however, can not be defined this way. See Script Function Reference for a complete list of functions.

 

Custom Functions

Custom functions can be defined with the regular C syntax “<return type> <function name> ( <argument_list> ) { <statements> }”. The return type can be void or any of the supported built-in data types. For example:

void OutputInt( int d ) {
    PrintF( "%d\n", d );
}
OutputInt( 5 );    // Call the custom function, which will then print 5.

Arguments are usually passed by value, but can be passed by reference using the & (address of) operator before the argument name. Array arguments can be indicated using the array operator [] after the argument name. Array arguments are always passed by reference, just as they are in C/C++.

Full recursion is supported on custom functions.

 

Synchronized Functions

Synchronized functions are functions that can only be executed by one thread at a time. If a script thread is inside a synchronized function and another script thread calls that, or any other synchronized function, the second script thread will be forced to wait until the first thread leaves the function. This allows the user to implement thread-safe code. To create a synchronized function, prefix the function declaration with the keyword synchronized. For example:

synchronized void OutputInt( int d ) {
    PrintF( "%d\n", d );
}

Note that normal threading issues and threats apply. Specifically, deadlocks can occur if two threads are waiting on each other to leave synchronized functions.

Only one synchronized function can be called at a time in a script set. This means that if thread A is inside a synchronized function, and thread B wants to enter a synchronized function, thread B must wait even if it is not calling the same synchronized function as thread A.

Copyright © 2006 Shawn (L. Spiro) Wilcoxen