OnPerception

From NWN Lexicon
Jump to: navigation, search

OnPerception

The script attached to an NPC's Perception event will execute whenever the NPC's perception is triggered by another creature. The perceived creature is returned with GetLastPerceived(). There are four types of perception - Heard, Inaudible, Seen, Vanished - only one of which will have triggered the event. To determine which type, use the following functions: GetLastPerceptionSeen(), GetLastPerceptionVanished(), GetLastPerceptionHeard(), GetLastPerceptionInaudible().

Common uses of this script include having the creature yell a battle cry, greet the PC, or run away.


Trigger

A creature object has switched a perception state of the caller from TRUE to FALSE or vice versa. There are two perception states tracked for each creature relative to the caller: Heard or Seen. It is important to note that this event only executes at the moment one of these states changes. If the state of both perception types change at the same time, this event will occur twice, once for Seen/Vanished, and once for Heard/Inaudible.

For example, when a creature is heard for the first time by the caller, the Heard state switches from FALSE to TRUE, and this event is triggered. Likewise when that same creature goes beyond the hearing range of the caller, the Heard state switches to FALSE and the event is triggered.

Note that "a creature" does not necessarily mean a PC. This event can execute when a NPC or PC is teleported out of the area, as well as if an NPC's corpse disappears.


Function(s)

Primary

Secondary


Remarks

The OnPerception generic AI gives priority to first going into search mode if an enemy has suddenly vanished, then telling the creature to combat an enemy that has appeared. GetLastPerception values, as the descriptions imply, will only return a maxium of 2 TRUE values - an object cannot vanish and become heard, for instance, but it can vanish and become inaudiable at the same time.


Example

// creature turns towards anyone he sees
// if the creature seen is female, the NPC bows
void main()
{
     int nEvent = GetUserDefinedEventNumber();
     if (nEvent == 1002) //  OnPerception event
     {
          object oTarget = GetLastPerceived();
          // check for sight
          if (GetLastPerceptionSeen())
          {
               ActionDoCommand(SetFacingPoint(GetPosition(oTarget)));
               // if they female, bow
               if (GetGender(oTarget) == GENDER_FEMALE)
               {
                    ActionWait(0.5);
                    ActionPlayAnimation(ANIMATION_FIREFORGET_BOW);
               }
          }
     }
}

See Also

functions: 

GetLastPerceptionVanished()