Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Arrow buying (https://forums.graalonline.com/forums/showthread.php?t=134261911)

Equinox 01-31-2011 09:07 PM

Arrow buying
 
I'm not very good at scripting so...
im trying to make a npc in a shop that takes gralats from you when you say Buy arrows. the script is like this:

if (player.chat=Buyarrows);
(playerdarts += 30);
(tokenize 10);
say2 here you go.;

It gives me the arrows but it does not deduct it from my gralats.
Help please?:confused:

MattKan 01-31-2011 09:18 PM

Quote:

Originally Posted by Equinox (Post 1626967)
I'm not very good at scripting so...
im trying to make a npc in a shop that takes gralats from you when you say Buy arrows. the script is like this:

if (player.chat=Buyarrows);
(playerdarts += 30);
(tokenize 10);
say2 here you go.;

It gives me the arrows but it does not deduct it from my gralats.
Help please?:confused:

This should work:

PHP Code:

function onPlayerChats()
{
 if (
player.chat == "Buyarrows")
  {
   
player.darts += 30;
   
player.rupees -= 30;
   
say2("Here you go!");
  }


However, that raises another issue. Players will be able to buy darts even if they have no gralats. So you have to add an if statement to check for that.

New Script:

PHP Code:

function onPlayerChats()
{
 if (
player.chat == "Buyarrows")
  {
   if (
player.rupees >= 30)
    {
     
player.darts += 30;
     
player.rupees -= 30;
     
say2("Here you go!");
    }
   else
    {
     
say2("You do not have enough gralats.");
    }
  }


That ought to stop players from buying arrows when they have no money.

Oh, and that is GS2, not GS1 which you used in your post. GS2 works better, so I suggest you learn that instead. There are some guides for GS2 scripting here: http://wiki.graal.net/index.php/Creation/Dev/GScript

Deas_Voice 01-31-2011 09:39 PM

if your going to do a "system" for buying items, you could use a gui window instead :)

there's too muh player commands :(

Equinox 01-31-2011 09:48 PM

ok thanks now it's working

Equinox 01-31-2011 10:14 PM

ok now its working

xAndrewx 01-31-2011 11:21 PM

hmm

HTML Code:

function onPlayerChats() {
  if (player.chat != "buy arrows") return;
 
  player.chat = this.onBuyArrows();
}
function onBuyArrows() {
  if (player.rupees < 30) return "You don't have enough money! You need 30 gralats";
 
  player.darts += 30;
  player.rupees -= 30;

  return "Yaaaay!! Thanks tuff guy";
}

quite funky

cbk1994 02-01-2011 12:44 AM

Quote:

Originally Posted by xAndrewx (Post 1626985)
hmm

You shouldn't name functions with an on prefix. That implies that it is an event.

Tigairius 02-01-2011 12:49 AM

Quote:

Originally Posted by cbk1994 (Post 1627002)
You shouldn't name functions with an on prefix. That implies that it is an event.

I agree here, usually events only start with "onMyFunction." If it's simply a method to purchase an item you should be using "function purchaseItem" instead of "function onPurchaseItem" unless you trigger the event from trigger() or a scheduleevent(). Naming a function with the prefix of "on" basically is saying you don't expect a return value.

Cloven 02-01-2011 03:56 AM

Quote:

Originally Posted by Tigairius (Post 1627004)
I agree here, usually events only start with "onMyFunction." If it's simply a method to purchase an item you should be using "function purchaseItem" instead of "function onPurchaseItem" unless you trigger the event from trigger() or a scheduleevent(). Naming a function with the prefix of "on" basically is saying you don't expect a return value.

I generally agree, though I don't think it's a big deal really...

Some people prefix their functions in that fashion for added readability. This is largely because many jerks create unintuitive names for functions, or their code is haphazardly styled. In those cases the 'Find' feature can be rendered almost useless. As such, scanning for the 'on' prefix can be of use in those cases.

For people who practice poor function and/or variable naming... !pissed! @ you.


:D

cbk1994 02-01-2011 12:42 PM

Quote:

Originally Posted by Cloven (Post 1627029)
I generally agree, though I don't think it's a big deal really...

Some people prefix their functions in that fashion for added readability. This is largely because many jerks create unintuitive names for functions, or their code is haphazardly styled. In those cases the 'Find' feature can be rendered almost useless. As such, scanning for the 'on' prefix can be of use in those cases.

How does "on" make a function more readable? :oo:

Fulg0reSama 02-01-2011 01:03 PM

Quote:

Originally Posted by cbk1994 (Post 1627122)
How does "on" make a function more readable? :oo:

I think he means if it looks the same as the rest people will recognize it as to what it is.
Also makes narrowing down for functions easier.

MrOmega 02-01-2011 03:50 PM

Quote:

Originally Posted by cbk1994 (Post 1627122)
How does "on" make a function more readable? :oo:

cause then you can capitalize the letter after! :P

onSomeFunction()

-or-

someFunction()

pointless, I know. xP

MattKan 02-04-2011 11:18 PM

1 Attachment(s)
I decided to help further by making you a GUI shopping system in which you can purchase bombs and arrows:

PHP Code:

function onActionServerSide(action) {
  switch (
action) {
    case 
"DecRup":
      
player.rupees -= params[1];
    break;
  }
}

//#CLIENTSIDE
function onCreated() {
 new 
GuiWindowCtrl("Bomb_Window") {
   
clientextent "300,200";
   
profile GuiBlueWindowProfile;
   
screenwidth/2-150;
   
screenheight/2-100;
   
visible true;

   
text "Get your bombs and arrows!";
   
canmove true;
   
canresize false;
   
canmaximize false;
 
   new 
GuiShowImgCtrl("Bomb_Img") {
     
55;
     
80;
     
width 32;
     
height 32;
     
this.image "bomb.png";
    }
   new 
GuiShowImgCtrl("Bomb_Img2") {
     
210;
     
80;
     
width 32;
     
height 32;
     
this.image "arrow.png";
    }
 
   new 
GuiMLTextCtrl("Bomb_Text") {
     
profile GuiBlueMLTextProfile;
     
30;
     
35;
     
width 430;     
     
height 50;
     
text "<font size=28><font color=white><b>Bombs:</b></font></font>";
    }
   new 
GuiMLTextCtrl("Bomb_Text2") {
     
profile GuiBlueMLTextProfile;
     
10;
     
130;
     
width 430;
     
height 50;
     
text "<font size=13.5><font color=white><b>10 Bombs for 10 Gralats.</b></font></font>";
    }
   new 
GuiMLTextCtrl("Bomb_Text3") {
     
profile GuiBlueMLTextProfile;
     
170;
     
35;
     
width 430;
     
height 50;
     
text "<font size=28><font color=white><b>Arrows:</b></font></font>";
    }
   new 
GuiMLTextCtrl("Bomb_Text4") {
     
profile GuiBlueMLTextProfile;
     
160;
     
130;
     
width 430;
     
height 50;
     
text "<font size=13.5><font color=white><b>20 Arrows for 10 Gralats.</b></font></font>";
    }
   new 
GuiButtonCtrl("Bomb_Button1") {
     
profile GuiBlueButtonProfile;
     
20;
     
155;
     
width 100;
     
height 30;
     
text "Purchase";
    }
   new 
GuiButtonCtrl("Bomb_Button2") {
     
profile GuiBlueButtonProfile;
     
170;
     
155;
     
width 100;
     
height 30;
     
text "Purchase";
    }
  }
}

function 
Bomb_Button1.onAction() {
  
//Button "Purchase"
 
if (player.rupees >= 9) {
   
triggerserver("gui",this.name,"DecRup",10);
   
player.bombs += 10;
  }else
    
player.chat "Insufficient Funds";
}

function 
Bomb_Button2.onAction() {
  
//Button "Purchase"
 
if (player.rupees >= 9) {
   
triggerserver("gui",this.name,"DecRup",10);
   
player.darts += 20;
  }else
    
player.chat "Insufficient Funds";


I've also included a .txt file.

Cubical 02-04-2011 11:21 PM

Should check to see how many rupees is decreased serverside instead of clientside.

Tigairius 02-04-2011 11:23 PM

Quote:

Originally Posted by Cubical (Post 1627939)
Should check to see how many rupees is decreased serverside instead of clientside.

I would check both clientside and serverside. That way if they aren't hacking, it won't send a needless trigger to the server.

cbk1994 02-04-2011 11:30 PM

Quote:

Originally Posted by MattKan (Post 1627937)
PHP Code:

function onActionServerSide(action) {
  switch (
action) {
    case 
"DecRup":
      
player.rupees -= params[1];
    break; 


This in itself ruins the script. Not only does this allow you to set your own price via Cheat Engine or the like, but you could even set the price to a negative value in order to spawn money.

Quote:

PHP Code:

function Bomb_Button1.onAction() {
  
//Button "Purchase"
 
if (player.rupees >= 9) {
   
triggerserver("gui",this.name,"DecRup",10);
   
player.bombs += 10;
  }else
    
player.chat "Insufficient Funds";



Always do checks like this on serverside. A player could change the amount of darts to gain, the number of rupees to pay, or even negate the trigger entirely. The proper way to do it would be to send a trigger with a parameter "item" (which is either "bomb" or "arrow"), and only add the bombs/arrows on serverside. This ensures that nothing can be tampered with. As a general rule, assume that any script you write on clientside can be changed by a hacker.

An example of how it should be done:
PHP Code:

function onActionServerSide(actionitem) { 
  if (
action == "purchase") {
    if (! (
item in {"bombs""darts"})) {
      return; 
// player sent a non-existing item
    
}
    
    if (
player.rupees 10) {
      return 
player.chat "You need at least 10 rupees!";
    }
    
    
player.rupees -= 10;
    
    if (
item == "bombs") {
      
player.bombs += 10;
    } else if (
item == "darts") {
      
player.darts += 20;
    }
  }


//#CLIENTSIDE 
// all of the GUI stuff here

function Bomb_Button1.onAction() { 
  
triggerServer("gui"this.name"purchase""darts");


function 
Bomb_Button2.onAction() { 
  
triggerServer("gui"this.name"purchase""bombs");


edit: Tig and Cubical beat me to it, but this should help as well

Cubical 02-05-2011 12:00 AM

Quote:

Originally Posted by Tigairius (Post 1627940)
I would check both clientside and serverside. That way if they aren't hacking, it won't send a needless trigger to the server.

I need to start doing that.

WhiteDragon 02-05-2011 12:30 AM

Quote:

Originally Posted by Tigairius (Post 1627940)
I would check both clientside and serverside. That way if they aren't hacking, it won't send a needless trigger to the server.

Unfortunately this results in code duplication, which makes going back and editing things annoying (twice the work, and you better not forget one of the checks). I usually only add in the extra clientside check if it's something that's going to be triggering extremely frequently. Of course this is a matter of preference but this is what I've found to be a nice balance.

The code duplication issue is sadly a hard problem to solve, even outside of Graal, and I haven't really seen a solution I've liked anywhere to port it, so I guess we'll just have deal with it for now in the critical areas.

Tigairius 02-05-2011 01:09 AM

Quote:

Originally Posted by WhiteDragon (Post 1627978)
Unfortunately this results in code duplication, which makes going back and editing things annoying (twice the work, and you better not forget one of the checks). I usually only add in the extra clientside check if it's something that's going to be triggering extremely frequently. Of course this is a matter of preference but this is what I've found to be a nice balance.

The code duplication issue is sadly a hard problem to solve, even outside of Graal, and I haven't really seen a solution I've liked anywhere to port it, so I guess we'll just have deal with it for now in the critical areas.

I usually use a this.attr[#] serverside to store the price which can be read clientside. Then if price is edited serverside, it applies clientside as well. Basically no code duplication involved?

fowlplay4 02-05-2011 01:14 AM

Quote:

Originally Posted by Tigairius (Post 1627991)
I usually use a this.attr[#] serverside to store the price which can be read clientside. Then if price is edited serverside, it applies clientside as well. Basically no code duplication involved?

You'll still have duplication (two of the same if statements) even though it's still only one extra line and not really much of a problem since you have the value synced on both.

WhiteDragon 02-05-2011 01:21 AM

Quote:

Originally Posted by Tigairius (Post 1627991)
I usually use a this.attr[#] serverside to store the price which can be read clientside. Then if price is edited serverside, it applies clientside as well. Basically no code duplication involved?

This only works for values. If your logic is complex enough to require a function, you will have to duplicate it since there is no way to pass a function as data (like you can with a value) from the client<->server (and it'd certainly be impossible to find a way to serialize a function with GS2).


All times are GMT +2. The time now is 11:28 PM.

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