Difference between revisions of "OnPerception"

From NWN Lexicon
Jump to: navigation, search
Line 9: Line 9:
 
Note only one of these will ever be true; as per below it will usually fire both seen and heard if you walk into a creatures perception range (and vice versa both vanished and inaudible if you walk away from it). The order appears to be hearing first, than seeing ie; GetLastPerceptionHeard() and GetLastPerceptionInaudible() fire before GetLastPerceptionSeen() and GetLastPerceptionVanished().
 
Note only one of these will ever be true; as per below it will usually fire both seen and heard if you walk into a creatures perception range (and vice versa both vanished and inaudible if you walk away from it). The order appears to be hearing first, than seeing ie; GetLastPerceptionHeard() and GetLastPerceptionInaudible() fire before GetLastPerceptionSeen() and GetLastPerceptionVanished().
  
Since they both fire at once, if the event is GetLastPerceptionHeard() it may still be that the perceived object is seen with GetObjectSeen().
+
When a creature spawns it appears they only get 1 event fired about 4 seconds after spawning (no AI except OnSpawn is run before this time, no heartbeats or anything) - it's a little weird this event!
 +
 
 +
Since they can both fire at once, if the event is GetLastPerceptionHeard() it may still be that the perceived object is seen with GetObjectSeen().
  
 
Note also that creatures always have "eyes". The Blindsense monster feat may help them detect invisible creatures more easily, but in essence they will "see" them still.
 
Note also that creatures always have "eyes". The Blindsense monster feat may help them detect invisible creatures more easily, but in essence they will "see" them still.

Revision as of 22:14, 25 June 2020

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:

Note only one of these will ever be true; as per below it will usually fire both seen and heard if you walk into a creatures perception range (and vice versa both vanished and inaudible if you walk away from it). The order appears to be hearing first, than seeing ie; GetLastPerceptionHeard() and GetLastPerceptionInaudible() fire before GetLastPerceptionSeen() and GetLastPerceptionVanished().

When a creature spawns it appears they only get 1 event fired about 4 seconds after spawning (no AI except OnSpawn is run before this time, no heartbeats or anything) - it's a little weird this event!

Since they can both fire at once, if the event is GetLastPerceptionHeard() it may still be that the perceived object is seen with GetObjectSeen().

Note also that creatures always have "eyes". The Blindsense monster feat may help them detect invisible creatures more easily, but in essence they will "see" them still.

Common uses of this script include having the creature yell a battle cry, greet the PC, or run away if combat isn't being initiated.


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.

Perception ranges are configured in ranges.2da - the default for creatures is significantly shorter than the players own range. The highest usable perception range is 35.0 (3.5 tiles) which going over can cause issues - both because placeables have a set 35.0M appear distance, and a multiplayer server will not work on things outside this range properly.

DM note the "disappear" and "appear" options in the top left are odd. If you start off map and in disappear mode, and appear in front of a creature in range of perception it correcly fires (as if invisibility or stealth was lost). However if you disappear in front of a creature, the perception events do not fire and GetObjectSeen() and GetObjectHeard() still return 1. In fact they won't fire until the DM appears again, whereupon suddenly the events fire. Very strange for sure.


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 maximum of 1 TRUE value but only fire at most twice at once. You can expect logically an object cannot vanish and become heard, for instance in the same call, but it can vanish and become inaudiable at the same time (eg; moving out of perception range).

Hearing and sight perception ranges appear to be identical if a creature is not in stealth mode or some form of invisibility.


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()