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

The Open Branch

Here you will see how to use the relational oeprators prsented in the previous section. This is the beginning of having the ability to code decision-making capabilities into the NWN2 environment.


 

Basic Idea

The basic idea behind the Open Branch is that there is a main part of the program that will always happen and a part of the program that may or may not happen depending on a condition.

As an example, suppose you had an age check on one of the games creatures entering your castle. If the creature was over a certain age, you gave them a special greeting and gold. If, however the creature was not over that age, they got a standard greeting and no gold. One way of doing this woudl be by using the Open Branch which uses the if statment.


 

The if Statement

The if statement is referred to as a conditionial statement because its execution will depend on a specific condition. The general form of the if statement is:

                                                         

                                                          if (expression) statement



The program below illustrates an application of the open branch using the if statement:

 

void()

{

object oPC = GetEnteringObject();

int Age;

int Gold;

string Greeting;

string Message;

  Age=GetAge(oPC);//Get creatures age.

  Gold=0;

  Greeting="";

 

  if(Age>=110)

  {

  Greeting="Welcome wise one.";

  Gold=100;

  }

 

Message="Enter Gandor!";

Message=Greeting + Message;

GiveGoldToCreature(oPC, Gold);

FloatingTextStringOnCreature(Message, oPC);

 

}

 

The above program compiles without errors. Here are some facts about the program:

  • Note that the if(Age>=110) sets a condition that if the creatures age is greater than or equal to the value of 110, than the program lines between the opening { and the closing } that immediately follow, will be executed.
  • If the above condition for the age is not met, then the staements:  Greeting="Weclome wise one."; Gold=100; would not be executed.
  • No matter what the creatures age, the remainder of the program: Message="Entor Gandor!"; Message=Greeting+Message; GiveGOldtoCreature(oPC, Gold);will always be executed.

 


 

Where do we go from here?

The next section introduces another important decision-making tool, the Closed Branch.

 



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.