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

Logical OR

Another logical condition used by C++ is the OR condition. Like the AND condition, the OR condition is a powerful addition to constructing gaming AI.


The Logical Or

The logical OR operation is expressed as:

                                              (expression1) || (expression2)

The above expressiosn will be evaluated TRUE if expression1 or expression2 or both are TRUE, otherwise the operation will be evaluated as FALSE. Another way of looking at this is the only tiem an OR operation is evaluated to FALSE is when both conditions are FALSE.

Note that two vertical bars are used to indicated the OR relationship. The vertical bar is found on the keyboard over the ENTER key and is shared with the backslash \.

In the NWN2 editor, TRUE is a 1 and FALSE is a 0. Observe that the double && is used to represent the OR operation:


expression1 expression2 Result
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

 


 

Logical || Example

An example of hte logical || operatoin is illustrated in the following program.

 

//Checks the age and hti points of the PC

//If one or both are equal to or greater then 100

//then PC is rewarded. Otherwise, PC must pay gold.

 

void main()

{

  object oPC=GetEnteringObject();

  string Message;

  int GOld=200;

  int Age;

  int HitPoints;

    Age=GetAge(oPC);

    HitPoints=GetCurrentHitPoints(oPC);

    if(Age>=100||(HitPoints>=100))

    {

     Message="Welcome, oh wise one.";

     GiveGoldtoCreature(oPC, Gold);

    }

   else

    {

     Gold=50;

     Message="That will be 50 gold please.";

     TakeGOldFromCreature(Gold, oPC);

    }

FloatingTextStringonCreature(Message, oPC);

}

 


 

The Above Program compiles without errors. Here are some facts aout the program:

  • The program checks the age and hit points of the creature.
  • If the creature has an age that is equall to or greater than 100 OR the hit points are equal to or greater than 100, then gold is given to the creature.
  • If neither of the above conditions are not met, then gold is taken from the creature.
  • Note the use of the parenthesises ((expression1) || ((expression2)). THis is done so the final test is evaluating just one TRUE or FALSE condition.


Where do we go from here?

The next section introduces more decision-making techniques.

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.