Help with Enemys.

Started by Puppet Master, Thu 29/12/2005 03:26:49

Previous topic - Next topic

Puppet Master

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.

Hammerite

I suppose you could have a walkable area regiony thing and put a conditional thing.
I cant really explain it myself.
i used to be indeceisive but now im not so sure!

petazzo

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.
I never forget a face, but in your case I'll be glad to make an exception.
(Groucho Marx)

monkey0506

#3
You could alternatively just check the exact coordinates of both the characters...something like:

Code: ags
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:

Code: ags
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)).

strazer

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.

Puppet Master

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

monkey0506

Okay, here's the revised example:

Code: ags
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.

Puppet Master

Sorry, it's still not working >:(

monkey0506

Well...what's wrong with it?  It's kind of hard to help diagnose the problem without knowing the symptoms...

Akumayo

#9
Quote from: monkey_05_06 on Thu 29/12/2005 18:31:54
Code: ags
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:
Code: ags

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.
"Power is not a means - it is an end."

Puppet Master

It's telling me I'm having problems with room a, and I'm confused.

Akumayo

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.
"Power is not a means - it is an end."

Gilbert

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.

Puppet Master

#13
Oh, thanks man, I'll try that out.

Ok now it says, NESTED FUNCTIONS ARE NOT SUPPORTED ??? what am I suppossed to do know.

Akumayo

That usually means that you forgot a closing brace ( } ) somewhere in the script above the error line.
"Power is not a means - it is an end."

SMF spam blocked by CleanTalk