Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   triggeraction all NPC's in the level? (https://forums.graalonline.com/forums/showthread.php?t=134266026)

Emera 03-18-2012 03:43 PM

triggeraction all NPC's in the level?
 
I'm trying to code a hat system, and I'm having a little bit of trouble. To bring up the shop GUI with all the information on the hat, you have to click the hat image in the level. All of the hat images have the same class joined to them, object_hat. Now, I'm using a triggeraction when the player clicks the buy button, and instead of the player buying just that hat, all the NPC's in the level with the class joined to them get the triggeraction too, and I end up buying 6 or 7 hats.

PHP Code:

function onActionBuyHat() {
  
temp.hat_name = params[1];
  
temp.hat_price = params[2];
  if (
temp.hat_name in clientr.questhats.tokenize(",")) {
    return;
  } else {
    if (!(
player.rupees => temp.hat_price)) {
      echo(
player.account SPC "doesn't have enough!");
    } else {
      
player.rupees -= temp.hat_price;
      
player.clientr.questhats.add(temp.hat_name);
    }
  }
} 

This is the triggeraction.
PHP Code:

triggeraction(this.x,this.y,"BuyHat","",this.hat_name,this.hat_price); 

I don't want all the NPC's with the class joined to receive the triggeraction.
Am I doing something wrong? Also, I tend to script something and get it working and then clean the script up a bit, so it's a little messy :P

cbk1994 03-18-2012 04:01 PM

It sounds like you're using the same name for the button for all of the NPCs. You didn't paste enough code to be sure, but I'd bet you're doing something like

PHP Code:

function BuyHat_Buy.onAction() {
  
triggeraction(this.x,this.y,"BuyHat","",this.hat_name,this.hat_price);  
} 

This function is going to be called on every script in the level. The easiest way to fix it is to specify the NPC when creating the window:

PHP Code:

// in the npc that creates the window
BuyHat_Window.npc = this; 

and then in the event:
PHP Code:

function BuyHat_Buy.onAction() {
  if (
BuyHat_Window.npc == this) {
    
triggeraction(this.x,this.y,"BuyHat","",this.hat_name,this.hat_price);
  }
} 


Emera 03-18-2012 04:05 PM

Yeah, that's what I'm doing. Thanks for the help!

Emera 03-18-2012 04:18 PM

I'm having some issues. It's not sending the trigger at all now. I don't know what I'm doing wrong. Here's the whole code. It wouldn't let me post it using php tags since incapsula got me D:

http://pastebin.com/p4p4MTFF

ffcmike 03-18-2012 04:59 PM

I would place the GUI display and action script within a weapon, have the NPC call a function to that weapon passing itself as a parameter, then store that reference within the GUI object, and refer to it upon the action.

Weapon:

PHP Code:

function onCreated(){
  
SomeWeaponReference = this;
}

public function 
displayBuyHat(temp.owner){
  new 
GUIControl(BuyHat_Buy){
    
this.owner = temp.owner;
  }
}

function 
BuyHat_Buy.onAction(){
  
BuyHat_Buy.owner.buyHatPressed();
  
BuyHat_Buy.destroy();
} 

NPC:

PHP Code:

function onPlayerTouchsMe(){
  
SomeWeaponReference.displayBuyHat(this);
}

public function 
buyHatPressed(){
  
triggeraction(stuff);
} 


Emera 03-18-2012 05:14 PM

Oh, I see what you mean :P Thanks <3 I'll tweak mah code then

Edit:
Whoever it was who gave me a red pepper saying "worst scripter 2012", I'm in the process of something called "learning", and also the negative reputation for Graal pastebin, my browser refused to load it after 10 minutes of trying D: Sorry about that. I would post the old code on pastebin since it's working now, but I don't see the point since I've solved the issue already!


All times are GMT +2. The time now is 07:03 AM.

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