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

Random Numbers

In the NWN2 game, as in many other games, random events can take place. In the days of board games, such as Dungon and Dragons, different multisided dice were used to determine the outcomes of events. In a like manner, the NWN2 editor offers the programming equivalent of these multisided dice. This section will give an introduction on how to use them.


The Six-Sided Dice

The most common dice are the six-sided dice. One of these can generate a number from 1 to 6. Two of them can generate numbers from 2 to 12. The NWN2 prototype function for a six-sided die is:


// Get the total from rolling (nNumDice x d6 dice).
//  nNumDice: If this is less than 1, the value 1 will be used.
  int d6(int nNumDice=1);



The above function prototype tells us that the function is of type int which means that it will return a whole number. The function identifier is d6 and that the function argument takes one value of type int. We are told in the comments section that if the value of the variable passed in the prototype is less than 1, that a value of 1 will be used by the function.
The following program illustrates an appliation with two six-sided dice:


void main( )
 {
  object oPC = GetEntenngObject(); int Number;
  string txtNumber;
   Number = d6(2);
   txtNumber = IntToString(Number);
   FloatingTextStringOnCreature(txtNumber, oPC);
 }



If we now compile the above program in our NWN2 Program I module, then try the module in the NWN2 game, we will see that random numbers will appear over the head of our PC every time we move it over the trigger area. These same random numbers will also appear in the box on the lower left of the screen.

The NWN2 editor provides many choices for different sided dice.

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.