Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   NPC range check/Place NPC (https://forums.graalonline.com/forums/showthread.php?t=134266527)

greggiles 05-28-2012 12:42 AM

NPC range check/Place NPC
 
question 1

Let's say I have a NPC on a looping TimeOut(). What is the code to check to see if a player is near by? I think it would go SOMETHING like this.

PHP Code:

funtion onTimeOut()
{
 if (
player.<= this.+10)
  {
    if (
this.player.-10)
     {
       
//what ever i want it to do//
       
setTimer(1); 

That's not right but i suck at math >_<

Question 2

I am a little confused on the placeNpc function.
How do I place a NPC that joins a certain class?

cbk1994 05-28-2012 02:31 AM

Use the distance formula:

http://0.tqn.com/d/math/1/0/A/1/distance.gif

Therefore, to get distance

PHP Code:

function dist(x1y1x2y2) {
  return (((
x1 x2) ^ 2) + ((y1 y2) ^ 2)) ^ 0.5;


So you can just do...

PHP Code:

if (this.dist(player.xplayer.ythis.xthis.y) < 5) {
  
player.chat "I'm within 5 tiles!";
} else {
  
player.chat "I'm not within 5 tiles!";


If you'd prefer to check based on tiles (e.g. check in a square around the NPC as opposed to a circle with distance), you can do:

PHP Code:

if (player.x in |this.2.5this.2.5| && player.y in |this.2.5this.2.5|) {
  
player.chat "I'm in the 5-tile box around the NPC.";
} else {
  
player.chat "I'm not in the 5-tile box around the NPC.";


I would typically choose the first, but it obviously depends on what you're trying to accomplish.

You'll probably want to adjust the player's x/y and the object's x/y to reach the center of the object.

greggiles 05-28-2012 07:56 PM

What about the second question???

Emera 05-28-2012 08:02 PM

PHP Code:

temp.npc putnpc2(xy"");
temp.npc.chat "I'm an NPC!"

Usage

putnpc2(x, y, script);

Crow 05-28-2012 08:25 PM

People often use putNPC2() like this:
PHP Code:

putNPC2(3030"this.join(\"classNameHere\");"); 

Using classes with putNPC2() is generally a good idea. You shouldn't ever write a whole script into that thing.

cbk1994 05-28-2012 09:13 PM

Quote:

Originally Posted by Crow (Post 1695762)
People often use putNPC2() like this:
PHP Code:

putNPC2(3030"this.join(\"classNameHere\");"); 

Using classes with putNPC2() is generally a good idea. You shouldn't ever write a whole script into that thing.

If you'd like to be even cleaner (more readable imo):

PHP Code:

temp.npc this.level.putNPC2(3030"");
temp.npc.join("myclass"); 


fowlplay4 05-28-2012 09:56 PM

Quote:

Originally Posted by cbk1994 (Post 1695773)
If you'd like to be even cleaner (more readable imo)

I prefer this way as well since escaping double-quotes looks weird due to the broken highlighting in RC.

greggiles 05-28-2012 10:01 PM

is there a code for finding the nearest player?

Emera 05-28-2012 10:31 PM

Quote:

Originally Posted by greggiles (Post 1695788)
is there a code for finding the nearest player?

findnearestplayer(x, y);
findnearestplayers(x, y);


There isn't a range though, so it doesn't matter how far away the player is, as long as they're closest to the NPC then they'll be picked up. You can get around it by doing:
PHP Code:

temp.pl findnearestplayer(this.xthis.y);
if (
temp.pl.x in this.5this.| && temp.pl.y in this.5this.5|) {
  
temp.pl.chat "I'm the nearest player!";


You could also calculate the distance from the player to the NPC, and set a maximum distance.

PHP Code:

this.max_distance 10;
temp.distance vectordist({this.xplayer.x}, {this.yplayer.y});

if (
temp.distance this.max_distance) {
  return;




All times are GMT +2. The time now is 10:01 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.