Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   OnPlayerTouchsMe Problem (https://forums.graalonline.com/forums/showthread.php?t=134266578)

blackbeltben 06-04-2012 11:20 PM

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?

callimuc 06-04-2012 11:28 PM

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!";



DustyPorViva 06-04-2012 11:41 PM

Graal really needs an onCollide() function.

fowlplay4 06-04-2012 11:48 PM

Quote:

Originally Posted by DustyPorViva (Post 1696419)
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.

blackbeltben 06-05-2012 12:10 AM

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

callimuc 06-05-2012 01:22 AM

Quote:

Originally Posted by blackbeltben (Post 1696425)
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!";



fowlplay4 06-05-2012 01:29 AM

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!"
    } 
  }



blackbeltben 06-05-2012 02:39 AM

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

callimuc 06-05-2012 04:57 AM

Quote:

Originally Posted by fowlplay4 (Post 1696434)
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!"
    } 
  }


?

DustyPorViva 06-05-2012 05:04 AM

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.

blackbeltben 06-05-2012 05:22 AM

This is all great information but my last reply post never got answered :/

fowlplay4 06-05-2012 05:33 AM

Quote:

Originally Posted by callimuc (Post 1696449)
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);
  }



DustyPorViva 06-05-2012 06:05 AM

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.

blackbeltben 06-05-2012 03:05 PM

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.

callimuc 06-05-2012 05:00 PM

Oh ok, thanks at dusty and fp

Quote:

Originally Posted by blackbeltben (Post 1696468)
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.

DustyPorViva 06-05-2012 05:13 PM

Quote:

Originally Posted by blackbeltben (Post 1696468)
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).


All times are GMT +2. The time now is 05:09 PM.

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