Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Puppet Master on Thu 29/12/2005 03:26:49

Title: Help with Enemys.
Post by: Puppet Master on Thu 29/12/2005 03:26:49
How would I make a character that kills you if it gets too close to you? I know it's possible because its done in alot of ags games and I can't imagine it being THAT hard. Thanx.
Title: Re: Help with Enemys.
Post by: Hammerite on Thu 29/12/2005 08:56:38
I suppose you could have a walkable area regiony thing and put a conditional thing.
I cant really explain it myself.
Title: Re: Help with Enemys.
Post by: petazzo on Thu 29/12/2005 09:20:50
You can use this for seeing when the characters are near:

Quote from: the manualIsCollidingWithChar
(Formerly known as AreCharactersColliding, which is now obsolete)

Character.IsCollidingWithChar(Character* otherChar)

Checks if the character is touching OTHERCHAR. This function just checks the baseline of both characters, so if one is standing a fair distance behind the other, it will not be marked as colliding.
Returns 1 if the characters feet are touching, 0 otherwise.

Example:

if (character[EGO].IsCollidingWithChar(cMan) == 1)
   { colliding code here }

will execute the colliding code only if the characters EGO and MAN are colliding.
Title: Re: Help with Enemys.
Post by: monkey0506 on Thu 29/12/2005 16:37:09
You could alternatively just check the exact coordinates of both the characters...something like:

if ((cNpc.x <= player.x + 20) && (cNpc.x >= player.x - 20) && (cNpc.y <= player.y + 20) && (cNpc.y >= player.y - 20)) {
  /* the NPC is within 20 pixels to the left or right, and 20 pixels above or below the player character! */
  }


As denoted by the comment, that would check if the NPC was within 20 pixels to the left or right, and 20 pixels above or below the player character.  Then between the brackets you could script the interaction.  For example:

if ((cNpc.x <= player.x + 20) && (cNpc.x >= player.x - 20) && (cNpc.y <= player.y + 20) && (cNpc.y >= player.y - 20)) {
  player.Say("ARG!  You've killed me!"); // display a message
  FadeOut(32); // fade the screen to black
  QuitGame(0); // quit the game
  }


That would make the player say the text "ARG!  You've killed me!", fade the screen to black, and then quit the game.

[EDIT:]

I forgot to mention, I used the generic name NPC for the NPC.  In the actual script you would replace cNpc with the script o-name of the character ('c' followed by the script name of the character (the first letter of the name capitalized, the rest lower case)).
Title: Re: Help with Enemys.
Post by: strazer on Thu 29/12/2005 17:28:54
Also make sure to also check if the character is in the same room the player is in, otherwise the condition will be true even when the NPC is an another room.
Title: Re: Help with Enemys.
Post by: Puppet Master on Thu 29/12/2005 18:07:39
Thanks guys, I'll try them both out and see which one works better.
Ok, first petazzo, the engine tells me it doesn't recognize "Colliding"
And monkey, it says it is an unexpected if statement. Explanations would be great.
Title: Re: Help with Enemys.
Post by: monkey0506 on Thu 29/12/2005 18:31:54
Okay, here's the revised example:

if ((cNpc.Room == player.Room) && (cNpc.x <= player.x + 20) && (cNpc.x >= player.x - 20) && (cNpc.y <= player.y + 20) && (cNpc.y >= player.y - 20)) {
  player.Say("ARG!  You've killed me!"); // display a message
  FadeOut(32); // fade the screen to black
  QuitGame(0); // quit the game
  }


This code has to be placed within a function.  I would recommend putting it in the room's repeatedly_execute function unless you need it in several rooms, then putting it in the global repeatedly_execute function should work.
Title: Re: Help with Enemys.
Post by: Puppet Master on Thu 29/12/2005 18:54:28
Sorry, it's still not working >:(
Title: Re: Help with Enemys.
Post by: monkey0506 on Thu 29/12/2005 19:29:02
Well...what's wrong with it?  It's kind of hard to help diagnose the problem without knowing the symptoms...
Title: Re: Help with Enemys.
Post by: Akumayo on Fri 30/12/2005 02:38:50
Quote from: monkey_05_06 on Thu 29/12/2005 18:31:54
if ((cNpc.Room == player.Room) && (cNpc.x <= player.x + 20) && (cNpc.x >= player.x - 20) && (cNpc.y <= player.y + 20) && (cNpc.y >= player.y - 20)) {
Ã,  player.Say("ARG!Ã,  You've killed me!"); // display a message
Ã,  FadeOut(32); // fade the screen to black
Ã,  QuitGame(0); // quit the game
Ã,  }


What's wrong is your code silly.Ã,  You wrote:
Quote
(cNpc.y <= player.y + 20) && (cNpc.y >= player.y - 20)

Now, doesn't that mean that if the player is above and below the Npc?Ã,  I think it does, and that is quite impossible.Ã,  Try this code instead:

if ((cNpc.Room == player.Room) && ((cNpc.x <= player.x + 20) || (cNpc.x >= player.x - 20)) && ((cNpc.y <= player.y + 20) || (cNpc.y >= player.y - 20))) {Ã,  //if player is 20 pixels near the enemy
Ã,  player.Say("ARG!Ã,  You've killed me!"); // display a message
Ã,  FadeOut(32); // fade the screen to black
Ã,  QuitGame(0); // quit the game


If my knowledge of operators is correct, this says:
IF player is in Npc's room, AND the player is left OR right 20 pixels of the Npc AND is 20 pixels up OR down from the Npc, then continue with code.
Title: Re: Help with Enemys.
Post by: Puppet Master on Fri 30/12/2005 05:11:31
It's telling me I'm having problems with room a, and I'm confused.
Title: Re: Help with Enemys.
Post by: Akumayo on Fri 30/12/2005 05:17:22
Please, tell us the error message you're getting, and if possible, also give us the script you think is causing the problem.  If you do this, we can help you much more easily.
Title: Re: Help with Enemys.
Post by: Gilbert on Fri 30/12/2005 05:35:13
If it's something like "function room_a() missing" you probably delete some editor created functions directly form the script, and not using the interaction editor to remove them for you. Read this (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=23527.0).
Title: Re: Help with Enemys.
Post by: Puppet Master on Fri 30/12/2005 16:43:45
Oh, thanks man, I'll try that out.

Ok now it says, NESTED FUNCTIONS ARE NOT SUPPORTED ??? what am I suppossed to do know.
Title: Re: Help with Enemys.
Post by: Akumayo on Fri 30/12/2005 17:23:29
That usually means that you forgot a closing brace ( } ) somewhere in the script above the error line.