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

Compound Statements

Compound assignment statements are a way of combining several logical conditions and testing them. Doing this gives the programmer a great deal of power over the logical operation of the program.


Compound Statement Example

The following compound assignment has two AND conditions connected with a single OR condition.

                  ((expression1) && (expression2)) || ((expression3) && (expression4))

The above expression will be evaluatd to TRUE if expression1 AND expression2 are TRUE, OR, if expression3 AND expression4 are TRUE or both of AND expressions are TRUE. Otherwise it will be evaluated as FALSE.


Program Compound Statement Example

As example of the use of a compound statement is shown in the program below:


//Checks the age and hit points of the PC

//If the PC is 21 or older and less than 120

//or if hit points are 10 or less, he is sold

//a cup of grog for 5 gold.

void main()

 {

 object oPC=GetEnteringObject();

 string Message;

 int Gold=5;

 int Age;

 int HitPoints;

  Age=GetAge(oPC);

  HitPoints=GetCurrentHitPoints(oPC);

  if(((Age>=21)&&(Age<120))||(hitPoints<=10))

   {

    Message="Have a cup of grog.";

    TakeGoldFromCreature(Gold, oPC);

   }

FloatingTextStringOnCreature(Message, oPC);

}

 


 

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

  • The program checks an age range and hit points of the creature.
  • If the creatures age range is between 21 and 120 inclusive then he is offered a cup of grog and charged 5 gold.
  • if the creatures hit poitns are 10 or less he is offered a cup of grog and charged 5 gold.
  • If either of both of the above two conidtions are TRUE he is offerd a cup of gorg and charged 5 gold.
  • If the creature does not meet any of these requirements, then nothing happens.

 


Where do we go from here?

The next section introduces the use of the if else  statement.

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.