Catch me if you can!

Started by Digital Mosaic Games, Tue 19/04/2011 14:12:02

Previous topic - Next topic

Digital Mosaic Games

Hey AGSers,

my character should be followed continuous by another character. I tried it with "cCharacter1.FollowCharacter(cCharacter2.x, cCharacter2.y, eNoBlock);" but there the following character stopped when my maincharacter stoppen. I want that the stalker walks all the time towards to my character.
"cCharacter1.Walk(cCharacter2.x, cCharacter2.y, eNoBlock, eAnywhere);" made that the followers fac location was ever to the character but he walked on the place and didn't moved.
Does somebody know how to do that?

Thanks!

Matti

cCharacter1.FollowCharacter(cCharacter2.x, cCharacter2.y, eNoBlock);  ???

That didn't give any error message? Isn't it supposed to be

Character.FollowCharacter(Character* chartofollow, optional int dist, optional int eagerness) ?

If you set dist to 0 then the character shouldn't stop until he reaches the other one.


Khris

Yeah, and why isn't this in Beginner's?

Digital Mosaic Games

Quote from: Matti on Tue 19/04/2011 14:35:31
cCharacter1.FollowCharacter(cCharacter2.x, cCharacter2.y, eNoBlock);  ???

That didn't give any error message? Isn't it supposed to be

Character.FollowCharacter(Character* chartofollow, optional int dist, optional int eagerness) ?

If you set dist to 0 then the character shouldn't stop until he reaches the other one.



I just wrote that example quickly down, that you know what I mean. Sorry if that irritated you. I have already tried to set the distance to 0 but there my problem was, that the stalker was just next to him and not in his centre.


Ali

This may be a patronising question... but are both characters standing on the central point of the sprite?

Digital Mosaic Games

#5
Maybe I should have started the thread a bit else. I want that my "hero" has to run away from the "enemy". The enemy wants to kill the hero. So if the stalking enemy catches the hero there should appear an animation how the hero is killed. This animation can appear in 4 different directions(down, keft, right, up). Now lets put aside my first question because maybe this will also be clear with the answer of this question. How can I script that it should happen the specific animation for each direction of the attack. For example if the enemy is "under" the character and is colliding with him and he can look whereever he want. Than it should appear an animation how he looks down and is killed by the enemy. I hope my aim is clear now.

I know that I've got to use the "IsCollidingWithChar"-Event in the repeadly executed funtion. But I don't know how to script these specific areas(down, up...). :-\

This "running away game" isn't something new. Its the same concept like in Gabriel Knight 1. Look! this is what I mean(first minutes):
http://www.youtube.com/watch?v=0SIyPAag_zM

Khris

You need a function to calculate the distance and relative position of the enemy.

pseudocode:

x = enemy.x - player.x;
y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

distance = Maths.Sqrt(x*x + y*y);

angle = Maths.RadiansToDegrees(Maths.ArcTan2(y/x));
// angle = -180 - 179.9
// e.g. -45 - 45 -> enemy is to the right of player

Digital Mosaic Games

Quote from: Khris on Tue 19/04/2011 18:37:19
You need a function to calculate the distance and relative position of the enemy.

pseudocode:

x = enemy.x - player.x;
y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

distance = Maths.Sqrt(x*x + y*y);

angle = Maths.RadiansToDegrees(Maths.ArcTan2(y/x));
// angle = -180 - 179.9
// e.g. -45 - 45 -> enemy is to the right of player

It's good that we put that to the beginners. ::) With your code I get the idea on how to go this thing on but I don't know where to put these lines in. I tried to define the "x" and the "y" in the first lines but after I noticed that have not enough knowledge to do that. I would be very pleased if you could give me a few more tips. Thanks a lot!


Khris

Your original question very much belonged into Beginner's. (And don't quote the previous post in full.)

The code obviously goes into repeatedly_execute since the distance must be checked all the time.
Plus there are threads about handling this already, with full code examples.

And if you try to do something and it doesn't work, POST YOUR CODE.

Scarab

#9
Quote from: NEON-GAMES on Tue 19/04/2011 19:06:30
Quote from: Khris on Tue 19/04/2011 18:37:19
You need a function to calculate the distance and relative position of the enemy.

pseudocode:

x = enemy.x - player.x;
y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

distance = Maths.Sqrt(x*x + y*y);

angle = Maths.RadiansToDegrees(Maths.ArcTan2(y/x));
// angle = -180 - 179.9
// e.g. -45 - 45 -> enemy is to the right of player

It's good that we put that to the beginners. ::) With your code I get the idea on how to go this thing on but I don't know where to put these lines in. I tried to define the "x" and the "y" in the first lines but after I noticed that have not enough knowledge to do that. I would be very pleased if you could give me a few more tips. Thanks a lot!

I'm fairly certain the following should work:

Code: ags

////at top of function////
 int x, y;
 float distance, angle;

////in room_RepEx()////
 x = enemy.x - player.x;
 y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

 distance = Maths.Sqrt(x*x + y*y);

 angle = Maths.RadiansToDegrees(Maths.ArcTan2(y, x));
 //alternately Maths.RadiansToDegrees(Maths.Arctan(y/x));
 
 if(distance <= 10.0){
    //run attacking function
 }

////in whatever function is called for the attack////
if( (angle > 45.0) && (angle <= 135.0) ){
 //run animations for direction 1
 // ( I believe this would be upwards, and anticlockwise from there,)
}
else if( (angle > 135.0) && (angle <= 225.0) ){
 //run animations for direction 2
}
else if( (angle > 225.0) && (angle <= 315.0) ){
 //run animations for direction 3
}
else {
 //run animations for direction 4
}
//death funciton for player (audio files, restoregame GUIs, etc.)



You wouldn't need to define the variables outside the function if you are calling the entire thing from RepEx(), although the use of functions is far cleaner.

Also note that the Khris' code has a typo in the syntax of Maths.ArcTan2() which takes an x and a y value, rather than a single (y/x) value (which is supported by Maths.ArcTan().

Digital Mosaic Games

Quote from: Scarab on Tue 19/04/2011 19:41:36
Quote from: NEON-GAMES on Tue 19/04/2011 19:06:30
Quote from: Khris on Tue 19/04/2011 18:37:19
You need a function to calculate the distance and relative position of the enemy.

pseudocode:

x = enemy.x - player.x;
y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

distance = Maths.Sqrt(x*x + y*y);

angle = Maths.RadiansToDegrees(Maths.ArcTan2(y/x));
// angle = -180 - 179.9
// e.g. -45 - 45 -> enemy is to the right of player

It's good that we put that to the beginners. ::) With your code I get the idea on how to go this thing on but I don't know where to put these lines in. I tried to define the "x" and the "y" in the first lines but after I noticed that have not enough knowledge to do that. I would be very pleased if you could give me a few more tips. Thanks a lot!

I'm fairly certain the following should work:

Code: ags

////at top of function////
 int x, y;
 float distance, angle;

////in room_RepEx()////
 x = enemy.x - player.x;
 y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

 distance = Maths.Sqrt(x*x + y*y);

 angle = Maths.RadiansToDegrees(Maths.ArcTan2(y, x));
 //alternately Maths.RadiansToDegrees(Maths.Arctan(y/x));
 
 if(distance <= 10.0){
    //run attacking function
 }

////in whatever function is called for the attack////
if( (angle > 45.0) && (angle <= 135.0) ){
 //run animations for direction 1
 // ( I believe this would be upwards, and anticlockwise from there,)
}
else if( (angle > 135.0) && (angle <= 225.0) ){
 //run animations for direction 2
}
else if( (angle > 225.0) && (angle <= 315.0) ){
 //run animations for direction 3
}
else {
 //run animations for direction 4
}
//death funciton for player (audio files, restoregame GUIs, etc.)



You wouldn't need to define the variables outside the function if you are calling the entire thing from RepEx(), although the use of functions is far cleaner.

Also note that the Khris' code has a typo in the syntax of Maths.ArcTan2() which takes an x and a y value, rather than a single (y/x) value (which is supported by Maths.ArcTan().




Well I put
Code: ags
int x, y;
 float distance, angle;

on the top of my room script.
Code: ags
x = enemy.x - player.x;
 y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

distance = Maths.Sqrt(x*x + y*y);

 angle = Maths.RadiansToDegrees(Maths.ArcTan2(y, x));
 //alternately Maths.RadiansToDegrees(Maths.Arctan(y/x));
 
 if(distance <= 10.0){
    //run attacking function
 }

This in the repeatly executed function and the rest in a functon I called "attack".

Now I get an error:

Failed to save room room36.crm; details below
room36.asc(32): Error (line 32): Type mismatch: cannot convert 'int' to 'float'

The line is:
"distance = Maths.Sqrt(x*x + y*y);"

Maybe it has to do sth with "Maths.ArcTan()" but I'm not sure what that is.



TomatoesInTheHead

(As Khris said, please don't quote the full post, only the relevant parts, if any. It's hard to read and spot the new things otherwise)

The error is indeed that AGS cannot convert an int to a float. That is: x and y are integer values, and the Maths functions (here: Sqrt) take floats as input. The functions to convert values are FloatToInt and IntToFloat.
You may either declare x and y as floats instead of ints, and convert the values you store in x and y, or you can leave it as is, and convert x and y to float where you need them as floats (that is, everywhere). Generally, I'd prefer the first option, but it doesn't make much of a difference in this case, as far as I see.

Khris

Here's a distance function:

Code: ags
int GetDistanceFrom(this Character*, Character*c) {

  float dx = IntToFloat(c.x - this.x);
  float dy = IntToFloat(c.y - this.y);

  float dis = Maths.Sqrt(dx*dx + dy*dy);

  return FloatToInt(dis, eRoundNearest);
}


Usage:
  if (player.GetDistanceFrom(cZombie) < 30) ...

monkey0506

I don't see why he even needs a distance function for this. It seems to me that he wants the character to be constantly chasing after the other, and if they collide, then do something about it. Per the manual:

QuoteCharacter.FollowCharacter

As a special case, setting DIST=0 and EAGERNESS=0 makes CHARID behave as if it is chasing CHARTOFOLLOW - it will try and get there as quickly as possible. Setting EAGERNESS=0 also tells the character not to stop when they reach CHARTOFOLLOW, but instead to randomly wander around the character - useful perhaps for a very energetic dog or something.

So, wouldn't it be as simple as:

Code: ags
cZombie.FollowCharacter(player, 0, 0);

// rep_exec
if (cZombie.IsCollidingWithChar(player))
{
  // attack
}


It doesn't give as fine a level of control, but based on the original request it seems to me that it fits better anyway.

Digital Mosaic Games

#14
Quote from: Khris on Tue 19/04/2011 20:23:36
Here's a distance function:

Code: ags
int GetDistanceFrom(this Character*, Character*c) {

  float dx = IntToFloat(c.x - this.x);
  float dy = IntToFloat(c.y - this.y);

  float dis = Maths.Sqrt(dx*dx + dy*dy);

  return FloatToInt(dis, eRoundNearest);
}


Usage:
  if (player.GetDistanceFrom(cZombie) < 30) ...


I placed your lines at the top of the roomscript. Right?
Than I deletet these first lines because an error appeared:
Code: ags
////in room_RepEx()////
 x = enemy.x - player.x;
 y = (enemy.y - player.y)*2;   // *2 to compensate for ground perspective

 distance = Maths.Sqrt(x*x + y*y);


Now I still get an error. In this line:
Code: ags
angle = Maths.RadiansToDegrees(Maths.ArcTan2(y, x));

There's a undefined symbol 'x'.
Maybe I need another code for "angle". Is that possible? Or it is again because the ints. which can't be readen right? But how can I change it if it is this case?

Digital Mosaic Games

#15
I've made all the kill_animations(4 directions) but I don't know how to go on as I described in my last post. ??? I want to use all this animations so that they are not for nothing. Please help me!!! Thank you so much. I tried it also many times with own methods but they didn't gave me the right aim. My Character was ever just attacked from one side. I think your methods are much better but as I wrote there is this error with the undefined symbol 'x' and I tried to solve it on my own but I noticed that I have not enough knowledge in this part of scripting.

Sephiroth

#16
"Undefined symbol" Output means you probably forgot to declare the mentioned variable. Decalaring x and y is accomplished with this line you must have forgotten in the process:

Code: ags


int x, y;



If you only use x and y inside Attack() then put these lines inside the function, otherwise outside.

Digital Mosaic Games

Quote from: Sephiroth on Fri 22/04/2011 13:49:00
"Undefined symbol" Output means you probably forgot to declare the mentioned variable. Decalaring x and y is accomplished with this line you must have forgotten in the process:

Code: ags


int x, y;



If you only use x and y inside Attack() then put these lines inside the function, otherwise outside.

Thank you but this is not much help. :'( It seems that nothing wants to work. Everytime the same cannot convert int to float error. And I cannot solve this damned error! I tried so many ways but it still doesn't work. Is there someone who could send me a whole script or module for this?? Don't think I'm lazy! I tried my best ever and ever again but I can't handle that. This thing is to difficult for me. This moment would be so beautiful if it works perfectly! :) So I thank everyone who helped me before but there comes an error if thought I solved the error before and I don't really see the point. It would be sooo glad if you could help me again! Thanks alot for your time guys!!!

Khris

A "cannot convert int to float error" is by far the easiest thing to solve.
If you don't post the surrounding piece of code, we can hardly help you though. (Who would've thought?)

Digital Mosaic Games

Quote from: Khris on Tue 26/04/2011 10:50:42
A "cannot convert int to float error" is by far the easiest thing to solve.
If you don't post the surrounding piece of code, we can hardly help you though. (Who would've thought?)

Sorry, of course... Look this is what I got at the moment. There are some parts which I just deleted because the errors. Before I tried to change them but with no good results.

At the top of room script(including distance function by Khris):
Code: ags
 int x, y;
 int angle;

 int GetDistanceFrom(this Character*, Character*c) {

  float dx = IntToFloat(c.x - this.x);
  float dy = IntToFloat(c.y - this.y);

  float dis = Maths.Sqrt(dx*dx + dy*dy);

  return FloatToInt(dis, eRoundNearest);
}


In repeadly execute:
Code: ags
x = cDog.x - player.x;
    y = (cDog.y - player.y)*2;  
 
    if (player.GetDistanceFrom(cDog) <= 10.0){
    Attack();
 }


And in the Attack function(I still havent scripted the animation stuff because this will not be difficult):
Code: ags
  if( (angle > 45.0) && (angle <= 135.0) ){
 //run animations for direction 1
 // ( I believe this would be upwards, and anticlockwise from there,)
}
else if( (angle > 135.0) && (angle <= 225.0) ){
 //run animations for direction 2
}
else if( (angle > 225.0) && (angle <= 315.0) ){
 //run animations for direction 3
}
else {
 //run animations for direction 4
}
//death funciton for player (audio files, restoregame GUIs, etc.)



SMF spam blocked by CleanTalk