?: Ternary Conditional

expression1 ? expression2 : expression3;

Basic Usage

The above code syntax would simply read:

if (expression1)
{
     expression2;
}
else 
{
     expression3;
}

This operator can be a more efficient way to do a simple if statement, especially if the resulting value is being stored in a common variable. Sometimes this operator is referred to as an "inline if" statement.


The example below will assign the larger of x or y to the common variable z:

int x = 5;
int y = 10;
int z = (x > y) ? x : y;
// z = 10

Advanced Usage

The Basic Usage compares the ternary operator with a simple if statement.

You can nest if statements within if statements:

if (condition_A)
{
    expression_1;
}
else
{
    if (condition_B)
    {
        expression_2;
    } 
    else
    {
        expression_3;
    } 
}

Similarly, you can nest ternary operators within ternary operators:

condition_A ? expression_1 : condition_B ? expression_2 : expression_3

Let's add some formatting to make that a little more readable:

condition_A 
    ? expression_1 
    : condition_B 
        ? expression_2 
        : expression_3

The Basic Usage code example found the larger of two numbers.

Suppose you want to compare two numbers and return:

int z = x > y ? 1 : x < y ? -1 : 0;

Master Class

The Advanced Usage showed one level of nesting; however, there is no pre-programmed maximum depth† of nesting.

By pre-programmed maximum depth, I mean there is no rule that says you can only nest to 3 or 4 or 7 or whatever levels.

Let's develop some real, and hopefully useful, code which will:

The following makes reference to the function GetMovementRate().

/************************************************************************\ 
    GetMovementRate() returns a heap of numbers I won't remember. 
    Better give them names. 
    I'll use my usual HB_ prefix - I hope no one else is using this. 
\************************************************************************/

const int HB_MOVERATEID_PC        = 0; 
const int HB_MOVERATEID_IMMOBILE  = 1; 
const int HB_MOVERATEID_VERY_SLOW = 2; 
const int HB_MOVERATEID_SLOW      = 3; 
const int HB_MOVERATEID_NORMAL    = 4; 
const int HB_MOVERATEID_FAST      = 5; 
const int HB_MOVERATEID_VERY_FAST = 6; 
const int HB_MOVERATEID_CREATURE  = 7; 
const int HB_MOVERATEID_DM_FAST   = 8; 

/************************************************************************\ 
    GetMovementRate() doco has a heap of numbers I won't remember. 
    Better give them names too. I'll start with Walkrate. 
    * TODO * Implement Runrate when Walkrate works. Use prefix HB_RNRT_ 
    Divide values by 6 - I want meters per second (not per round). 
\************************************************************************/

const float HB_WLKRT_PC        = 0.333333333;
const float HB_WLKRT_IMMOBILE  = 0.000000001;
const float HB_WLKRT_VERY_SLOW = 0.125000000;
const float HB_WLKRT_SLOW      = 0.208333333;
const float HB_WLKRT_NORMAL    = 0.291666667;
const float HB_WLKRT_FAST      = 0.375000000;
const float HB_WLKRT_VERY_FAST = 0.458333333;
const float HB_WLKRT_DM_FAST   = 0.916666667;

/************************************************************************\ 
    Prototypes 
    * STICKY TODO * Check there's a prototype for every function. 
\************************************************************************/

float get_walkrate_by_creature_ms(object creature=OBJECT_SELF); 

/************************************************************************\ 
    Functions 
\************************************************************************/

float get_walkrate_by_creature_ms(object creature=OBJECT_SELF)
{

    int iMoveRateId = GetMovementRate(creature); 

    return
          iMoveRateId == HB_MOVERATEID_PC        ? HB_WLKRT_PC 
        : iMoveRateId == HB_MOVERATEID_CREATURE  ? HB_WLKRT_NORMAL 
        : iMoveRateId == HB_MOVERATEID_NORMAL    ? HB_WLKRT_NORMAL 
        : iMoveRateId == HB_MOVERATEID_SLOW      ? HB_WLKRT_SLOW 
        : iMoveRateId == HB_MOVERATEID_FAST      ? HB_WLKRT_FAST 
        : iMoveRateId == HB_MOVERATEID_VERY_FAST ? HB_WLKRT_VERY_FAST 
        : iMoveRateId == HB_MOVERATEID_VERY_SLOW ? HB_WLKRT_VERY_SLOW 
        : iMoveRateId == HB_MOVERATEID_DM_FAST   ? HB_WLKRT_DM_FAST 
        : iMoveRateId == HB_MOVERATEID_IMMOBILE  ? HB_WLKRT_IMMOBILE 
        : /* undocumented return value */          HB_WLKRT_NORMAL; 
}

Now, I did say: be easy to use and adapt by any NWScript coder.

It is not important to understand how this syntax works!

The 9 conditions are all on the left: iMoveRateId == HB_MOVERATEID_

The values on the right are the Walkrates (in meters per second).

It can be read pretty much as English:

If this is the PC's movement rate ID return the PC's Walkrate
otherwise
    If this is the Creature's movement rate ID return the Creature's Walkrate
    otherwise
        ... and so on ...

The default condition is the /* undocumented return value */. Yes, this is a comment: use whatever text is appropriate; /* default */ is fine and holds to the switch metaphor. Here I've decided to return the Normal Walkrate if GetMovementRate() returns an undocumented ID. I use a default condition because any future update to NWScript may enhance the GetMovementRate() function by adding one or more new movement rates; if these were not trapped, my module could crash. Doing this makes your code forwardly compatible.

Gotchas

Four things that might trip the unwary:

Optimisation

If you think certain conditions are more likely to be TRUE than others, put them nearer to the top of the list.

This is because the statement ends as soon as a TRUE condition is found.

Here I'm guessing that get_walkrate_by_creature_ms() will be targetting creatures which walk at a more normal speed than those which are particularly fast or slow.

Additional Example Code

Here's another example that returns an indication of what might be described as a creature's personal space based on the creature's size. Note the similarity to the previous function. Also note the optimisation: expecting to target medium sized creatures more often than huge or tiny ones.

float GetProximityRadius(object creature=OBJECT_SELF)
{
    int iSizeIndex = GetCreatureSize(creature); 

    return
          iSizeIndex == CREATURE_SIZE_MEDIUM  ? HB_PROXIMITY_RADIUS_MEDIUM 
        : iSizeIndex == CREATURE_SIZE_SMALL   ? HB_PROXIMITY_RADIUS_SMALL 
        : iSizeIndex == CREATURE_SIZE_LARGE   ? HB_PROXIMITY_RADIUS_LARGE 
        : iSizeIndex == CREATURE_SIZE_TINY    ? HB_PROXIMITY_RADIUS_TINY 
        : iSizeIndex == CREATURE_SIZE_HUGE    ? HB_PROXIMITY_RADIUS_HUGE 
        : iSizeIndex == CREATURE_SIZE_INVALID ? HB_PROXIMITY_RADIUS_INVALID 
        : /* undocumented return value */       HB_PROXIMITY_RADIUS_INVALID; 
}
Acknowledgements

The idea for the tabular layout comes from Damian Conway's excellent book Perl Best Practices (ISBN: 0-596-00173-8). Published by O'Reilly: http://search.oreilly.com/?q=perl+best+practices


 authors: Ryan Hunt (Basic Usage), Ken Cotterill (Advanced Usage and Master Class), editor: Charles Feduke