Author Topic: Block NPC movement via trigger  (Read 404 times)

Legacy_Mirjeki

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Block NPC movement via trigger
« on: July 17, 2015, 09:27:37 pm »


               

Hello,


 


I'm pretty new to NWN Module making and the scripting in general (better late than never, right?). I currently have a bunch of NPCs bumbling around on random walk patterns, which is nice. But I'd like to be able to block them off from certain areas. For example I have a character inside a building, and I wish to make sure he doesn't walk through a certain doorway (but allow players to walk through this doorway).


 


What's the best way of blocking the NPC? Is there an easy script I can attach to the onEnter of a trigger that will make him stop moving without disrupting his ability to continue randomly walking around the room?


 


Cheers.



               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Block NPC movement via trigger
« Reply #1 on: July 17, 2015, 10:21:09 pm »


               

If his random walk is controlled by a heartbeat script, you should just be able to have the on-enter script direct him to a waypoint:



void main()
{
    object oNPC = GetEnteringObject();

    // Any checks to make sure this is the right type of object should go here.
    // As it is, this will cause all non-PCs to move back to our waypoint.
    if (GetIsPC(oNPC)) return;

    object oWaypoint = GetWaypointByTag("MY_WAYPOINT_TAG");
    if (GetIsObjectValid(oWaypoint))
    {
        AssignCommand(oNPC, ClearAllActions());
        AssignCommand(oNPC, ActionPlayAnimation(ANIMATION_FIREFORGET_PAUSE_SCRATCH_HEAD));
        AssignCommand(oNPC, ActionMoveToObject(oWaypoint));
    }
}


               
               

               
            

Legacy_Mirjeki

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Block NPC movement via trigger
« Reply #2 on: July 17, 2015, 11:09:57 pm »


               

Great! Thank you!


 


I'm not sure how to get the NPC by his Tag now (as your code implies, I need a check to make sure only this one NPC is affected by this trigger). 


 


I'm not sure why this isn't working. The error is: NON INTEGER EXPRESSION WHERE INTEGER REQUIRED


 


I'm guessing it's looking for 1 or 0, as in true or false? I'm not sure how to return this information though. I have tried if (Tag = "TK27GE3") return; and it tells me the same thing.



void main()
{
    object oNPC = GetEnteringObject();
    string Tag = GetTag(oNPC);

    if (Tag = "TK27GE3")
    {
        if (GetIsPC(oNPC)) return;

        object oWaypoint = GetWaypointByTag("OlinsHouse");
        if (GetIsObjectValid(oWaypoint))
        {
            AssignCommand(oNPC, ClearAllActions());
            AssignCommand(oNPC, ActionMoveToObject(oWaypoint));
        }
    }
}

               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Block NPC movement via trigger
« Reply #3 on: July 18, 2015, 12:18:57 am »


               

= is the assignment operator. You need ==, which tests equivalency. For more information check out this Lexicon article and this tutorial.


 


Also, PCs don't typically have tags, so you can drop the PC check:



void main()
{
    object oNPC = GetEnteringObject();

    if (GetTag(oNPC) == "TK27GE3")
    {
        object oWaypoint = GetWaypointByTag("OlinsHouse");
        if (GetIsObjectValid(oWaypoint))
        {
            AssignCommand(oNPC, ClearAllActions());
            AssignCommand(oNPC, ActionMoveToObject(oWaypoint));
        }
    }
}


               
               

               
            

Legacy_Mirjeki

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Block NPC movement via trigger
« Reply #4 on: July 18, 2015, 12:35:18 am »


               

Ah great, thank you very much. I'm pretty new to C# as you can probably tell, dumb mistake to make!


 


Anyway, cheers for that. Works perfectly.