Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-28-2012, 12:42 AM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
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?
Reply With Quote
  #2  
Old 05-28-2012, 02:31 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Use the distance formula:



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.
__________________
Reply With Quote
  #3  
Old 05-28-2012, 07:56 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
What about the second question???
Reply With Quote
  #4  
Old 05-28-2012, 08:02 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
PHP Code:
temp.npc putnpc2(xy"");
temp.npc.chat "I'm an NPC!"
Usage

putnpc2(x, y, script);
Reply With Quote
  #5  
Old 05-28-2012, 08:25 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
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.
Reply With Quote
  #6  
Old 05-28-2012, 09:13 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Crow View Post
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"); 
__________________
Reply With Quote
  #7  
Old 05-28-2012, 09:56 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by cbk1994 View Post
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.
__________________
Quote:
Reply With Quote
  #8  
Old 05-28-2012, 10:01 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
is there a code for finding the nearest player?
Reply With Quote
  #9  
Old 05-28-2012, 10:31 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Quote:
Originally Posted by greggiles View Post
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;

Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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