Author Topic: Buy system broke  (Read 652 times)

Code: [Select]
    $Buying_Price["Bowimage"] = 100;
    $Buying_ImgEqual["Bow"] = "Bowimage";

function serverCmdBuy(%client, %item)
{
    if(!$Buying_ImgEqual[%item])
    {
        messageClient(%client,'',"\c6Please enter a valid item name! Replace all spaces with \"_\"!");
        return false;
    }
   
    else if($Buying_ImgEqual[%item])
    {
        %player = %client.player;
        if(%client.gold < $Buying_Price[$Buying_ImgEqual[%item]])
        {
            messageClient(%client,'',"\c6You don't have enough gold!");
            return false;
        }
       
        if(%player.getDatablock().maxTools <= %player.weaponCount)
        {
            messageClient(%client,'',"\c6You don't have the space for that item!");
            return false;
        }
       
        else
        {
            messageClient(%client,'',"\c6You purchased a\c3"@%item@"\c6for\c3" SPC $Buying_Price[$Buying_ImgEqual[%item]] SPC "\c6!");
            for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
           {
              %tool = %player.tool[%i];
              if(%tool == 0)
              {
                 %player.tool[%i] = $Buying_ImgEqual[%item];
                 %player.weaponCount++;
                 messageClient(%client,'MsgItemPickup','',%i,%image);
                 break;
              }
           }
            return true;
        }
    }
}
When you type /Buy Bow, it says
Quote
Please enter a valid item name!

What the hell.

Get rid of shortcut method:
Change "$Buying_ImgEqual["Bow"] = "Bowimage";"
to
"$Buying_ImgEqual["bowimage"] = 1;"

and
add "%item = strlwr(%item);" to the start for good measure.

Try to keep shortcut
Or you could try if($Buying_ImgEqual[%item]$="")

I would really just suggest adding an extra array, one that uses the above(the above above) and another that stores the string, rather than combining the two.
« Last Edit: June 03, 2010, 06:48:49 PM by rkynick »

What the hell.

Get rid of shortcut method:
Change "$Buying_ImgEqual["Bow"] = "Bowimage";"
to
"$Buying_ImgEqual["bowimage"] = 1;"

and
add "%item = strlwr(%item);" to the start for good measure.

Try to keep shortcut
Or you could try if($Buying_ImgEqual[%item]$="")

I would really just suggest adding an extra array, one that uses the above(the above above) and another that stores the string, rather than combining the two.
Is there any tutorial on arrays and stuff like [%this]?
My friend wrote this code and I just tried to figure it out.

$Buying_ImgEqual["Bow"] would be the same thing as saying $Buying_ImgEqualBow
$Example[1,%number2] would be, if %number2 is 4, $Example1_4
The whole point of this is being able to use variables within a global variable, but only their value, not the reference to.

Try something like this:

Code: [Select]
$Buying_StoreItem["Bow"] = "BowImage/t100";

function serverCmdBuy(%client, %item)
{
    if(!$Buying_StoreItem[%item])
    {
        messageClient(%client,'',"\c6Please enter a valid item name! Replace all spaces with \"_\"!");
        return false;
    }
    %price = getField($Buying_StoreItem[%item], 1);

    %player = %client.player;
    if (%client.gold < %price)
      {
       messageClient(%client,'',"\c6You don't have enough gold!");
       return false;
      }
       
    if (%player.getDatablock().maxTools <= %player.weaponCount)
      {
       messageClient(%client,'',"\c6You don't have the space for that item!");
       return false;
      }

    %client.gold-=%price;
    messageClient(%client,'',"\c6You purchased a\c3"@%item@"\c6for\c3" SPC %price SPC "\c6!");
    for (%i = 0; %i < %player.getDatablock().maxTools; %i++)
      {
       %tool = %player.tool[%i];
       if(%tool == 0)
       {
        %player.tool[%i] = getField($Buying_StoreItem[%item], 0);
        %player.weaponCount++;
        messageClient(%client,'MsgItemPickup','',%i, %image);
        break;
       }
      }
    return true;
}

Note - I havent actually tested this, so there may be minor typos.  But its shorter (less else if else), and the data for each store item is in 1 global instead of several.


I typed /Buy Bow and it gave the error about not a valid item. I then changed
Code: [Select]
$Buying_StoreItem["Bow"] = "BowImage/t100";to
Code: [Select]
$Buying_StoreItem["Bow"] = 1;and it got farther. It says
You have purchased abowfor !

It doesn't add anything to the inventory but it increases a weapon slot.

That's because item datablocks go into the inventory, not image datablocks.

Edit: Also where are you pulling %image out of?