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

Functions

The NWN2 editor has hundreds of built-in programs called functions. A function is a part of the program that does a certain action. You can write your own functions and you can use the functions written by others.
In this section, you will discover how to use a function that was already made for you. In another section you will learn how to create your own functions.



A pre-made function
Below is an example of a pre-made function used in NWN2. When a function is displayed this way, it is called a function prototype. A function prototype tells us what value (if any) the function will return and what values (if any) it needs to do its job.


   void FloatingTextStringOnCreature(string txtDisplay, object oCreater);


Here are some facts about the above function:
•    The function starts with the void data type - This tells us it does not return any values.
•    The name of the function (its identifier) is: FloatingTextStringOnCreature
•    The parentheses following the function identifier is called the function argument. It identifies the variables, if any, that are needed by the function.
•    The function contains two variables in its argument, they are string txtDisplay and object oCreater
•    The variables inside the function argument are called the function parameters
•    In this example, two parameters must be passed to the function. The first must be of the data type string and the other must be of the data type object

The following program shows the function in use:


//This program causes text to appear over an entering creature.
void main( )
  {
    object oPC = GetEnteringObject();
    string Greeting = "Welcome to NWN2!";
    FloatingTextStringOnCreature(Greeting, oPC);
  }



The above program compiles without errors. Here is an analysis of the above program:

•    An opening comment tells us what the program will do.
•    After the void main( ), two declarations are mad: one is of type object and the other of type string. This must be done because the function prototype requires variables of this type to be passed to it.
•    Both declarations are given assignments. One is to assigned GetEnteringObject() (a built-in NWN2 object) and the other is assigned "Welcome to NWN2!" (a string which must be enclosed in double quotes).



Using the above program.

You can go to the NWN2 editor, compile the above program using the Program I module you created. Then get into the NWN2 game itself, call up the module and have vour PC walk over the area. You will see the string:



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.