Jump to content

Home

Introduction to Scripting Syntax


tk102

Recommended Posts

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.

int DISGUISE_TYPE_TURRET          = 182;
^   ^                               ^
|   |                               |-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

 

Rules

  1. All parentheses (), brackets[], and braces{} must be used in a pair.
  2. Statements that do not open braces must end with a semicolon.
  3. Scripts must have a void main() function or int StartingConditional() function or they will not be compiled into an .ncs
  4. All functions and variables must be declared.
  5. 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.
  6. Data types are strict meaning you cannot put an integer value into a float variable for example.
  7. 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 */

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...