View Full Version : Introduction to Scripting Syntax

01-25-2005, 04:59 AM
Here is a very brief introduction into scripting syntax. For those of you who have programmed in C, you will find scripting syntax very familiar.

The primary game functions are all defined in nwscript.nss along with most of the useful game variables. You will often refer to nwscript.nss as you begin scripting.

If you haven't had any programming experience, scripting may look intimidating. Don't worry. KotOR provides numerous source files (.nss) within scripts.bif that you can look at for examples. After awhile, you will begin to see the patterns of how functions are called and how variables are used.

Variable declaration and intialization
Note the use of the equals sign and no paranthesis.

^ ^ ^
| | |-the value to which the variable is set
| |-the variable
|-the data type of the variable (integer)

Function declaration
Paranthesis present, equals sign absent

object CreateItemOnObject(string sItemTemplate, object oTarget, int nStackSize);
^ ^ ^ ^ ^
| | | | |- an integer parameter
| | | |- an object parameter
| | |-a string parameter
| |-the function
|-data type of what the function returns (an object)

Data types

Basic data types:
int integer (eg.: 8) also used for boolean operations
float floating point (eg.: 3.14)
string text string (eg.: "Hello world")

Complex data types:
object Special data type for referencing in-game objects
effect Special data type for referencing in-game effects
vector a bundle of 3 floats (x, y, z, coordinates)
location a vector bundled with a float (orientation)
talent Special data type for referencing a feat, skill, or spell (rare)

More info concerning data types available at: NWNLexicon's page (http://www.nwnlexicon.com/compiled/type.index.html)

All parentheses (), brackets[], and braces{} must be used in a pair.
Statements that do not open braces must end with a semicolon.
Scripts must have a void main() function or int StartingConditional() function or they will not be compiled into an .ncs
All functions and variables must be declared.
KotOR1 and KotOR2 have different nwscript.nss files that define their functions. You should not expect an .ncs from one game to work in the other.
Data types are strict meaning you cannot put an integer value into a float variable for example.
Indentation and line breaks are purely for the benefit of the programmer.

Operators (same as C programming)
Arithmetic: + - * /
Modulus: % the remainder, for integers only (eg. 5%2==1)
Assignment = don't confuse with equality!
Increment ++ x++ is the same as x=x+1
Assign and increment += x+=2 is the same as x=x+2

Equality ==
Inequality !=
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=

Logical AND &&
Logical OR ||
Logical NOT !

Single line comment indicator //
Multi-line comment begin indicator /*
Multi-line comment end indicator */