Trigger OnExit problem

Howdy all, a 1.69 user here…again!

I’ve run into an issue with a little Trigger/Waypoint/Script system that I cobbled together to stop creatures from wandering all over the place when they have Ambient Animations set in their OnSpawn scripts.

While it works nicely for ducks, fish, herd animals etc, there is a problem when I try to use it with hostile creatures that I want confined to a given part of an area.
Although the creature moves about randomly as expected and moves to attack an enemy within its perception range, if it reaches the trigger and returns to its waypoint it stops moving and ignores the enemy completely!!
I’m guessing I need some sort of function to make it look for the enemy again and attack it, but I can’t find anything via searching that will help.

Below is the script that works fine for non-hostile creatures:

//******************************************************************************
//*** npc_return.nss (Trigger OnExit script)
//*** Makes creatures exiting a zone of operation return to a Waypoint.
//*** Ideal for ducks, fish etc, to prevent them from roaming everywhere.
//***
//*** All such creatures MUST have the Tag: boundnpc
//***
//*** The script allows the use of multiple instances of the waypoint which can
//*** be of use within long triggers such as those drawn along/around streams.
//***
//*** RESOURCES:
//*** Trigger  -> Custom -> Generic Trigger ->     Bound NPC Return Trigger
//*** Waypoint -> Custom -> Special -> Custom 4 -> Bound NPC Return Waypoint
//***
//*** DO NOT USE FOR HOSTILE CREATURES - THEY WILL STOP ATTACKING AFTER MOVING!!
//***
//******************************************************************************

void main()
{
object oExiter = GetExitingObject();
object oWP = GetNearestObjectByTag("WP_CREATURE_RETURN", oExiter);
if (GetTag(oExiter) != "boundnpc") return;

   AssignCommand(oExiter, ClearAllActions(TRUE));
   AssignCommand(oExiter, ActionForceMoveToObject(oWP, FALSE, 1.0, 30.0));
}

If anyone can help get my nasties charging back to the attack, I would be very grateful indeed.
Many thanks,
PT

Very strange, but I can confirm that behaviour. Seemingly it has something to do with the list of seen objects of the creature. You aborted the attack to a specific PC and this state is not canceled until the PC vanishes and appears again.

I made a add on to your script which might help.

void to_battle(object ox)
{
  object oPC = GetFirstObjectInShape (SHAPE_SPHERE, 20.0, GetLocation (ox));
  while (GetIsObjectValid (oPC))
    {
      if (GetIsEnemy(oPC, ox) && GetObjectSeen (oPC, ox))
        {
          AssignCommand (ox, ActionAttack (oPC));
          return;
        }
      oPC = GetNextObjectInShape (SHAPE_SPHERE, 20.0, GetLocation (ox));
    }
}

void main()
{
  object oExiter = GetExitingObject();
  object oWP = GetNearestObjectByTag("WP_CREATURE_RETURN", oExiter);
  if (GetTag(oExiter) != "boundnpc") return;

   AssignCommand(oExiter, ClearAllActions(TRUE));
   AssignCommand(oExiter, ActionMoveToObject(oWP, FALSE));
   DelayCommand (5.0, AssignCommand (oExiter, to_battle (oExiter)));
}

@Mmat you’re right, but this is expected behaviour.

Combat can be triggered OnPerception, but that event doesn’t occur again if the enemy is still perceived.

Since the script tells the creature to stop fighting, something else needs to renew combat.

IIRC the simplest solution is to add

AssignCommand(oExiter, ActionDoCommand(DetermineCombatRound()));

Of course, this is MUCH better.

@pturner20 This requires
#include "nw_i0_generic"

Many thanks fellas!

After using your code, Proleric, and MMat’s #include, the hostile creature attacked as soon as the PC got within its perception range.

But life is unfair, as we know, and I noticed that if the PC retreats out of the creature’s perception range, the creature just stands still (although it will attack as before if the PC approaches it again.)

Is there a way to make the creature resume using Ambient Anims or am I asking for the moon?

Thanks again as always for your time and advice.
PT

You can do this OnHeartbeat after checking that there’s no combat or perceived foe.

IIRC the function is

I don’t know, how you call the “Ambient Animations”. For me, the following on_spawn works fine:

#include "x0_i0_anims"
#include "x2_inc_switches"

void main()
{
  SetSpawnInCondition(NW_FLAG_AMBIENT_ANIMATIONS);
  SetListeningPatterns();
  WalkWayPoints();
}

Thanks again for your advice, Proleric, but I’m still struggling…

Having looked at nw_c2_default1 it seems to my (very) limited understanding that there is already a call to PlayMobileAmbientAnimations buried in one of the “else if” routines that is supposed to fire if the NPC is not having a conversation.
There are also earlier lines that check that there aren’t targets that the NPC tried to attack, cast a spell at, or is a “SeenEnemy”.
However, my creaure STILL just returns to its waypoint and does nothing at all until the PC gets close enough again to cause it to attack.

If you can suggest how I can force the creature to move about again, I would be so grateful.

Many thanks for helping again, Mmat.

The creature in question already has an OnSpawn script with the Ambient Anims flag set, and it moves around exactly as it should. However, after it sees an enemy and attacks, but is forced back to its waypoint by the OnExit trigger surrounding it, it refuses to move at all once its enemy goes beyond its perception range. So that’s why I’m looking for a way to stick a firework up its backside and get it moving again!!!

Any ideas you might have will be greatly appreciated, as always.

It stops moving around because you cleared its actions. Maybe add WalkWayPoints() once it gets back. You need something to get the default code going again. Proleric’s suggestion would also probably work.

In the original script of bio, the animation is conditional and depends on the flag
NW_FLAG_AMBIENT_ANIMATIONS.

My script set it always (in fact, I use different scripts for spawning).

If the mobile animation isn’t set by the on_spawn, but a side effect of an “encounter-spawning”, then the animation might fail after moving the creature back.

That script is quite a tangle.

You can diagnose why it’s failing by putting debug statements e.g. SendMessageToPC() in each branch, so that you can see where it fails to reach PlayMobileAmbientAnimations.