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 Closed Branch

This section illustrates the use of the Closed Branch. The Closed Branch extends the decision-making capabilities of you programs. As you will discover, the closed branch statements are very similar to the open brach statement.

 


 

Basic Idea

The basic idea behind the Closed Branch is that it will execute one of two options depending on a given condition. What this means is that one of two options will be executed, but not both.

 


 

The if...else Statement

The if...else Statement is another conditional statement. It differs from the if staement in that the if statement is an open branch where the if..else is an closed branch. The general form of the if..else statement is:

                                    if (expression) statement1 else statement2

What this means is that if expression is true then statement1 will be executed. If, on the other hand, expression is false, than statement2 will be executed. This is illustrated in the following program:

 

voidmain()

{

objectoPC=GetEnteringObject();

int Gold;

string Message;

   Gold=GetGold(oPC); //How much gold creature has.

    if(Gold<250)

     {

       Message="You don have enough gold.";

     }

   else

     {

       Message="Pleae Enter.";

     }

  FloatingTextStringOnCreature(Message, oPC);

}


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

  • The variale Gold gets the amount of gold the entering creature has.
  • The if statemetn checks to see if the creature has less than 250 gold.
  • If the creature has less than 250 gold (condition is TRUE), the first program segment between the { and } will be executed.
  • If the creature has 250 or more gold (condition is FALSE), the program will go to the else condition and the program segment between the { and } of the  else will be executed.
  • If the creature has 250 or more gold he will be allowed to enter and 250 gold will be taken form him.
  • If the creature does nto have at least 250 gold, no gold will be taken from hiim and he will not be allowed to enter.


Where do we go from here?

The next section introduces more decision-making techniques with the introduction of the logical AND and the logical OR.

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.