Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: barefoot on Sun 07/02/2010 22:06:05

Title: inventory item can only be used when another character is at a certain distance
Post by: barefoot on Sun 07/02/2010 22:06:05
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.


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



cheers for any help

barefoot
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: Ethan D on Sun 07/02/2010 22:31:57
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.
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: barefoot on Sun 07/02/2010 22:42:19
just a slight problem Ethan

My code is:


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:

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


barefoot
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: Ethan D on Sun 07/02/2010 22:49:30
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.
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: discordance on Sun 07/02/2010 22:49:44
You'll need one more set of parantheses around the whole statement, like so:


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.
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: Matti on Sun 07/02/2010 22:52:14
Or you could leave some of the brackets out  ;) :


if ((cBartender.x - cEgo.x <= 10 && cBartender.x >= cEgo.x) || (cEgo.x - cBartender.x < 10 && cEgo.x > cBartender.x))
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: barefoot on Sun 07/02/2010 22:56:46
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
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: discordance on Sun 07/02/2010 23:26:30
You'll need to add braces.


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.....
    }

  }

}
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: barefoot on Sun 07/02/2010 23:48:59
Sorted sorted sorted

many many thanks

barefoot
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: Khris on Sun 07/02/2010 23:50:56
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.

// 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:

 if (player.DistanceFrom(cBartender) < 10) {
   ...
 }


Add this to Global.ash if you want to use the function in room scripts:
import int DistanceFrom(this Character*, Character other);
Title: Re: inventory item can only be used when another character is at a certain distance
Post by: barefoot on Sun 07/02/2010 23:56:38
cheers khris

i will take that into account..

barefoot