Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Fri 29/03/2013 16:24:12

Title: Check if npc is following player
Post by: arj0n on Fri 29/03/2013 16:24:12
How can I determine if a npc is following the player and if so, activate the roomchange immediately?

Having something like this in the room_RepExec will only activate cMerchant to follow cJack
(in stead of checking if he is following cJack, immediately followed by the roomchange):

Code (AGS) Select

if (cMerchant.FollowCharacter(cJack) == true)
      {
      player.ChangeRoom (1, 200, 2000);
      }
Title: Re: Check if npc is following player
Post by: Slasher on Fri 29/03/2013 17:03:27
Off the cuff,

What about a boolean if npc following / not following (why and how would it not be following?) and use that for your room change?

Or are you just wondering about the if code and why it does not work as expected? It looks as if it should.

???
Title: Re: Check if npc is following player
Post by: Snarky on Fri 29/03/2013 17:09:14
Yes. It doesn't look like you can query it from AGS, so you'll have to create a bool to keep track of it yourself.
Title: Re: Check if npc is following player
Post by: Billbis on Fri 29/03/2013 17:28:06
Not very elegant, but you can also iterate to all characters:
Code (AGS) Select
int i = 0;
while (i < Game.CharacterCount) {
    if (character[i].FollowCharacter(cJack) == true) {
      player.ChangeRoom(1, 200, 2000);
      i++;
    }
}
Title: Re: Check if npc is following player
Post by: Crimson Wizard on Fri 29/03/2013 17:35:42
Quote from: Billbis on Fri 29/03/2013 17:28:06
Not very elegant, but you can also iterate to all characters:
Code (AGS) Select
int i = 0;
while (i < Game.CharacterCount) {
    if (character[i].FollowCharacter(cJack) == true) {
      player.ChangeRoom(1, 200, 2000);
      i++;
    }
}


This is not only "not elegant", it also won't work.
FollowCharacter does NOT return boolean value. You can't make a check like that, instead you will make all characters follow your player.

E: Well, I said, all characters, but, in fact, the result is undefined, because FollowCharacter does not formally return any value, yet, in practice, may return any random one.
EE: This script will compile simply because AGS allows to get return values even from functions that do not formally return ones... :(
Title: Re: Check if npc is following player
Post by: Billbis on Fri 29/03/2013 17:49:46
Hihi! I had not realized that OP example code was not working. :sealed: Sorry about giving bad advices.
I just learn very late about the character & object arrays, and now I want to use them everywhere.  :P
Title: Re: Check if npc is following player
Post by: arj0n on Fri 29/03/2013 21:11:58
Quote from: Crimson Wizard on Fri 29/03/2013 17:35:42
FollowCharacter does NOT return boolean value.
Indeed, and I can't manage to find a workaround for checking if a specific npc is following the player or not.
Title: Re: Check if npc is following player
Post by: Snarky on Fri 29/03/2013 22:01:26
Well, we already told you: make your own boolean and check that.

Look, an npc only follows the player if you tell him to follow the player, right? So at that point (or those points) in the code, you just make sure to set a boolean to indicate that yes, that character is following the player. (When you use a boolean to keep track of something like that, it's called a flag.)

Code (AGS) Select

bool merchantIsFollowing; // If you need to keep track of this in several rooms, put this line in the global script

function eventWhereMerchantStartsFollowing()
{
  // Other stuff that happens, perhaps
  // ...
  cMerchant.FollowCharacter(player);
  merchantIsFollowing = true;
}

function eventWhereMerchantStopsFollowing()
{
  cMerchant.FollowCharacter(null); // This makes the character stop following
  merchantIsFollowing = false;
}

function whereverYouNeedToCheckIfMerchantIsFollowing()
{
  if(merchantIsFollowing)
  {
    player.ChangeRoom(1,200,2000);
  }
}
Title: Re: Check if npc is following player
Post by: arj0n on Fri 29/03/2013 23:33:58
Clear, thanx Snarky.
I was focussed on a check at the moment the npc should be following but putting a flag before that fixes the problem.
Title: Re: Check if npc is following player
Post by: Crimson Wizard on Sat 30/03/2013 08:58:21
I guess the "CharacterBeingFollowed" (readonly) property may be a feasible request then? :)

E: duh http://www.adventuregamestudio.co.uk/forums/index.php?issue=392.0
Title: Re: Check if npc is following player
Post by: Yeppoh on Sat 30/03/2013 12:02:47
Alternatively you can set a global Character pointer like characterFollowing. That way you can keep track which character is following your PC, and if it's set as null it checks that no NPC does.
Title: Re: Check if npc is following player
Post by: pharlap on Sun 31/03/2013 07:27:26
Awesome,  was going to post a question about this myself tonight. All solved for me :) many thanks guys
y