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 01-04-2014, 02:08 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
NPC checking for player

As you all know, the onPlayerTouchsMe() only works if the player runs into the NPC.

How would I script the NPC to check if the player is in the x, y of the object.
In this case, block.png

PHP Code:
function onCreated(){
 
setImg("block.png");
 
setTimer(1);
}

function 
onTimeOut(){
 
destroy();
}

function 
onPlayerTouchsMe(){
 
player.chat "HIT";

Reply With Quote
  #2  
Old 01-04-2014, 04:34 PM
Starfire2001 Starfire2001 is offline
Unholy Nation
Starfire2001's Avatar
Join Date: Dec 2010
Location: The streets.
Posts: 156
Starfire2001 will become famous soon enough
You need continuously to check the players position in a loop to see if they are within the bounds of the block. If you are looking to do it clientside...

PHP Code:
//#CLIENTSIDE
function onCreated() {
  
setImg("block.png");
  
onTimeout();  // probably better than setting a timer here as you might not want to wait a second before checking
}

function 
onTimeout() {
  
// assuming that your block's dimensions are 2x2
  
if (player.x in |this.xthis.2| && player.y in |this.ythis.2|) {
    
//destroy or hide the block or whatever you want to do
  
} else {
    
setTimer(.05); // or higher if it doesn't need to be so accurate
  
}

If you want to do it serverside you'll have to loop through the players array in your timeout and check if any of them are within the bounds of the block.
__________________
-Ph8
Reply With Quote
  #3  
Old 01-04-2014, 10:27 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 Starfire2001 View Post
PHP Code:
  // assuming that your block's dimensions are 2x2
  
if (player.x in |this.xthis.2| && player.y in |this.ythis.2|) { 
Just a note that you can use this.width and this.height to get the width/height of an NPC with a defined shape (like an image).
__________________
Reply With Quote
  #4  
Old 01-05-2014, 01:51 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 cbk1994 View Post
Just a note that you can use this.width and this.height to get the width/height of an NPC with a defined shape (like an image).
changing the image though (like a 1x1 pixel one) will also alter this.width/height clientside though, that way players would be able to cheat through blocks
__________________
MEEP!
Reply With Quote
  #5  
Old 01-05-2014, 02:13 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
Quote:
Originally Posted by callimuc View Post
changing the image though (like a 1x1 pixel one) will also alter this.width/height clientside though, that way players would be able to cheat through blocks
If you're worried about cheating players, then you can't trust anything that happens clientside. You're right that that would be one of the easier ways to cheat, though.
__________________
Reply With Quote
  #6  
Old 01-05-2014, 03:12 AM
Torankusu Torankusu is offline
Elite Member
Torankusu's Avatar
Join Date: Jun 2001
Posts: 10,065
Torankusu has a spectacular aura aboutTorankusu has a spectacular aura about
don't want to seem like i'm giving advice, because I am not, and I also don't know what the OP. wants to do with this, since it's so vague and there might be a better approach, but here's something i came up with on a serverside approach to at least give an array of players in that object's coordinates.

Quote:
Originally Posted by Using...
TServerLevel.findareaplayers(float, float, float, float) - returns object - returns an array of all players in the specified rectangle (x,y,width,height), uses the blocking rectangle of players (0.5,1,2,2) instead of (0,0)
PHP Code:
// Created by Torankusu

function onCreated()
{
  
setshape(1,32,32);
  
this.image "block.png";
  
drawunderplayer();
  
dontblock();
  
setTimer(0.1);
}

function 
onTimeout()
{
  
temp.pls this.level.findareaplayers(this.x,this.y,2,2);
  
this.chat temp.pls;
  
setTimer(0.1);

the object's chat in this example will display the array of players that are in the x,y of that object.
__________________
Quote:
Originally posted by Spark910
Think befreo you type.
Reply With Quote
  #7  
Old 01-05-2014, 03:15 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Quote:
don't want to seem like i'm giving advice, because I am not, and I also don't know what the OP. wants to do with this, since it's so vague and there might be a better approach, but here's something i came up with on a serverside approach to at least give an array of players in that object's coordinates.
I am trying to make "damage blocks" so when the player collides with them, player.clientr.hp is deducted. That's why I want to go serverside. I can see where I was unclear by making the player chat in the end result. I should have been more specific.
Reply With Quote
  #8  
Old 01-05-2014, 04:26 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 i8bit View Post
I am trying to make "damage blocks" so when the player collides with them, player.clientr.hp is deducted. That's why I want to go serverside. I can see where I was unclear by making the player chat in the end result. I should have been more specific.
Just to make sure, you're not using onPlayerTouchsMe() because I'm guessing that both the damage block and the players will be moving?
__________________
Reply With Quote
  #9  
Old 01-05-2014, 06:44 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Quote:
Originally Posted by Emera View Post
Just to make sure, you're not using onPlayerTouchsMe() because I'm guessing that both the damage block and the players will be moving?

Well if the player was at constant motion, the onPlayerTouchsMe() would not be a problem. But it's the players that are on idle that it won't work.
Reply With Quote
  #10  
Old 01-05-2014, 07:08 PM
Jakov_the_Jakovasaur Jakov_the_Jakovasaur is offline
Deleted by Darlene159
Jakov_the_Jakovasaur's Avatar
Join Date: Sep 2013
Location: Deleted by Darlene159
Posts: 353
Jakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud ofJakov_the_Jakovasaur has much to be proud of
you could always make a clientside system which triggers to serverside upon damage to reduce hp or handle deaths, youd just need to make sure that it works securely (such as preventing people who modify packets invoking negative damage aka increased health) and that theres a clientside health variable to give the illusion of real time damage

if you do this then it would be easy to have a single weapon system for checking all box intersects for every enemy, rather than scripting the box intersect checks in a timeout inside all of their scripts
__________________
This signature has been deleted by Darlene159.
Reply With Quote
  #11  
Old 01-05-2014, 11:21 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Quote:
you could always make a clientside system which triggers to serverside upon damage to reduce hp or handle deaths, youd just need to make sure that it works securely (such as preventing people who modify packets invoking negative damage aka increased health) and that theres a clientside health variable to give the illusion of real time damage
Think you could give me an example/scenario?
Reply With Quote
  #12  
Old 01-05-2014, 11:32 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 i8bit View Post
Think you could give me an example/scenario?
The simple solution is just take Starfire's code and modify it to send a trigger...

PHP Code:
//#CLIENTSIDE
function onCreated() { 
  
setImg("block.png"); 
  
onTimeout();  // probably better than setting a timer here as you might not want to wait a second before checking 


function 
onTimeout() { 
  
// assuming that your block's dimensions are 2x2 
  
if (player.x in |this.xthis.2| && player.y in |this.ythis.2|) { 
    
triggerServer("npc""HealthSystem""hitByBlock");
    return 
setTimer(1); // to avoid sending 20 triggers per second
  
}
  
  
setTimer(.05); // or higher if it doesn't need to be so accurate 

and then in a DB NPC HealthSystem...

PHP Code:
function onActionServerSide(temp.cmd) {
  if (
temp.cmd == "hitByBlock") {
    echo(
player.niceName() @ " was hit by a block.");
    
player.hit(1); // or whatever
  
}

You could also send a point trigger (but those can be unreliable) or do this a bunch of other ways. There are some neat tricks you can use to do this more efficiently but I'd need to know more about what you're trying to do to know if they'd work here.

Note that the above makes it possible for cheating players to not be hit by the block, so if you care about that, you should do everything serverside.
__________________
Reply With Quote
  #13  
Old 01-06-2014, 12:47 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
So I took what you said, but modified it a bit

PHP Code:
//#CLIENTSIDE
function onCreated() { 
  
setImg("block.png"); 
  
onTimeout();  
 
scheduleEvent("onDestroy"1);


function 
onTimeout() { 
  if (
player.x in |this.xthis.2| && player.y in |this.ythis.2|) { 
    
triggerServer("weapon""HealthSystem""hitByBlock");
  }
  
  
setTimer(.05); 
}  

function 
onDestroy(){
 
destroy()

Then in the weapon- "HealthSystem"
PHP Code:
function onActionServerSide(){
 if (
params[0] == "hitByBlock"){
  [
PHP]player.clientr.hp -= //whatever amount
  
}
 } 
[/PHP]

I haven't had a chance to test this but I don't see why it wouldn't work

SO my next question is...
How would I check local NPCs (Baddies) to trigger their "Hurt" function with that block too?

I want my "damage block" to not only effect players but NPCs also.
Reply With Quote
  #14  
Old 01-06-2014, 05:30 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
Looks fine to me except you probably don't want to send 20 triggers per second.

Hurting NPCs should probably be done serverside:

PHP Code:
function hurtNPCs() {
  for (
temp.npc findAreaNPCs(this.xthis.y22)) {
    
temp.npc.trigger("hurt");
  }

then you can add an onHurt function in any NPCs which can be hurt.
__________________
Reply With Quote
  #15  
Old 01-06-2014, 07:25 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Quote:
Looks fine to me except you probably don't want to send 20 triggers per second.
Oh yeah, good observation. What if I don't make it loop and it just checks for players/NPCs in that 0.05 time range, triggers the actions, then destroys?
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 07:28 PM.


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