Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   player.join and triggeraction? (https://forums.graalonline.com/forums/showthread.php?t=134257975)

scriptless 02-11-2010 11:28 AM

player.join and triggeraction?
 
I am curious as to how I would use a triggeraction to trigger an action in a joined class? Or is this not possible?

Class:
PHP Code:

//#CLIENTSIDE
public function onActionHurt(acct) {
  
player.hearts -= 0.5;
  
player.chat "hurt by" SPC acct;
  
setani"human2_hurt"NULL);


Weapon:
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
player.join("hurt");
}
function 
onKeyPressed() {
  
ar = {player.+ (vecx(player.dir) * 2) + 1.5player.+ (vecy(player.dir) * 2) + 1.8};
  
triggeractionar[0], ar[1], "Hurt"player.account);


I have 2 accounts logged in. But only 1 account can hit the other. Is there any reason for this?

ffcmike 02-11-2010 12:36 PM

Quote:

Originally Posted by scriptless (Post 1555600)
I am curious as to how I would use a triggeraction to trigger an action in a joined class? Or is this not possible?

Class:
PHP Code:

//#CLIENTSIDE
public function onActionHurt(acct) {
  
player.hearts -= 0.5;
  
player.chat "hurt by" SPC acct;
  
setani"human2_hurt"NULL);


Weapon:
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
player.join("hurt");
}
function 
onKeyPressed() {
  
ar = {player.+ (vecx(player.dir) * 2) + 1.5player.+ (vecy(player.dir) * 2) + 1.8};
  
triggeractionar[0], ar[1], "Hurt"player.account);


I have 2 accounts logged in. But only 1 account can hit the other. Is there any reason for this?

While triggeraction can be used to trigger directly to the clients of players at a given coordinate I don't believe this is possible no, I believe they have to be recieved within weapons.
I'd suggest moving the "onActionHurt" into a weapon, but then simply calling a player.injured() or whatever in it's place within the class once recieved.

cbk1994 02-11-2010 01:53 PM

It is usually best to do the entire thing serverside


PHP Code:

function attack(dxdy) {
  for (
temp.pl findNearestPlayers(dxdy)) {
    if (! (
dx in |player.xplayer.3| && dy in |player.yplayer.3|)) {
      continue;
    }
    
    if (
pl == player) { // if it's the attacking player, skip
      
continue;
    }
    
    
pl.hit(0.5player);
  }


then in the player class,

PHP Code:

public function hit(damagepl) {
  
this.hearts -= damage;
  
this.chat "Hurt by: " pl.account;
  
this.setAni("human2_hurt"null);



However, if you choose to do it clientside, you will need a weapon to intercept the trigger. Once you intercept it clientside in a weapon, you will need to trigger serverside again in that weapon then call the player function as player.hearts can't be modified clientside.

ffcmike 02-11-2010 02:17 PM

Quote:

Originally Posted by cbk1994 (Post 1555618)
It is usually best to do the entire thing serverside


PHP Code:

function attack(dxdy) {
  for (
temp.pl findNearestPlayers(dxdy)) {
    if (! (
dx in |player.xplayer.3| && dy in |player.yplayer.3|)) {
      continue;
    }
    
    if (
pl == player) { // if it's the attacking player, skip
      
continue;
    }
    
    
pl.hit(0.5player);
  }


then in the player class,

PHP Code:

public function hit(damagepl) {
  
this.hearts -= damage;
  
this.chat "Hurt by: " pl.account;
  
this.setAni("human2_hurt"null);



However, if you choose to do it clientside, you will need a weapon to intercept the trigger. Once you intercept it clientside in a weapon, you will need to trigger serverside again in that weapon then call the player function as player.hearts can't be modified clientside.

I think this is intended to be a Clientside Hit Detection, using the triggeraction method would only result in the function occuring if your player is positioned on the given coordinate at the time of the trigger being recieved on your own client.

I think the hearts variable can -falsely- be modified Clientside albeit for the illusion of no delay, which should be done along side the setting of the Gani, but yes hearts should definately be modified serverside either way.
(Edit: having tested it seemed hearts had saved having set a new value Clientside and reconnected)

fowlplay4 02-11-2010 05:14 PM

I join a damage class script to the weapon (not the player), and just make sure both players have the weapon, then it works fine.

scriptless 02-11-2010 10:20 PM

Revised. Still not working.

PHP Code:

function punch(dxdy) {
  
player.chat "2";
  if ( 
player.ani != "human2_swim" ) {
    if ( 
player.ani != "human2_hthc_punch" setani"human2_hthc_punch"null);
    
freezeplayer(.5);

  for (
temp.pl findNearestPlayers(dxdy)) {
    if (! (
dx in |player.xplayer.3| && dy in |player.yplayer.3|)) {
      continue;
    }
    if (
pl == player) continue;
    
pl.hit(0.5player);
  }

    
waitforGraalControl"punch".5);
    if ( 
this.keyb_d == true ) {
      
setani"human2_shield_hold"null );
      
onshield();
    } else  
setani"human2_idle" null );
  }
}

function 
onCreated() {
  
disableweapons();
  
addtiledef("picso2.png","psi_"0);
  
enablefeaturesallfeatures ---0x10 -0x20 );
  
showstats(256 512 1024);
  
  
// allow ourselves to hurt :o
  
player.join("hurt");
}

//#CLIENTSIDE
function GraalControl.onKeyDown(keycodekeychar) {
  
this.("keyb_" keychar) = true;
  switch( 
keychar ) {
    case 
"s":
      
player.chat "1";
      
punch(player.xplayer.y);
    break;
    case 
"d":
      
setani"human2_shield_hold"null );
      
this.playerdir player.dir;
      
onshield();
    break;
  }
  if ( 
keycode == ) {
    
this.keyb_d false;
    
setani"human2_idle"null );
    
shield();
  }



cbk1994 02-12-2010 12:21 AM

Quote:

Originally Posted by scriptless (Post 1555687)
Revised. Still not working.

You can't call serverside functions like punch() directly. Instead you have to trigger serverside using the function triggerserver, like so:

PHP Code:

// this is the event invoked serverside by triggerserver
function onActionServerSide(cmd) {
  if (
cmd == "punch") {
    
punch(player.xplayer.y);
  }
}

function 
punch(dxdy) {
  
player.chat "2";
  if ( 
player.ani != "human2_swim" ) {
    if ( 
player.ani != "human2_hthc_punch" setani"human2_hthc_punch"null);
    
freezeplayer(.5);

  for (
temp.pl findNearestPlayers(dxdy)) {
    if (! (
dx in |player.xplayer.3| && dy in |player.yplayer.3|)) {
      continue;
    }
    if (
pl == player) {
      continue;
    }
    
    
pl.hit(0.5player);
  }

   
// I'm not sure what this is supposed to do?
   /* waitfor( GraalControl, "punch", .5);
    if ( this.keyb_d == true ) {
      setani( "human2_shield_hold", null );
      onshield();
    } else  setani( "human2_idle" , null );
  }*/
}

// normally it is best to use the onActionPlayerOnline even evoked in
// Control-NPC since onPlayerLogin() isn't called for each player
// if the NPC-server restarts, which causes problems since
// classes don't join
function onPlayerLogin(pl) { // onCreated is only called when the script is updated
  // allow ourselves to hurt :o
  
pl.join("hurt");
}

//#CLIENTSIDE
function onCreated() {
  
// all of this has to be done clientside
  
disableweapons();
  
addtiledef("picso2.png","psi_"0);
  
enablefeaturesallfeatures ---0x10 -0x20 );
  
showstats(256 512 1024);
}
function 
GraalControl.onKeyDown(keycodekeychar) {
  
this.("keyb_" keychar) = true;
  switch( 
keychar ) {
    case 
"s":
      
player.chat "1";
      
triggerServer("gui"this.name"punch"); // tell serverside to punch
    
break;
    case 
"d":
      
setani"human2_shield_hold"null );
      
this.playerdir player.dir;
      
onshield();
    break;
  }
  if ( 
keycode == ) {
    
this.keyb_d false;
    
setani"human2_idle"null );
    
shield();
  }


A brief explanation of clientside and serverside:

Clientside is code that is ran directly on the players computer. This allows you to create effects, show images for only that player (including things like health interfaces), do things when the player presses a key, etc. The benefit to clientside code is that there is no delay between when the player tries to do something and when it happens. For example, if you had a clientside punching system, they would immediately see the punch. The disadvantage to clientside code is that it is run on the player's computer, and therefore it can be modified to some degree. It is very easy to disable onwall checks in a clientside movement system, for example. Because clientside code is local, it can't communicate with other players.

Serverside is code that is ran on the NPC-server. This allows you to create solid systems that can interact with every player. An example of serverside code would be a "monster" like on GK which runs serverside and sends back the updates such as position, GANI, and so on, where it is displayed clientside. The advantage to serverside scripting is that it is secure and can't be tampered with by any player. You can also communicate with other players. The disadvantage is that there is a "lag" time between when the user does something and when it happens, depending on the player's internet connection.


To communicate between the two you have to use things called triggers. There's a pretty good explanation of those in this thread.

scriptless 02-12-2010 12:48 AM

Thank you ckb. And the part you didnt understand was just some checks. Basically im using a system to check if a key is pressed or not. "this.keyb_" then the keychar. because tab+key seems to break alot of scripts under windows (not on linux from what i experienced).

Problem I am having now is you do not even have ot be remotly close to players to hit them.. and my sword seems to popoyt when i swing now. this did not happen before tho =/

Also, freezeplayer() no longer works. Possible because its being used serverside.

Quote:

Script: Function freezeplayer not found at line 11 in script of -System (in level onlinestartlocal.graal at pos (0, 0))
Or at least according to the link u provided. So I can fix that, however the player hitting from across the server is a problem I need help fixing lol

fowlplay4 02-12-2010 02:56 AM

Little note about onCreated() on the server-side in weapon scripts, it is called when the server starts up too, not just when it's updated.

cbk1994 02-12-2010 03:20 AM

How inaccurate is it? Right not it's just using the player's x and y instead of the attack x/y, but if it's like a 10 tile difference, that's obviously not it.

scriptless 02-12-2010 03:35 AM

i dont think its checking x/y at all. I mean were talking 64 tile difference in this level :o

No further need to reply.. i have fired myself from all development servers (except testbed) and devborn2kill.. didnt have rights to fully remove all my rights (ironic i know)

cbk1994 02-12-2010 04:11 AM

Well, in case anybody was wondering, the problem was this line:

PHP Code:

if (! (dx in |player.xplayer.3| && dy in |player.yplayer.3|)) { 

It should have read:

PHP Code:

if (! (dx in |pl.xpl.3| && dy in |pl.ypl.3|)) { 



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

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