You can create as many custom functions as you wish and save them under one script file name. Here you will see hhow to do that,
Multiple Functions Example
The folllowing program shows the creation of four custom functions: Add( ), Subtract( ), Multiply ( ), and Hyp( ). It is saved as ta_mymath.
ta_mymath
int Add(int V1, int V2)
{
int Sum;
Sum= V1+v2;
return Sum;
}
int Subtract(int V1, int V2)
{
int Difference;
Difference= V1- V2;
return Difference;
}
int Multiply(int V1, int V2)
{
int Product;
Product= V1*V2;
return Product;
}
float Hyp(float V1, Float V2)
{
float Result;
Result= sqrt(V1*V1 + V2*V2);
return Result;
}
The above program compiles without errors. Here are some facts about the program:
The first three custom functiosn are of type int.
The last custom function is of type float.
All of the functions use the reserved word: return. This directs the program to return a value to the calling program.
The first custom function adds two integers and returns the result.
The second custom function subtracts two integers and returns the result.
The third custom function multiplies two integers and returns the result.
The last custom function takes the square root (using sqrt, a built-in NWN2 function) of the sum of the square of two numbers and returns the result.
Calling the Custom Functions
THe following program calls the above custom functions:
#include "ta_mymath"
void main()
{
int Answer1;
int Answer2;
int Answer3;
float Answer4;
Answer1=Add(5, 3);
Answer2=Subtract(5, 3);
Answer3=Multiply(5, 3);
Answer4=Hyp(3.0, 4.0);
}
The above program compiles without errors. The program produces the following results:
The variable Answer1 contains the value of 8 (5+3=8)
The variable Answer2 contains the value of 2 (5-3=2)
The variable Answer3 contains the value of 15 (5X3=15)
The variable Answer4 (of type float) contains a value of 5 (sqr(3X3+4X4)=sqr(9+16)=sqr(25)=5)
The argument for this last function require values of type float, therefore the values passed to thie function are floating point: 3.0 and 4.0.
There is no theoretical limit to the number of custom functions you can have in any one script.
Custom functions may return any legal NWN2 data type.
Where do we go from here?
THe next section introduces method of creating custom functiosn that simplify built-in NWN2 functions.
Nwn2Scripting provides material for training only. We do not warrant the correctness of its contents.
The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 2008 by Adamson House, Ltd. All Rights Reserved.