Dead Branch Optimisation

From NWN Lexicon
Jump to navigationJump to search

As of 1.89.8193.37 (and newer versions of nwn_script_comp) you can optimise dead branches of nwscript away with if blocks.

This is only done at the compiler level, not at runtime.

Usage

If you have a if statement that, with constants, evaluates to FALSE it will disregard adding it to the compiled file.

Example

// This controls the debug statements
const int DEBUG = 1;

void main()
{
    // The below will not be compiled if DEBUG is 0, but if it is anything other than 0 it will be compiled.
    if (DEBUG) SendMessageToPC(GetFirstPC(), "DEBUG code block. Function: " + __FUNCTION__ + " file: " + __FILE__ + " line: " + __LINE__ + " date: " + __DATE__ + " time: " + __TIME__)

    // This code block will not be compiled at all unless the FALSE (0) value below is changed to anything else
    if (FALSE)
    {
         SendMessageToPC(GetFirstPC(), "FALSE code block");
    }
}

Version

This primer was added in 1.89.8193.37 of NWN:EE. scriptcomp: Will optimize away dead branches when an if condition is a constant.