NWN2 Scripting.




Simulation Training.

Introduction
Home
First Program
Seeing Results
Variables and Data Types
Comments
Functions
Data Conversion
Random Numbers
Concatenation
Arithmetic Operators
Compound Assignments
One More or Less
Precedence
Relational Operators
The Open Branch
The Closed Branch
Logical AND
Logical OR
Compound Statements
ELSE If
Switch Case
The ? Operator
The for Loop
The while Loop
The do while Loop
Introduction to Functions
Passing Values
Passing Multiple Values
Multiple Functions
Simplifying Functions
TRUE/fALSE Conditions
Return Values
Setting Global Variables
Getting Global Variables
Setting Local Variables
Getting Local Variables
WayPoints Introduction
Static Waypoint Sets
Dynamic Waypoint Sets
Dynamic WP Cycles
Input Output

Introduction to Functions

Functions are sectiosn of a program which contain a compelte set of code and can be used over and over again by other programs. In your experience with teh NWN2 scripting editor you have already seen many functions created by the NWN2 folks. These are functions that contain compelx code that you never have to (hopefully) worry about. All you have to do is call (use) that function in aother program.

You can create your own functions and add them to the NWN2 library of existing functions. You can then use these custom made functions in other programs, saving you hour of programming time and frustration. This section is an introduction on how to create functions in the NWN2 scripting environment.


Creating a Function

The following program illustrates the creation of a custom function:


/******************************

Created by Me

May 8, 2007

Will Cause the text:

Hello NWN2 to appear over

characters head.

******************************/

 

void SayIt()

  {

   object oPC=GetEnteringObject();

     FloatingTextStringOnCreature("Hello NWN2!", oPC);

  }

 


 

The above funtion is saved as ta_speak (I begin all of my custom functions with my initials so they ar enot confused with the ones created by the NWN2 folks).

The custom function has the following.

  • The comments section which explains who created the function, when it was created, and what it will do. Not required, but good to have.
  • Void SayIt() - The function identifier with its declared data type followed by a pair of parenthesizes. You use any legal identifier and try to make it descriptive of what the function will do. This is required.
  • Between the { and } is the body of the custom function. This is what the function will do when it is used (called).

 


Using the Function (Function Calling)

The program below uses (calls) the above function.


#include "ta_speak"

void main()

 {

  SayIt();

  }

The above program has the following:

 

Code Meaning
# Requirred symbol in C++ to let the program know you are giving a pre-compiler directive. (Somethign to do before getting into the main part of the program.)
include C++ reserved word that tells the compiler to include the program as follows
"ta_speak" The name of the program which has the custom function used in this example. Note that it is enclsoed in quotation marks and it is NOT followed by the semicolon.
void main() The start of the program that iwl use your custom function.
{ Opening brace defines the beginning of the program body.
SayIt(); The called custom funtion. IT will cause the instructions you defined in the custom function to e executed.
} CLosing brace defines the end of the program body.

The above program will now cause the word "Hello NWN2!" to float over the Pcs head.


Conclusion

Creating your own custom functions is easy. It offers a powerful way of creating your own NWN2 library of code that fits your style of coding and specific applications or your module.


Where do we go from here?

THe next section on Functions shows how to make your custom functiosn even more powerful through the passing of parameters. These act like information messages to make your custom functoins preform different tasks.


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.

Questions or Comments: EMail Webmaster

Donations are to Adamson House, Ltd who maintains this site.
All donations go the the improvement of this site.