A function is the top-level grouping of instructions in a script. Instructions must exist inside functions, and no function may be inside another. Functions may, however, invoke other functions by name. Some functions take parameters, and some return a value to the caller. Their use is similar to that of commands, but commands are pre-defined and implemented outside NWScript.

Execution of most scripts begins with the specially-named function main(), which takes no parameters and returns no value. The only exceptions are the scripts used to determine which lines of dialog appear in a conversation. These scripts begin execution with the specially-named function StartingConditional(), which takes no parameters and returns an integer.

A function definition begins with a specification of its interface (return value, function name, and parameters, in that order), followed by an open curly brace, followed by the instructions that comprise the function, followed by a closing curly brace.

// A sample function definition.
void FunctionName(int nParameter1, object oParameter2)
    // Instructions go here.

A function is then invoked by using its name and supplying whatever parameters are needed. (If no parameters are needed, the function name must still be followed by a pair of parentheses.)

    // Invoking the above function.
    FunctionName(1, OBJECT_SELF);

However, a function can only be invoked if it is currently "known". A function becomes known starting with the opening curly brace in its definition. If a function needs to be known earlier, it can be declared. A function declaration starts the same as a function definition, but instead of instructions inside curly braces, there is a semicolon.

// A sample function declaration, which would come before the above definition.
void FunctionName(int nParameter1, object oParameter2);

One advantage of declaring a function is that the Toolset's script editor will then know the function, so double-clicking the function name will cause the declaration to appear in the editor's lower window. Any comments immediately preceding the declaration will also be displayed, making this a convenient way to check the usage of a function.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.