Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-04-2012, 11:20 PM
blackbeltben blackbeltben is offline
Registered User
Join Date: Aug 2011
Posts: 83
blackbeltben is on a distinguished road
OnPlayerTouchsMe Problem

PHP Code:
function onPlayerTouchsMe() 
works fine.

But what if the NPC moves instead and touchs the player?

Is there a function for that?
Reply With Quote
  #2  
Old 06-04-2012, 11:28 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
You could always check the NPCs x/y and if its in the players x/y within a loop or in whatever you need that
PHP Code:
if (this.x in |player.xplayer.x+3| && this.x in |player.yplayer.y+3) {
  
player.chat "The NPC touched me!";

__________________
MEEP!
Reply With Quote
  #3  
Old 06-04-2012, 11:41 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Graal really needs an onCollide() function.
Reply With Quote
  #4  
Old 06-04-2012, 11:48 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 DustyPorViva View Post
Graal really needs an onCollide() function.
and while we're at it, a way to detect changes in variables/attributes without using a timeout or sending a trigger.
__________________
Quote:
Reply With Quote
  #5  
Old 06-05-2012, 12:10 AM
blackbeltben blackbeltben is offline
Registered User
Join Date: Aug 2011
Posts: 83
blackbeltben is on a distinguished road
Quote:
You could always check the NPCs x/y and if its in the players x/y within a loop or in whatever you need that
I have the check in a looping TimeOut but for some reason it's still not working
Reply With Quote
  #6  
Old 06-05-2012, 01:22 AM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by blackbeltben View Post
I have the check in a looping TimeOut but for some reason it's still not working
Just realized that I had a typo. Accidently wrote
PHP Code:
&& this.x in |player.yplayer.y+
instead of
PHP Code:
&& this.y in |player.yplayer.y+3

So try this
PHP Code:
if (this.x in |player.xplayer.x+3| && this.y in |player.yplayer.y+3|) {
  
player.chat "The NPC touched me!";

__________________
MEEP!
Reply With Quote
  #7  
Old 06-05-2012, 01:29 AM
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
If you're moving the npc on the server-side you need to use findnearestplayers or some other kind of loop and a bounds check like callimuc has posted. I.e.

PHP Code:
for (temp.pplayers) {
  
with (temp.p) {
    if (
this.x in |player.xplayer.x+3| && this.y in |player.yplayer.y+3|) { 
      
player.chat "The NPC touched me!"
    } 
  }

__________________
Quote:
Reply With Quote
  #8  
Old 06-05-2012, 02:39 AM
blackbeltben blackbeltben is offline
Registered User
Join Date: Aug 2011
Posts: 83
blackbeltben is on a distinguished road
Quote:
Just realized that I had a typo. Accidently wrote
yea I noticed the typo in fixed it when i saw error in RC lol

The script wakes up my npc. But the NPC doesn't know who to follow once woken up. here is what I got

PHP Code:
  for (temp.pplayers) { 
  
with (temp.p) {
 if (
temp.p.this.x){
  
this.dir 1;
 
this.-= (this.speed);}
 if (
temp.p.this.x){
  
this.dir 3;
 
this.+= (this.speed);}
 if (
temp.p.this.y){
 
this.-= (this.speed);}
 if (
temp.p.this.y){
 
this.+= (this.speed);}
  }
 } 
This is also in a looping script. The baddie will follow you once you attack it or something, but other that than that, it just runs in place after woken up when a player is near

Last edited by blackbeltben; 06-05-2012 at 03:05 AM..
Reply With Quote
  #9  
Old 06-05-2012, 04:57 AM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by fowlplay4 View Post
If you're moving the npc on the server-side you need to use findnearestplayers or some other kind of loop and a bounds check like callimuc has posted. I.e.

PHP Code:
for (temp.pplayers) {
  
with (temp.p) {
    if (
this.x in |player.xplayer.x+3| && this.y in |player.yplayer.y+3|) { 
      
player.chat "The NPC touched me!"
    } 
  }

Wouldnt it be
PHP Code:
for (temp.pplayers) {
  
with (temp.p) {
    if (
thiso.x in |xx+3| && thiso.y in |yy+3|) { 
      
temp.p.chat "The NPC touched me!"
    } 
  }

?
__________________
MEEP!
Reply With Quote
  #10  
Old 06-05-2012, 05:04 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
PHP Code:
function onPlayerTouchesMe() {
  
this.chat "I need an adult, I need an adult!";

But no, Cal, that wouldn't work because when you use 'with' the reference to this. changes to the NPC being called by 'with.' To circumvent that(because this. no longer refers to the NPC itself, but in this case the player) you use thiso. to reference the parent object, or the NPC. However he was referencing player which would be wrong in this case, as it should have been this.

Alternatively this can be done:
PHP Code:
for (temp.pplayers) {
  if (
this.x in |temp.p.xtemp.p.x+3| && this.y in |temp.p.ytemp.p.y+3|) { 
    
temp.p.chat "The NPC touched me!"
  }

However you may want to consider taking into account the width/height of the NPC for proper collision detection.
Reply With Quote
  #11  
Old 06-05-2012, 05:22 AM
blackbeltben blackbeltben is offline
Registered User
Join Date: Aug 2011
Posts: 83
blackbeltben is on a distinguished road
This is all great information but my last reply post never got answered :/
Reply With Quote
  #12  
Old 06-05-2012, 05:33 AM
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 callimuc View Post
Wouldnt it be thiso?
This is one of those cases where this will still refer to then NPC.

Uploaded this npc in a level I was in:

PHP Code:
function onPlayerEnters() {
  
this.nick "NPC";
  echo(
"Test");
  echo(
"-" SPC this.nick);
  for (
temp.pplayers) {
    
with (temp.p) {
      echo(
"+" SPC this.nick SPC player.nick);
    }
  }

echos

PHP Code:
Test
NPC
NPC Leader fp4 
@blackbelben: Get rid of the with statement and your code should work fine, at least the section you posted should.

PHP Code:
for (temp.pplayers) {
  if (
temp.p.this.x) {
    
this.dir 1;
    
this.-= (this.speed);
  }
  if (
temp.p.this.x) {
    
this.dir 3;
    
this.+= (this.speed);
  }
  if (
temp.p.this.y) {
    
this.-= (this.speed);
  }
  if (
temp.p.this.y) {
    
this.+= (this.speed);
  }

__________________
Quote:
Reply With Quote
  #13  
Old 06-05-2012, 06:05 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
I think something like this would be a more appropriate way to check for collisions, albeit there's more calculation going on:

PHP Code:
for (temp.pplayers) {

  
// Determine width/height of player
  
temp.pw 2;
  
temp.ph 2;
  
// Find center of player
  
temp.px p..5 pw/2;
  
temp.py p.1  ph/2;

  
temp.tw temp.th 2;
  
temp.nx this.x;
  
temp.ny this.y;
  
// If NPC is not a showcharactor, try to find center
  
if (this.ani.name == null) {
    
temp.tw this.width;
    
temp.th this.height;
    
temp.nx this.tw/2;
    
temp.ny this.ty/2;
  } else {
  
// If NPC is a showcharacter, find proper center of character
    
temp.tw 2;
    
temp.th 2;
    
temp.nx this..5 tw/2;
    
temp.ny this.1  ty/2;
  }

  
// Check axis distance between NPC and player
  
temp.dx abs(px nx);
  
temp.dy abs(py ny);

  
// If distance on both axes are less than the width and height
  // of the NPC and player combined, by half(because you're 
  // checking from the center of the entities), then a collision
  // has occurred.
  
if (dx < (pw nw)/&& dy < (ph nh)/2) {
    
// Collision!
  
}


The problem with the other method is you're relying on a single pixel collision within a box, instead of looking for the fact that two box entities are collision(checking the center of the player entering the entire size of the NPC isn't considering the fact the player has a size as well).

However I threw this up in notepad and didn't test it myself. Your mileage may vary.
Reply With Quote
  #14  
Old 06-05-2012, 03:05 PM
blackbeltben blackbeltben is offline
Registered User
Join Date: Aug 2011
Posts: 83
blackbeltben is on a distinguished road
Quote:
@blackbelben: Get rid of the with statement and your code should work fine, at least the section you posted should.
I got it to follow. But it glitches up with two players. Is there a way I can get it to pick a player to follow.

Last edited by blackbeltben; 06-05-2012 at 04:19 PM..
Reply With Quote
  #15  
Old 06-05-2012, 05:00 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Oh ok, thanks at dusty and fp

Quote:
Originally Posted by blackbeltben View Post
I got it to follow. But it glitches up with two players. Is there a way I can get it to pick a player to follow.
PHP Code:
findnearestplayer(this.x+1.5this.y+1.5); 
You should take a look at Gamby's GBaddy v.3.0.
__________________
MEEP!
Reply With Quote
  #16  
Old 06-05-2012, 05:13 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Quote:
Originally Posted by blackbeltben View Post
I got it to follow. But it glitches up with two players. Is there a way I can get it to pick a player to follow.
You need to start applying a little logic and problem solving.

What can you do to store information? What can you do to check the status of said stored information before proceeding to do other things?

If you find a player then store the player's account/object as the NPCs current interest. If the NPC has an interest then proceed to track ONLY that player. If it has no interest then LOOK for a new player to hunt down(with findnearestplayer).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:19 AM.


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