Stub This article is a stub. You can help NWNWiki by expanding it.

Function wrapping is a technique used in programming where a new function is created to add new functionality to an existing function. In essence, it creates a layer around, or "wraps", the original function inside its code. The new function may add new parameters to the function call, may check for certain conditions that warrant changing the result from the original function, or may just add a method for calling out to other functions to allow for new systems to be built around the original code.


The example below is a simple example of function wrapping that fixes a bug in Bioware's function. According to the Dungeons and Dragons Player's Handbook, Paladins and Rangers are supposed to have their caster level be equal to one half of their class level. This is not the case with the Bioware function GetCasterLevel. The example below wraps the call to GetCasterLevel, checking if the class the caster is using to cast a spell is Paladin or Ranger, and returns the proper value.

int NewGetCasterLevel(object oCaster=OBJECT_SELF) {
    // get the caster level from the Bioware function
    int iCasterLevel = GetCasterLevel();
    // get the class the caster is using to cast the spell
    int iSpellCastClass = GetLastSpellCastClass();
    if(iSpellCastClass==CLASS_TYPE_PALADIN || iSpellCastClass==CLASS_TYPE_RANGER) {
        // The caster is a paladin or ranger, adjust their 
        // caster level to half class level
        iCasterLevel = GetLevelByClass(iSpellCastClass) / 2;

    return iCasterLevel;  // return the caster level

Now if all the calls to GetCasterLevel are replaced with NewGetCasterLevel, Paladins and Rangers will be treated correctly for every spell in the game. Once all the calls are replaced, any new changes can be made to NewGetCasterLevel, and all functions will be updated accordingly with a recompile of the scripts.