Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: Old_Guy on September 29, 2007, 08:34:48 PM

Title: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on September 29, 2007, 08:34:48 PM
I'm a complete noob with a (probably) dumb question.

I am making a 2D game. It is my first shot with WME  :)
Is there a way to make selected NPC's "follow" my main actor where ever he goes (as if the NPC is being led by the hand so to speak)?
Title: Re: Making NPCs Follow Your Main Actor?
Post by: metamorphium on September 29, 2007, 08:46:14 PM
you can have a daemon script which would periodically check the X and Y position of your lead actor and then call GoTo method of support
actor. Try it and if you're still unsure, I'll post some code.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on September 29, 2007, 08:55:17 PM
Very cool. Thanks! I'll give it a shot.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on September 29, 2007, 11:03:12 PM
The Old Guy hasn't quite been able to pull it off.  ???

And so I come a beggin' for the aforementioned code . . .please.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Daniel on September 29, 2007, 11:47:13 PM
If you're referring to the same OldGuy from the demo, then if memory serves, he is an Entity and not an Actor. Entities can't walk in WME, if I recall correctly, and therefore they don't have any GoTo() method. If that is really the case, you should try what meta recommended but this time with an Actor instead of an Entity and it should work.

Oh, and welcome to the forum, by the way :)
Title: Re: Making NPCs Follow Your Main Actor?
Post by: sychron on September 29, 2007, 11:53:48 PM
I rather think he's referencing himself ...
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Daniel on September 30, 2007, 01:12:17 AM
I rather think he's referencing himself ...
Could be... I'm just not very used to people using third person to refer to themselves...
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on September 30, 2007, 05:23:34 AM
Yes, he was referring to himself. Sorry for the mix up.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: metamorphium on September 30, 2007, 09:10:02 AM
First of all we're going to tweak the main actor script file (it's usually called actor.script and is located in the actor's folder). We'll add here custom
method GoTo, which will override the original.

Code: [Select]
method GoTo(posx,posy)
{
   // We store some attributes to indicate, that our actor got a Go To command
   this.newWalking = true;
   this.targetX = posx;
   this.targetY = posy;
   
  // we call the original Go To
   this.GoTo(posx, posy);
}

Now we modify the script for the second actor:
Code: [Select]
#include "scripts\base.inc"

while (1)
{
  if (actor.newWalking)
  {
     actor.newWalking = false;
     if (this.IsWalking()) this.Reset();  //if our support cast already moves, stop him
     this.GoTo(actor.targetX, actor.targetY);  // send support cast to the leading role character.
  }
   Sleep(100);
}


Sorry, I can't test it right now, but should work.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: sychron on September 30, 2007, 12:22:05 PM
I would add a trigger to the endless loop, something like

while (1) {
  if (isFollowing){
    your stuff here
  }
}

So you can trigger wether actor 2 should follow or not.
Title: Re: Making NPCs Follow Your Main Actor?
Post by: metamorphium on September 30, 2007, 01:21:58 PM
yes but for understanding the principle it's better to present the minimal version and build on it.  ;)
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on October 01, 2007, 03:10:14 PM
Thank you very much, kind Sir   : ).
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on October 01, 2007, 05:09:33 PM
Here is my final non-elegant tweaking. It seems to work great. It lets me take hold of the npc and lead it until I "re-take", which then lets go of the npc.

In the NPC's .script file:
=================
on "Take"
{
  if (this.isHeld)
   {
   this.isHeld = false; // Let go of the NPC
   }
  else
   {
   GoToObject();
   this.isHeld = true;  // Hold the NPC
   FollowHim();
   }
}


function FollowHim()
{
 while (this.isHeld)
 {
   if (actor.newWalking)
   {
     actor.newWalking = false;
     if (this.IsWalking()) this.Reset();  //if npc is already moving, stop him
     this.GoTo(actor.targetX-25, actor.targetY-25);  // send npc to the main actor
   }//end if
  Sleep(100);
 }//end while
} // end FollowHim


Many thanks  :)
Title: Re: Making NPCs Follow Your Main Actor?
Post by: metamorphium on October 01, 2007, 07:05:43 PM
the only suggestion would be to define your "following" function as a method instead of function.

This way you could also trigger the follow mode outside of the script through:

npc.FollowHim();
Title: Re: Making NPCs Follow Your Main Actor?
Post by: Old_Guy on October 04, 2007, 02:02:45 PM
Excellent idea. TYVM ! : )