Simple script question

I’ve got a simple script question that I’m sure a lot of you can answer. I’ve got a custom speaktrigger with a script like this.

#include "ginc_trigger"

void main ()
{
    object oPC = GetEnteringObject();
    object oPriestess = GetNearestObjectByTag("priestess",oPC);
    
    if(!GetIsPC(oPC)) return;

    if (StandardSpeakTriggerConditions(oPC) == FALSE)
    {
        return;
    }
    
    if (!GetFactionEqual(oPC,oPriestess))
    {
        return;
    }

    DoSpeakTrigger(oPC);
}

If the object with the tag “priestess” isn’t in the area at all, will the script still do a return to void? I mean, it just struck me, if it can’t find the object with the tag at all, will it still come to the conclusion that the object isn’t of the same faction as oPC, or will the game get confused and do nothing?

On Discord they suggested I do it like this to be on the safe side (don’t know why I didn’t think about that):

#include "ginc_trigger"

void main ()
{
    object oPC = GetEnteringObject();
    object oPriestess = GetNearestObjectByTag("priestess",oPC);
    
    if(!GetIsPC(oPC) || !GetIsObjectValid(oPriestess)) return;

    if (StandardSpeakTriggerConditions(oPC) == FALSE)
    {
        return;
    }
    
    if (!GetFactionEqual(oPC,oPriestess))
    {
        return;
    }

    DoSpeakTrigger(oPC);
}

According to the NWNLexicon, the tag object needs to be in the same area as the calling object.

By getting rid of all the tests for false conditions that function can be much simplified like this -

#include "ginc_trigger"

void main ()
{
    object oPC = GetEnteringObject();
    object oPriestess = GetNearestObjectByTag("priestess", oPC);
    
    if(GetIsPC(oPC) && 
       GetIsObjectValid(oPriestess) && 
       StandardSpeakTriggerConditions(oPC) && 
       GetFactionEqual(oPC, oPriestess))
    {
        DoSpeakTrigger(oPC);
    }
}

By combining the original multiple if() statements into a single if() and relying on short-circuit processing of the conditions (in a statement containing multiple &&s the first condition (starting at the leftmost and going right) that returns FALSE terminates processing of all the conditions and returns FALSE) we can remove all those return statements.

TR

2 Likes