Check if npc is following player

Started by arj0n, Fri 29/03/2013 16:24:12

Previous topic - Next topic

arj0n

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

if (cMerchant.FollowCharacter(cJack) == true) 
      {
      player.ChangeRoom (1, 200, 2000);
      }

Slasher

#1
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.

???

Snarky

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.

Billbis

Not very elegant, but you can also iterate to all characters:
Code: AGS
int i = 0;
while (i < Game.CharacterCount) {
    if (character[i].FollowCharacter(cJack) == true) {
      player.ChangeRoom(1, 200, 2000);
      i++;
    }
}

Crimson Wizard

#4
Quote from: Billbis on Fri 29/03/2013 17:28:06
Not very elegant, but you can also iterate to all characters:
Code: AGS
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... :(

Billbis

#5
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

arj0n

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.

Snarky

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

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);
  }
}

arj0n

#8
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.

Crimson Wizard

#9
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

Yeppoh

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.

pharlap

Awesome,  was going to post a question about this myself tonight. All solved for me :) many thanks guys
y

SMF spam blocked by CleanTalk