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.

xXziroXx 08-06-2011 08:36 PM

Quote:

Originally Posted by fowlplay4 (Post 1662229)
transferring data from the server-side to the client-side in a level npc can be a un-needed pain in the ass

Uh, why? :noob:

fowlplay4 08-06-2011 09:17 PM

Quote:

Originally Posted by xXziroXx (Post 1662236)
Uh, why? :noob:

You'd have to rig something together using a trigger system or attributes. For the fairly new scripter that task could be overwhelming and/or adds un-needed complexity right now.

Whereas if you store it twice, once on the server-side and once on the client-side. You have access to it on the client-side right away without sending it through your server-to-client data routing system.

Then when his shop is working the way he wants, he can always go in after and setup a way to sync the shop data to the client.

Adddeeee 08-08-2011 10:12 PM

This is what we've got so far!

Although I'm interested in re-doing the shop with DB NPC's instead of putting all the item information in the shop class. Do you think that would be more efficient or is this a good solution? Don't really know how to set it all up using DB NPC's yet though.

PHP Code:

function onActionBuyItem(cmdselected)

  if(
cmd == "Buy")
  {
    for(
temp.itemthis.listofitems)
    {
      if(
temp.item[0] == selected)
      {
        if(
player.rupees temp.item[3])
        {
          
player.chat "Not enough rupees!";
        }
        else
        {
          
player.chat "Bought " temp.item[1] @ "!";
          
player.rupees player.rupees temp.item[3];
          
this.money this.money temp.item[3];
          
this.chat this.money;
          
player.addweapon(temp.item[2]);
        }
      }
    }
  }
}

function 
onCreated()
{
  
this.setShape(13232); 
  
this.listofitems = {{"Shop_Item_Fireball""Fireball""split_fireball"30}, 
                      {
"Shop_Item_Sword""Sword""split_sword"20},
                      {
"Shop_Item_Bow""Bow""split_bow"10}
                     };
}

//#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;
    
    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 32;
      
height 32;
      
      
this.imageselected "split_fireball_selected.png";
      
this.imagenotselected "split_fireball.png";
      
this.image this.imageselected;
      
thiso.catchevent(this.name"onMouseDown""onItemSelected");
    }
    
    new 
GuiShowImgCtrl("Shop_Item_Sword"
    {
      
Shop_Item_Fireball.Shop_Item_Fireball.width 20;
      
20;
      
width 32;
      
height 32;
      
      
this.imageselected "split_sword_selected.png";
      
this.imagenotselected "split_sword.png";
      
this.image this.imagenotselected;
      
thiso.catchevent(this.name"onMouseDown""onItemSelected");
    }
    
    new 
GuiShowImgCtrl("Shop_Item_Bow"
    {
      
Shop_Item_Sword.Shop_Item_Sword.width 20;
      
20;
      
width 32;
      
height 32;
      
      
this.imageselected "split_bow_selected.png";
      
this.imagenotselected "split_bow.png";
      
this.image this.imagenotselected;
      
thiso.catchevent(this.name"onMouseDown""onItemSelected");
    }
  }
  
Shop_Window.show();
  
this.listofitems = {Shop_Item_FireballShop_Item_SwordShop_Item_Bow};
  
this.itemselected this.listofitems[0].name;
}

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""Buy"this.itemselected);
  
Shop_Window.makefirstresponder(true);
}

function 
onItemSelected(selected
{
  for(
tempi 0temp.this.listofitems.size();temp.++) 
  {
    
temp.item this.listofitems[i];
    
temp.item.image = (temp.item == selected temp.item.imageselected temp.item.imagenotselected);
  }
  
  
this.itemselected selected.name;



cbk1994 08-08-2011 10:26 PM

You can use += and -= to add/subtract from a value (purely based on your preference, but most people do that).

Besides that, the only thing that sticks out is the way you're forming your array. Rather than looping through an array, use it like a hash map.

PHP Code:

function onCreated()
{
  
this.setShape(13232);
  
this.item.fireball = {"Fireball""split_fireball"30};
  
this.item.sword = {"Sword""split_sword"20};
  
this.item.bow = {"Bow""split_bow"10};


Then, to access it, you can do:

PHP Code:

temp.itemInfo this.item.(@ selected); 


Adddeeee 08-08-2011 10:34 PM

Okay, thank you very much :)

So you don't think we need to re-build it using DB NPCs?

We don't really know how to use a DB NPC properly but I understand that's the way the "pro's" are doing it?

Thank you :)

cbk1994 08-08-2011 11:21 PM

I would recommend a DB NPC only because it will let you keep one copy of the data rather than keeping it in a class joined to many NPCs. As a general rule, I would recommend not storing data in classes.

Adddeeee 08-09-2011 12:26 AM

Then we will try that solution :)

If we use a DB NPC named DB_Items we could store all the information regarding the items in that NPC.

But what happens with the clientside of the level NPC? Shall we have a copy of all the items information on the clientside? Because we can't read the DB NPCs information from the client side right?

Thank you :)

Mark Sir Link 08-09-2011 12:36 AM

Quote:

Originally Posted by Adddeeee (Post 1662565)
Then we will try that solution :)

If we use a DB NPC named DB_Items we could store all the information regarding the items in that NPC.

But what happens with the clientside of the level NPC? Shall we have a copy of all the items information on the clientside? Because we can't read the DB NPCs information from the client side right?

Thank you :)

triggerclient a weapon with the data you want to send, IE, have a class join the generic shop class, have that triggerserver the NPC when you want the shop to open, then have the NPC trigger a weapon the player has that opens the shop GUI with the information provided. Can have a trigger in the class as well to hide the shop should the player leave the level or get too far away.


PHP Code:

triggerserver("npc""DB_Items""getitems"//possibly level name as well to restrict what shops have what items

triggerclient("gui"shopweaponname"senditems"this.items); 


Adddeeee 08-09-2011 12:52 AM

Ah I see, will try this later on when I get the GUI to be more flexible :) Sorry for asking so many dumb questions, but I'm kind of stuck both on the DB NPC and on the GUI part!

Why doesn't this work:

PHP Code:

  this.listofitems = {{"Fireball""split_fireball""split_fireball.png""split_fireball_selected.png"30}, 
                      {
"Sword""split_sword""split_sword.png""split_sword_selected.png"20},
                      {
"Bow""split_bow""split_bow.png""split_bow_selected.png"10}
                     }; 
  new 
GuiShowImgCtrl(this.listofitems[0][0]) 
  {
    
20;
    
20;
    
width 32;
    
height 32;
    
    
this.imageselected this.listofitems[0][3];
    
this.imagenotselected this.listofitems[0][2];
    
this.image this.imageselected;
    
thiso.catchevent(this.name"onMouseDown""onItemSelected");
  }
  
Shop_Window.addControl(this.listofitems[0][0]);
  
Shop_Window.show(); 

when this works just fine:

PHP Code:

  this.listofitems = {{"Fireball""split_fireball""split_fireball.png""split_fireball_selected.png"30}, 
                      {
"Sword""split_sword""split_sword.png""split_sword_selected.png"20},
                      {
"Bow""split_bow""split_bow.png""split_bow_selected.png"10}
                     };

  new 
GuiShowImgCtrl(this.listofitems[0][0]) 
  {
    
20;
    
20;
    
width 32;
    
height 32;
    
    
this.imageselected "split_fireball_selected.png";
    
this.imagenotselected "split_fireball.png";
    
this.image this.imageselected;
    
thiso.catchevent(this.name"onMouseDown""onItemSelected");
  }
  
Shop_Window.addControl(this.listofitems[0][0]);
  
Shop_Window.show(); 

The only difference is that in the first code sample, I read the string from the array listofitems and in the second code sample I write the string directly.

Hmm...

Thank you :)

fowlplay4 08-09-2011 12:55 AM

'this' refers to the GuiShowImgCtrl instead of the level npc or weapon. You have to use thiso to access your listofitems.

I would recommend doing.. new GuiShowImgCtrl("Split_ShopItem_" @ this.listofitems[0][0]) instead of just using the item's name as well.

Adddeeee 08-09-2011 12:58 AM

Omg, thank you! Getting a little tired ;D

Adddeeee 08-09-2011 06:34 PM

To start with I want to thank you all for your replies and good support :) We're closing in on the main goal!

We don't have a DB just yet, but we're close to it...I think!

This is what we've got so far:

Class shop
PHP Code:

function onCreated()
{
   
setShape(13232);
}

function 
onPlayerTouchsMe()
{
  
this.listofitems = {{"Fireball""split_fireball""split_fireball.png""split_fireball_selected.png"30}, 
                        {
"Sword""split_sword""split_sword.png""split_sword_selected.png"20},
                        {
"Bow""split_bow""split_bow.png""split_bow_selected.png"10}
                       };
                     
  
triggerclient("gui""-shopcontrol"this.listofitemsthis.xthis.y);
}

function 
onActionBuyItem()
{
   
this.chat "I received your request!";


Class shop serves as our temp DB. We will fix a DB to hold the information about the items. But for now it's just a class :) This class is joined to the NPC who is the shopkeeper. When a player touchs him/her it will send a triggerclient to the shopcontrol weapon:

Weapon -shopcontrol:

PHP Code:


//#CLIENTSIDE
function onActionClientSide(listofitemsnpcxnpcy)
{
  
this.npcx npcx;
  
this.npcy npcy;
  
this.listofitems listofitems;
  
  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;
    
    new 
GuiButtonCtrl("Shop_Button")
    {
      
width 100;
      
height 40;
      
Shop_Window.width this.width 2;    
      
Shop_Window.height this.height 10;
      
text "Buy";
    } 
  }
  
  for(
0this.listofitems.size(); i++)
  {
    new 
GuiShowImgCtrl("Shop_Item_" thiso.listofitems[i][0]) 
    {
      
20 20 32;
      
20;
      
width 32;
      
height 32;
    
      
this.imageselected thiso.listofitems[i][3];
      
this.imagenotselected thiso.listofitems[i][2];
      
this.image this.imagenotselected;
      
thiso.catchevent(this.name"onMouseDown""onItemSelected");
    }
    
Shop_Window.addControl("Shop_Item_" thiso.listofitems[i][0]);
  }
 
  
Shop_Window.show();
}

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.npcx 0.5this.npcy 0.5"BuyItem""Buy"this.itemselected.substring("Shop_Item_".length()));
  
Shop_Window.makefirstresponder(true);
}

function 
onItemSelected(selected
{
  for(
1Shop_Window.controls.size(); ++)
  {
    
temp.control Shop_Window.controls[i];
    
temp.control.image = (temp.control == selected temp.control.imageselected temp.control.imagenotselected);
  }

  
this.itemselected selected.name;


The shopcontrol weapon is basically just a gui interface which shows all the possible items to buy.

This is where the trouble starts. When I'm about to send the chosen item back to the npc something goes wrong. It seems like the triggeraction doesn't really do what I thought it would do. I thought it would trigger the npc closest to the coordinates given in the triggeraction (x, y, ...). But maybe I'm doing it all wrong?

Also, it seems as if the NPC's shape is out of synch. The player touches it when the player is positioned at the top left tile of the NPC. Maybe there's something I've done with setshape that doesn't work?

Thank you :)

EDIT: I managed to get the data back to the NPC! So there's no problem with the triggeraction(npcx, npcy, ...) command :) But the NPC PlayerTouchsMe is still troubeling us.

EDIT2: As stated above I managed to get the triggerserver and trigger client between the NPC and the weapon to work. I have now created a DB NPC named DB_Items:

PHP Code:

function onInitialized()
{
  
onCreated();
}

function 
onCreated()
{
  
/*
    Format:
    this.item_id = {Name, WeaponName, Image, ImageWhenSelected, Price};
   */

  
this.lisofitems = {{"Fireball""split_fireball""split_fireball.png""split_fireball_selected.png"30}, 
                     {
"Sword""split_sword""split_sword.png""split_sword_selected.png"20},
                     {
"Bow""split_bow""split_bow.png""split_bow_selected.png"10}
                    };


function 
getItems()
{
  return 
this.listofitems();


Is that a proper way of scripting the DB NPC? Or do I have to trigger the DB NPC and then trigger back to the level NPC in order to get the information the DB holds?

fowlplay4 08-09-2011 07:17 PM

Here's one way of doing it, you assign a shop id to your npc.

I.e: this.shop_id = "basicshop";

PHP Code:

function onCreated() {
  
this.shop_id "basicshop";
  
this.join("shopkeeper");


DB-NPC: DB_Shops (For this example)

PHP Code:

function onCreated() {
  
// Setup Basic Shop's Item List
  
this.shop_basicshop = {
    {
"Fireball""split_fireball""split_fireball.png""split_fireball_selected.png"30}, 
    {
"Sword""split_sword""split_sword.png""split_sword_selected.png"20},
    {
"Bow""split_bow""split_bow.png""split_bow_selected.png"10}
  };
}

// Public method to retrieve the items for a specific shop
public function getShopItems(shop_id) {
  return 
this.("shop_" shop_id);


then in your NPC:

PHP Code:

// other code..

function getShopItems() {
  return 
DB_Shops.getShopItems(this.shop_id);
}

function 
onPlayerTouchsMe() {
  
// You don't need to specify "gui", you can just use the weapon's name.
  
player.triggerclient("-shopcontrol"this.shop_idgetShopItems(), this.xthis.y);


Then in your script you can just store the shop id, and display the items, and then trigger your DB-NPC, verify the purchase against the list of items for that shop, and allow the player to purchase the item.

Adddeeee 08-09-2011 07:29 PM

Thank you :) I tried something similar just now, but failed because I didn't make the function getItems in the DB NPC public. I suppose it has to be public?

fowlplay4 08-09-2011 07:37 PM

Quote:

Originally Posted by Adddeeee (Post 1662700)
Thank you :) I tried something similar just now, but failed because I didn't make the function getItems in the DB NPC public. I suppose it has to be public?

To call it from other scripts like in my class example, yes.

Adddeeee 08-09-2011 08:27 PM

Thank you very much! Now I think I understand the DB NPC :)

Although, I still have problems with the PlayerTouchsMe. It seems as if the player touches the NPC when the player is located to the top left of the NPC.

This is the NPC script:

PHP Code:

join("shop");

//#CLIENTSIDE

function onCreated()
{
  
showcharacter();
  
nick "Candy"//Sets the nickname of the character
  
ap 100//Sets the ap of the character. Also affects the colour of the nickname
  
headimg "head722.gif"//What head image to use
  
bodyimg "body8.png";   //What body image to use
  
colors[0] = "orange";     //Sets the skin colour
  
colors[1] = "lightblue";     //Sets the coat colour
  
colors[2] = "yellow";     //Sets the sleeves colour
  
colors[3] = "darkred";     //Sets the shoes colour
  
colors[4] = "lightgreen";     //Sets the belt colour
  
shieldimg "no-shield.gif"//Sets the shield image
  
shieldpower 1//Must have this or the shield won't show



and this is the shop class:

PHP Code:

function onCreated()
{
   
setShape(13232);
   
this.shop_id "basicshop";
}

function 
onPlayerTouchsMe()
{
  
this.listofitems DB_Items.getShopItems(this.shop_id);
  
triggerclient("gui""-shopcontrol"this.listofitemsthis.xthis.y);         


Am I missing something that causes the PlayerTouchsMe to fire when I'm located to the top left of the NPC?

cbk1994 08-09-2011 08:30 PM

You're setting the shape to a 32px by 32px block, which doesn't perfectly fit the character. Try something like 38x48 (change the setShape). I can't remember exactly what it should be, so just change the values until it's working as you want it to, being sure to update the level in-between changes.

Adddeeee 08-09-2011 08:32 PM

Ah okay, I thought 32x32 pixels was the default character size :O

Isn't the "hit box" drawn from the upper left corner of the NPC to the lower right corner as a rectangle with the size specified in setShape?

fowlplay4 08-09-2011 08:53 PM

For normal NPCs 48 by 48 will do fine on server-side, and you're right on about the hit box.

The point of your shop class is to make it accept any shop id, so in your NPC you should be setting the shop id not in the class itself. I.e:

PHP Code:

function onCreated() {
  
this.shopid "basicshop";
  
this.join("shop");


In your class you can then do a check like this..

PHP Code:

// Undefined shops default to the basic shop
if (this.shop_id == NULLthis.shop_id "basicshop"


Adddeeee 08-09-2011 09:03 PM

Yepp :) I did that just to check if it worked. I'm going to setup different shop IDs and add them to the different NPCs before joining the class.

But if I set the setshape to 48, 48 wouldn't I touch the NPC even when I'm far away from it?

EDIT: NOticed that it worked just fine :) But what about the 32, 32 hit box?

Feels like I need an explanation why the hit box is 32,32 while the NPC should be set to 48,48? Is it because the NPCs image blocks the player?

fowlplay4 08-09-2011 09:20 PM

Quote:

Originally Posted by Adddeeee (Post 1662725)
Yepp :) I did that just to check if it worked. I'm going to setup different shop IDs and add them to the different NPCs before joining the class.

But if I set the setshape to 48, 48 wouldn't I touch the NPC even when I'm far away from it?

It's in pixels, so no. I don't think it will be touched until the "wall" of your player collides with it on the server-side.


All times are GMT +2. The time now is 07:32 PM.

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