Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Won't detect players touching NPC (https://forums.graalonline.com/forums/showthread.php?t=134265768)

Emera 02-17-2012 03:13 PM

Won't detect players touching NPC
 
I'm trying to script some sort of an event, and I'm having a little trouble. Basically, the point is not to get hit by the blocks randomly warping around the room. I've got that sorted. Now, I can't seem to get the NPC to detect if it's hit a player! Can somebody tell me what I'm doing wrong?

PHP Code:

function onCreated() {
  
setTimer(0.05);
  
setshape(1,32,32);
}

function 
onTimeout() {
  
  if (
this.on == 1) {
    
this.timer -= 1// Start the count down
    
this.random(3.5,57.5);
    
this.random(18,46);
   
    if (
player.x in this.xthis.this.width | && player.y in this.ythis.this.height|) {
      
player.chat "I've been hit!";
    }
  }
  
  if (
this.timer == 0) {
    
this.on 0// Stop warping
  
}
  
setTimer(0.05);


Edit
I think I've found my issue. It can't detect the players x and y co-ords server side? It worked when I created a new NPC and made it clientside. If I do this to my NPC, players won't be able to see the same X and Y of the block that other players see, and that's what I'm trying to do. Anybody got any ideas?

Just wondering if I could use a triggeraction to get around this. Would it be possible to send a triggeraction from the serverside to the clientside?

Edit
I've managed to put the movement and whatever clientside, and trigger the movement via the serverside using triggeraction. Now, I need help again trying to make the NPC detect if a player is in it's x and y.

PHP Code:

/*
Timer is the amount of time it takes
to warp around the room. The higher the
timer, the longer the warp session is.
*/
function onPlayerChats() {
  if (
player.guild != "Events Team") {
    return;
  }
  if (
player.chat == ":warp") {
    
triggeraction(31,33,"start",NULL);
  }
}

function 
onActionhit() {
  
player.chat "HAS BEEN HIT!";
}

//#CLIENTSIDE

const TIMER 25;

function 
onCreated() {
  
setTimer(0.05);
  
setshape(1,32,32);
  
this.height this.width 32;
}

function 
onTimeout() {
  
this.chat "Status:" SPC this.on;
  if (
this.on == 1) {
    
this.timer -= 1;
    
this.random(3.5,57.5);
    
this.random(18,46);
    if (
player.x in this.xthis.this.width | && player.y in this.ythis.this.height|) {
      
triggeraction(player.xplayer.y"hit"NULL);
    }
  }
  if (
this.timer == 0) {
    
this.on 0;
    
this.31;
    
this.33;
  }
  
setTimer(0.05);
}

function 
onActionstart() {
  
this.on 1;
  
this.timer TIMER;



Crow 02-17-2012 04:06 PM

First off: you don't even have a player scope in that timeout. You will have to loop through all players and check them all. Also, this.width and this.height are set by setShape() already, I believe, and even if they aren't, they're way off. You want them in tiles, not pixels.

Emera 02-17-2012 04:09 PM

Quote:

Originally Posted by Crow (Post 1684806)
First off: you don't even have a player scope in that timeout. You will have to loop through all players and check them all. Also, this.width and this.height are set by setShape() already, I believe, and even if they aren't, they're way off. You want them in tiles, not pixels.

I think I get what you mean by looping through the players, and I had a hunch that the setShape() set the width and height, but wasn't sure so I dumped the height and width check just for good measure. Also, how would I detect the tiles instead of pixels?

Crow 02-17-2012 04:15 PM

Quote:

Originally Posted by Emera (Post 1684807)
Also, how would I detect the tiles instead of pixels?

I was just saying that your width/height should be 2 instead of 32, since you already are checking for tile coordinates.

Emera 02-17-2012 04:18 PM

Quote:

Originally Posted by Crow (Post 1684808)
I was just saying that your width/height should be 2 instead of 32, since you already are checking for tile coordinates.

So, are you saying I should use setShape(1,2,2);?
Don't think that's what you're saying since I tried that and it didn't work.

Crow 02-17-2012 06:26 PM

Quote:

Originally Posted by Emera (Post 1684809)
So, are you saying I should use setShape(1,2,2);?
Don't think that's what you're saying since I tried that and it didn't work.

No :oo: But this line:
PHP Code:

if (player.x in this.xthis.this.width | && player.y in this.ythis.this.height|) { 

Doesn't make sense if you have width/height as 32, which you did.

Emera 02-17-2012 06:32 PM

Quote:

Originally Posted by Crow (Post 1684811)
No :oo: But this line:
PHP Code:

if (player.x in this.xthis.this.width | && player.y in this.ythis.this.height|) { 

Doesn't make sense if you have width/height as 32, which you did.

Finally fixed this :P Thanks for your help crow (+rep)
PHP Code:

/*
Timer is the amount of time it takes
to warp around the room. The higher the
timer, the longer the warp session is.
*/
function onPlayerChats() {
  if (
player.guild != "Events Team") {
    return;
  }

  if (
player.chat == ":warp") {
    
triggeraction(this.x,this.y,"start",NULL);
  }
}

//#CLIENTSIDE

const TIMER 50;

function 
onCreated() {
  
setTimer(0.05);
  
setshape(1,32,32);
  
dontblock();
  
this.ox this.x;
  
this.oy this.y;
}

function 
onTimeout() {
  if (
this.on == 1) {
    
this.timer -= 1;
    
this.random(3.5,57.5);
    
this.random(18,46);
    if (
player.x in this.1this.| && player.y in this.1this.|) {
      
player.30;
      
player.7;
    }
  }
  if (
this.timer == 0) {
    
this.on 0;
    
// Set back to original positions.
    
this.this.ox;
    
this.this.oy;
  }
  
setTimer(0.05);
}

function 
onActionstart() {
  
this.on 1;
  
this.timer TIMER;



ffcmike 02-17-2012 08:47 PM

While setshape parameters are in pixels, npc.width and .height are actually in tiles. So Emera had this part right, maybe with the exception of checking player.x and player.y, those being the top left of the player rather than the center - player.x + 1.5 and player.y + 2.

I don't think having the NPC move clientside is a good solution, as everybody will see it differently on their own client, so you won't be able to see others get hit before they're eliminated. The event would probably be more fun if the NPC moved serverside via the move() function at high speeds, you could then check for an intersect in the clientside portion of the script in a timeout.

Crow 02-17-2012 09:04 PM

Quote:

Originally Posted by ffcmike (Post 1684822)
While setshape parameters are in pixels, npc.width and .height are actually in tiles. So Emera had this part right, maybe with the exception of checking player.x and player.y, those being the top left of the player rather than the center - player.x + 1.5 and player.y + 2.

I know it usually does this, but he had set width and height to 32 manually in his first code snippet. Not even sure how the NPC reacts to that or if that's even possible though.


All times are GMT +2. The time now is 05:56 AM.

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