Issues getting the desired chasing effect with FollowCharacter [SOLVED]

Started by Adazalya, Mon 12/01/2015 17:53:57

Previous topic - Next topic

Adazalya

Hello everybody !

I am currently on a small project and, as a beginner, I'm kinda stuck.
So I need to make my ennemy chase the main character, nothing complicated here, used "cBaddy.FollowCharacter(cEgo,0,0)" and this is working very well ! Until I move the character, I disabled the character moving by clicking so that the character could only "jump" between five locations affected to five keys. The thing is that if I move the character to another location while the ennemy is moving toward him, the ennemy goes to the first location and start going to the second location only when after this. Which is not what I would like to do of course . . .

So I tried to script something that checks if the location of the character is still the same and disable and re-enable the follow function when it has changed but I still have the same problem. So I am guessing that the game is still finishing the movement before reading the FollowCharacter(null) line and I don't know what I could do.
I mean, is there a way to kind of "abort" the move of the ennemy if the coordinates change and then make him instantly re-move to the new location or something ? Or does the FolloweCharacter simply not allow this ? Would the walk function be more appropriate ? (my attempts with this function didn't really work for now . . .)

Thank you for reading

Let’s Get Cooking

You could perhaps write a code which checks every half a second or so, and tells the character to start following again.

Create a new global variable int called counter, default value 0.

Then in the room script, something like this under the rooms repeatedly execute:

Code: ags

function room_RepExec() {
 counter ++; // increases the "counter" int every frame

 if (counter == 20){   
 cChar1.FollowCharacter(player,0,0);}  // cBaddy will start following again every 20 frames
 
 if (counter > 20){ 
 counter = 0;} // resets the counter to 0 after 20

Play ANOMALY RESOLUTION on itch - a split-screen retro arcade shoot 'em up.

Adazalya

Thank you very much for your answer

It indeed seems quite logical but I tried and the problem remains the same, it seems like the enemy just can't update its destination to a new location until it reaches the first location . . . I experimented, even "cBaddy.FollowCharacter(null)" won't be executed until the enemy finishes to travel to the location set when the "cBaddy.FollowCharacter(cEgo, 0, 0)" was last read . . .

That's why I am wondering if the Walk function wouldn't be better as the StopMoving function can instantly interupt the enemy movement, I experimented that too with a simple walk command (I could use this to make him move again with an adjusted destination). My problem is that a walk command in the RepExec just doesn't work (dunno why yet) and when I tried to create something with while, it just aborted the script because it loope 150 000 times, so I am guessing I am not using the while word the right way . . . Any idea of how I could manage to write a while loop that checks the character location, stops ans then re-enable the enemy movement ? must I had a Wait somewhere or a counter as you suggested ? That would really help me a lot :-D

Mandle

Quote from: Adazalya on Tue 13/01/2015 22:19:03
My problem is that a walk command in the RepExec just doesn't work (dunno why yet)

I'm guessing that your Walk command is set as eBlock? I believe you are not allowed to have blocking scripts within RepExec...

Adazalya

Actually nope
I'm always using a eNoBlock Walk but it still doesn't work . . .

johanvepa

I had the exact same problem in this thread, FollowCharacter simply doesn't react fast enough for it to work properly for a chase.

A solution is to set a timer that, whenever it reaches 0, makes the enemy walk towards the chased character in a non-blocking walk. A sort of a  re-writing of the FollowCharacter command. This should work:

Code: ags

bool Monsterischasingcharacter;
int chasetimer;


function room_FirstLoad()
  {
  Monsterischasingcharacter = true;
  chasetimer = 0;
  }
 

function room_RepExec()
  {
  if (Monsterischasingcharacter)
    {
    if (chasetimer == 0)
      {
      cMonster.Walk(player.x, player.y, eNoBlock, eWalkableAreas);
      chasetimer = 80;
      }
    else
      {
      chasetimer -1;
      }
    }
  }


Vincent


Adazalya

Thank you very much for your answer ! Sorry Nanuaraq but I didn't test your script because I just saw it as I am here to tell you guys that I found the solution of my problem ^^"

So I realized that the Walk thing wasn't working because the Walk line was read again and again instead of only one time. So I added a global boolean variable newplace that become true when I press one of the keys that choose the player location and then added this code in the room repeatedly execute function :

Code: ags
///Room Repeatedly Execute
int x;
int y;
if (placement==1) {x=320; y=268;} //each value of "placement" is for a location for the character
if (placement==2) {x=145; y=140;}
if (placement==3) {x=496; y=137;}
if (placement==4) {x=148; y=391;}
if (placement==5) {x=492; y=389;}

if (newplace==true) {
  if (baddy1==true){ //If Baddy is in the room
  cBaddy1.Walk(x, y, eNoBlock);
  newplace=false;}
}


That way Baddy only changes his destination when the character changes his location !
So I will let this here as an other solution to that problem ;)

Thank you again for your answers guys

johanvepa

Quote from: Vincent on Wed 14/01/2015 17:53:10
What would happen if chasetimer for absurd is < 0 ?
The function would be broken, thats what would happen. It doesn't go below 0 in the drafted script, but for safety measures, of course, it could be fixed by adding
if (chasetimer <= 0)
instead.

johanvepa

you might still want to set the baddys walk coordinates to player.x, player.y. That will save you some of your code.

Adazalya

Oh ineed ! Thank you haha :-D I often think to complicated things before simple ones

johanvepa

Quote from: Adazalya on Thu 15/01/2015 10:26:26
Oh ineed ! Thank you haha :-D I often think to complicated things before simple ones

Sometimes, complicated solutions are simpler than simple solutions. I do that a lot ;)

selmiak


johanvepa

Quote from: selmiak on Fri 16/01/2015 15:07:48
now it's getting really cryptic... ;-D
Sorry for being needlessly cryptic :D   I just wanted to point out that sometimes it is simpler (though in the longer run more tiresome and bug-prone) to think all logical possibilities through then write it all in code, instead of looking for some specific function that does it. Like Adazalya does when he/she simply writes specific coordinates instead of using player.x, player.y. Personally, I had to write myself through a lot of code with a lot of integers that looked (almost) exactly the same, before I could appreciate the point of using arrays    :D

SMF spam blocked by CleanTalk