Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Problem using a level NPC that's joined to a class (https://forums.graalonline.com/forums/showthread.php?t=134264114)

Sp_lit 08-05-2011 11:46 PM

Problem using a level NPC that's joined to a class
 
Hey there!
This is me and my friend's first attempt at GS2. This is what we've got so far. We're just wondering; are we on the right track here? We have encountered some problem with triggeraction when trying to trigger serverside from clientside, in a level NPC.

Any ideas or suggestions on what to improve or totally change?
In case the aweful script does not tell; we're trying to create a shop system..or something closely remote to one...

Help is very much appreciated, thanks!

Class:
PHP Code:

function onActionBuyItem()
{
  
player.rupees 0;
}

function 
onCreated() {
  
this.setShape(13232);
  
this.dontblock();
}

//#CLIENTSIDE
function onPlayerTouchsMe()
{
  new 
GuiBitmapBorderCtrl("Shop_Window"
  {
    
this.profile GuiBitmapBorderProfile;
    
this.width screenwidth 2;
    
this.height screenheight 2;
    
this.screenwidth this.width 2;
    
this.screenheight this.height 2;
    
this.canmove false;
    
this.canresize false;
    
this.makefirstresponder(true);
    
    new 
GuiButtonCtrl("Shop_Button")
    {
      
width 100;
      
height 40;
      
Shop_Window.width this.width 2;    
      
Shop_Window.height this.height 10;
      
text "Buy";
    }
        
    new 
GuiShowImgCtrl("Shop_Item_Fireball"
    {
      
20;
      
20;
      
width 60;
      
height 60;
      
      
this.imageselected "split_fireball_selected.png";
      
this.imagenotselected "split_fireball.png";
      
this.image this.imageselected;
      
      
this.price 20;
    }
    
    new 
GuiShowImgCtrl("Shop_Item_Sword"
    {
      
Shop_Item_FireBall.Shop_Item_Fireball.width 20;
      
20;
      
width 60;
      
height 60;
      
      
this.imageselected "split_sword_selected.png";
      
this.imagenotselected "split_sword.png";
      
this.image this.imagenotselected;
      
      
this.price 30;
    }
  }  
  
this.listofitems = {Shop_Item_FireballShop_Item_Sword};
  
this.itemselected this.listofitems[0];
}

function 
onMouseDown()
{
  if(!((
mousescreenx in |Shop_Window.xShop_Window.Shop_Window.width|)
      &&(
mousescreeny in |Shop_Window.yShop_Window.Shop_Window.height|)))
  {
    
Shop_Window.destroy();
  }
}

function 
Shop_Button.onMouseDown()
{
  
triggeraction(this.0.5this.0.5"ButItem"null);
}

function 
Shop_Button.onMouseUp()
{
  
Shop_Window.makefirstresponder(true);
}

function 
Shop_Item_Fireball.onMouseDown()
{
  for(
0this.listofitems.size(); ++)
  {
    
this.listofitems[i].image this.listofitems[i].imagenotselected;   
    if(
this.listofitems[i] == Shop_Item_Fireball)
      
this.itemselected this.listofitems[i];
  } 
  
Shop_Item_Fireball.image Shop_Item_Fireball.imageselected;
}

function 
Shop_Item_Sword.onMouseDown()
{
  for(
0this.listofitems.size(); ++)
  {
    
this.listofitems[i].image this.listofitems[i].imagenotselected;   
    if(
this.listofitems[i] == Shop_Item_Sword)
      
this.itemselected this.listofitems[i];
  }
  
Shop_Item_Sword.image Shop_Item_Sword.imageselected;


NPC:
PHP Code:

this.join("shopkeeper"); 


ffcmike 08-05-2011 11:48 PM

Quote:

Originally Posted by Sp_lit (Post 1662089)
triggeraction(this.x + 0.5, this.y + 0.5, "ButItem", null);

Looks like a mere typo.

Sp_lit 08-06-2011 12:03 AM

Quote:

Originally Posted by ffcmike (Post 1662091)
Looks like a mere typo.

Oh well..took you two minutes, took us four hours..

Just wondering; what do you think about the structure of our shop system? Any thoughts or suggestions?

ff7chocoboknight 08-06-2011 12:05 AM

Box formatting is the devil.

ffcmike 08-06-2011 12:10 AM

Quote:

Originally Posted by Sp_lit (Post 1662089)
PHP Code:

  for(0this.listofitems.size(); ++) 


This is being a little bit picky and won't really make any difference in this particular instance but it's more efficient to avoid using .size(); within an array that doesn't change value in this type of loop.

With this type of loop where you're accessing 'i' it would take less script time to do:

PHP Code:

temp.this.listofitems.size();
for(
0temp.s++) 

This is albeit something that would be worthwhile when it comes to looping through much larger arrays, the reason being is that the .size(); function would be processed on every part of the loop, which takes longer with larger arrays.

WhiteDragon 08-06-2011 12:34 AM

You forgot temp.s on your is.

Sp_lit 08-06-2011 03:46 PM

Quote:

Originally Posted by ff7chocoboknight (Post 1662096)
Box formatting is the devil.

I bet it is, since I barely understand what it means..haha

Could you please expand or elaborate a bit on the meaning of Box formatting? :)

I suppose our structure is not that good?

And thanks for all the replies and comments so far!

ff7chocoboknight 08-06-2011 03:57 PM

PHP Code:

function Box()
{
  
this.herp "derp";
}

function 
notBox() {
  
this.hurr "durr";



xXziroXx 08-06-2011 04:04 PM

Box formatting is the ugliest thing to read through ever, not to mention how it expands the code for no good reason. :rolleyes:

Adddeeee 08-06-2011 04:57 PM

Hi!

This is what we've got so far. We have now updated the script so that our trigger action triggers onActionBuyItem.

Although, we have a variable called this.itemselected, which will store information regarding the currently selected item.

this.itemselected contains one of the two available gui controls: Shop_Item_Fireball or Shop_Item_Sword.

The gui looks just fine when running the script. We can click on the different items and the currently selected item changes.

Although, when we trigger the server via triggeraction, the gui control which is stored in this.itemselected disappears. We've noticed that this happens when we use this.itemselected as a parameter for triggeraction. If we use a string, for example "higuys" as parameter to the triggeraction everything works just fine.

I get a feeling that we're not ready to build a shop system!

PHP Code:

function onActionBuyItem(item)
{
  
player.chat item;  
}

function 
onCreated() 
{
  
this.setShape(13232);
}

//#CLIENTSIDE
function onPlayerTouchsMe()
{
  new 
GuiBitmapBorderCtrl("Shop_Window"
  {
    
this.profile GuiBitmapBorderProfile;
    
this.width screenwidth 2;
    
this.height screenheight 2;
    
this.screenwidth this.width 2;
    
this.screenheight this.height 2;
    
this.canmove false;
    
this.canresize false;
    
this.makefirstresponder(true);
    
    new 
GuiButtonCtrl("Shop_Button")
    {
      
width 100;
      
height 40;
      
Shop_Window.width this.width 2;    
      
Shop_Window.height this.height 10;
      
text "Buy";
    }
        
    new 
GuiShowImgCtrl("Shop_Item_Fireball"
    {
      
20;
      
20;
      
width 60;
      
height 60;
      
      
this.imageselected "split_fireball_selected.png";
      
this.imagenotselected "split_fireball.png";
      
this.image this.imageselected;
      
      
this.price 20;
    }
    
    new 
GuiShowImgCtrl("Shop_Item_Sword"
    {
      
Shop_Item_FireBall.Shop_Item_Fireball.width 20;
      
20;
      
width 60;
      
height 60;
      
      
this.imageselected "split_sword_selected.png";
      
this.imagenotselected "split_sword.png";
      
this.image this.imagenotselected;
      
      
this.price 30;
    }
  }
  
Shop_Window.show();
  
this.listofitems = {Shop_Item_FireballShop_Item_Sword};
  
this.itemselected this.listofitems[0];
}

function 
onMouseDown()
{
  if(!((
mousescreenx in |Shop_Window.xShop_Window.Shop_Window.width|)
      &&(
mousescreeny in |Shop_Window.yShop_Window.Shop_Window.height|)))
  {
    
Shop_Window.hide();
  }
}

function 
Shop_Button.onMouseDown()
{
  
triggeraction(this.0.5this.0.5"BuyItem"this.itemselected);
  
}

function 
Shop_Item_Fireball.onMouseDown()
{
  
temp.this.listofitems.size();
  
  for(
0temp.s++)
  {
    
this.listofitems[i].image this.listofitems[i].imagenotselected;   
    if(
this.listofitems[i] == Shop_Item_Fireball)
      
this.itemselected this.listofitems[i];
  } 
  
Shop_Item_Fireball.image Shop_Item_Fireball.imageselected;
}

function 
Shop_Item_Sword.onMouseDown()
{
  
temp.this.listofitems.size();
  
  for(
0this.listofitems.size(); ++)
  {
    
this.listofitems[i].image this.listofitems[i].imagenotselected;   
    if(
this.listofitems[i] == Shop_Item_Sword)
      
this.itemselected this.listofitems[i];
  }
  
Shop_Item_Sword.image Shop_Item_Sword.imageselected;



xXziroXx 08-06-2011 05:31 PM

If I understand it correctly, this.itemselected is a GUI control, yeah? GUI controls are a clientsided object and thus can't be read on serverside.

Adddeeee 08-06-2011 05:35 PM

Yepp. Since this.itemselected contains a gui control. But when I add one more parameter to the triggeraction it works just fine.

triggeraction(this.x + 0.5, this.y + 0.5, "BuyItem", this.itemselected, null);

Any thoughts regarding that?

fowlplay4 08-06-2011 07:00 PM

You can cut down on your 'itemSelected' code with catchevent. I.e:

PHP Code:

new GuiShowImgCtrl("Shop_Item_Fireball") {
  
// existing code...
  
thiso.catchevent(this.name"onMouseDown""onItemSelected");
}

// other code...

// The first parameter passed from a 'caught event' is the object that received it.
function onItemSelected(selected) {
  
// Loop through Items
  
for (temp.itemthis.listofitems) {
    
// Change Image 
    
temp.item.image = (temp.item == selected temp.item.imageselected temp.item.imagenotselected);
  }
  
// Set Selected to Item's Name (w/o the Shop_Item_ part)
  
this.itemselected selected.name.substring("Shop_Item_".length());


I also used the ternary operator, which is just: value = (condition ? true_result : false_result);

It would also fix your problem where you're passing an object to the server-side instead of string.

Objects behave weirdly (as you can see) resulting in their name getting passed or it passes null/0.

Adddeeee 08-06-2011 07:12 PM

Thank you very much!

I was a little bit concerned about how to solve that problem.

I'm also a little bit concerned about where to put the raw data.

I would like to create an array consisting of arrays to keep track of all the items:

//{Gui name, name, price}
this.shop_item_fireball = {"Shop_Item_Fireball", "Fireball", 30};

and then create an array called

this.listofItems = {this.shop_item_fireball};

When this is done on the server side I would like to pass that information to the clientside so that the GUI can be built.

Would that be inefficient? Or what do you think?

fowlplay4 08-06-2011 07:55 PM

Your best bet would be to do something like this:

PHP Code:

function onCreated() {
  
this.items = {
    {
"Item Name""Description""Price"}
  };
  
this.join("shopkeeper");
}

//#CLIENTSIDE

function onCreated() {
  
this.items = {
    {
"Item Name""Description""Price"}
  };  


It's not very DRY, but transferring data from the server-side to the client-side in a level npc can be a un-needed pain in the ass. It'll be more instant this way anyway.

Then in your shopkeeper class just process the this.items array and create the list you need. Also on the server-side verify their purchase against the server-side items array.


All times are GMT +2. The time now is 04:14 PM.

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