OnPerception Woes

I have a minotaur set to spawn in then walk a series of waypoints. I want the PC to be attacked if he gets too close to the minotaur, but I think walking the waypoints might be interfering with the desired result. Here’s the perception code I wrote, but does nothing. Any help… :wink:

#include "nw_i0_generic"


void main()
{
    if ( !GetLastPerceptionSeen() )
        return;

    // Get the creature who triggered this event.
    object oPC = GetLastPerceived();

    if ( !GetIsPC(oPC)  ||  GetIsDMPossessed(oPC) )
        return;

    // Only fire once.
    if ( GetLocalInt(GetModule(), "DO_ONCE__" + GetTag(OBJECT_SELF)) )
        return;
    SetLocalInt(GetModule(), "DO_ONCE__" + GetTag(OBJECT_SELF), TRUE);

        if ( GetDistanceToObject(oPC) < 2.5 )
        {
            // Attack the PC.
            AdjustReputation(oPC, OBJECT_SELF, -100);
            SetIsTemporaryEnemy(oPC);
            DetermineCombatRound(oPC);
        }
    }
}

@Nizidramaniit

Any reason for not using the default script? (E.g. Non hostile until seen?)

There are a number of potential issues … Firstly, you are setting a “finished” var before the PC is less than its perception check, which you limit the check to 2.5. Therefore, by the time the PC is within 2.5, you have already told it not to try again. Second, and more importantly, your script does not compile as it stands.

You could try the script below … I made the assumption that you wanted the creature to have its own check rather than any with the same tag, else none others with tag will fire.

NOTE: Using a distance check, you may still have some issues, as the OnPerception normally will not fire again when you expect. Therefore, unless the PC is within 2.5 on its first perception, it will not likely fire. Better to comment out the distance check, and just allow the default perception for the creature to do its job. I have commented out your distance check in the script below to allow default perception ranges to work.

If you need the distance check, then I agree. :slightly_smiling_face:

CREATURE ON PERCEPTION SCRIPT:

#include "nw_i0_generic"

void main()
{
    // Get the creature who triggered this event.
    object oPC = GetLastPerceived();

    if (!GetIsPC(oPC)  ||  GetIsDMPossessed(oPC) )
	{
        return;
	}

    // Only fire once.
    if(GetLocalInt(OBJECT_SELF, "DONE"))
	{
        return;
	}
        
	// if (GetDistanceToObject(oPC) < 2.5 ) // TRY WITHOUT THIS.
 	if(GetLastPerceptionSeen())
    {
            // Attack the PC.
            AdjustReputation(oPC, OBJECT_SELF, -100);
            SetIsTemporaryEnemy(oPC);
            DetermineCombatRound(oPC);
			
			SetLocalInt(OBJECT_SELF, "DONE", 1);
    }    
}

Probably the do_once stuff is the problem. It will only run once when the PC is first perceived and probably at > 2.5 meters and then it won’t ever check again.

Also, once the PC is seen perception should not fire again anyway if I recall correctly. The probably should be in the minotaur’s HB or as an area of effect effect on the baddy.

1 Like

Basically yes. OnPerception will fire only when the NPC sees/hears previously unseen/unheard enemy. (Or when previously seen enemy vanishes.)

So in a case when player is rushing towards NPC, the said NPC is will OnPerception just once (or maybe twice first with heard, then with seen). And since the NWN has 360° vision, then unless the player jumps on NPC from behind a corner or some placeable obstructing vision, it will be around 20-30.0 distance. And unless the player doesn’t leave said NPC vision, then it will not fire again.

Therefore as was already explained, this needs to be coded in OnHeartbeat event, or much better using invisible persistent circle AOE on the NPC with the attack code in OnEnter event.

1 Like

okay guys thanks… I’m just using the default script for now - Lance_Botelle was right. I don’t know why I didn’t use it in the first place. Guess I wasn’t thinking straight… lol :wink:

1 Like