inventory item can only be used when another character is at a certain distance

Started by barefoot, Sun 07/02/2010 22:06:05

Previous topic - Next topic

barefoot

Hi

Is there a function/event script where an inventory item can only be used on another character if they are at a certain distance away?

EG: you can't use a knuckleduster on someone yards away, they have to be close...

Summary: A character has to wait for another character to come close to him before you can use an inventory item.. in this case a knuckle duster.

I have looked around and not found exactly whatI'm looking for although i have seen collide...

Can I use the Collide script like the one below and tweak it? All advice and help appreciated.

Code: ags

if (cEgo.IsCollidingWithChar(cMan) == 1)
   { colliding code here 



cheers for any help

barefoot
I May Not Be Perfect but I Have A Big Heart ..

Ethan D

I haven't run it in the editor but this should work just fine to check distance between the characters.

If ((cMan.x - cEgo.x <= 10) && (cMan.x >= cEgo.x)) || ((cEgo.x - cMan.x < 10) && (cEgo.x > cMan.x))


In this case the editor would check whether the man is within 10 pixels of cEgo whether he is on the right or the left.  There may be a simpler way of doing this but this should work just fine.

barefoot

just a slight problem Ethan

My code is:

Code: ags

function cBartender_UseInv()
{
 if (player.ActiveInventory == iduster)

 if ((cBartender.x - cEgo.x <= 10) && (cBartender.x >= cEgo.x)) || ((cEgo.x - cBartender.x < 10) && (cEgo.x > cBartender.x))
 Display("You hit him with the knuckle duster!");

}




Gives this error:
Code: ags

GlobalScript.asc(633): Error (line 633): PE04: parse error at '||'


barefoot
I May Not Be Perfect but I Have A Big Heart ..

Ethan D

Code: ags
Try it like this:
[ code ]
function cCharacter_UseInv()
{
 if (player.ActiveInventory == iduster)

  if (((cCharacter.x - cChar1.x <= 10) && (cCharacter.x >= cChar1.x)) || ((cCharacter.x - cChar1.x < 10) && (cCharacter.x > cChar1.x)))
  Display("You hit him with the knuckle duster!");

  }

Edit: Accidentally used character instead of bartender etc,.


BTW: what are the tags for posting code.

Edit again:  Thanks for the info. discordance.

discordance

You'll need one more set of parantheses around the whole statement, like so:

Code: ags

if (((cBartender.x - cEgo.x <= 10) && (cBartender.x >= cEgo.x)) || ((cEgo.x - cBartender.x < 10) && (cEgo.x > cBartender.x)))


EDIT: Yeah, just as Ethan has aptly demonstrated above! But without using the "[ code ]" tag.

Matti

Or you could leave some of the brackets out  ;) :

Code: ags

if ((cBartender.x - cEgo.x <= 10 && cBartender.x >= cEgo.x) || (cEgo.x - cBartender.x < 10 && cEgo.x > cBartender.x))

barefoot

Thank so much guys....

It works but i need script  to continue...

when i put 'display' and then 'say' only the 'say' runs.. if i take the 'say' out the 'display' shows.

After being hit the Barman says "Ouch" and then changes view to laying on the floor..

how can i get the script to continue to the changeview?

It seems to only take 1 event...  at the moment


barefoot
I May Not Be Perfect but I Have A Big Heart ..

discordance

You'll need to add braces.

Code: ags

function cCharacter_UseInv()
{
  if (player.ActiveInventory == iduster) {

    if (((cCharacter.x - cChar1.x <= 10) && (cCharacter.x >= cChar1.x)) || ((cCharacter.x - cChar1.x < 10) && (cCharacter.x > cChar1.x))) {
      Display("You hit him with the knuckle duster!");
      etc.....
    }

  }

}

barefoot

Sorted sorted sorted

many many thanks

barefoot
I May Not Be Perfect but I Have A Big Heart ..

Khris

This code doesn't factor in the characters' y coordinate; it'll still be possible to hit someone if they are far away (above or below the player, but close x coordinate).

If the game uses a standard perspective, the area which counts as close is usually a wider than high ellipse around the character's feet.

Code: ags
// top of Global.asc

int DistanceFrom(this Character*, Character other) {
  int dx = this.x - other.x;
  int dy = (this.y - other.y)*2;

  return FloatToInt(Maths.Sqrt(IntToFloat(dx*dx + dy*dy)));
}


Use it like this:

Code: ags
  if (player.DistanceFrom(cBartender) < 10) {
    ...
  }


Add this to Global.ash if you want to use the function in room scripts:
Code: ags
import int DistanceFrom(this Character*, Character other);

barefoot

I May Not Be Perfect but I Have A Big Heart ..

SMF spam blocked by CleanTalk