Author Topic: Variable wont subtract  (Read 1921 times)

Hey, I'm making a crafting system but whenever I do the function it wont subtract the varaible. Anyone see why?

Code: [Select]
        function player::addnewitem(%this,%item)
        {
                if(!isObject(%this))
                        return;
                if(!isObject(%item))
                        return;
                for(%i = 0; %i < %this.getDatablock().maxTools; %i++)
                {
                        %tool = %this.tool[%i];
                        if(%tool == 0)
                        {
                                %this.tool[%i] = %item.getId();
                                %this.weaponCount++;
                                messageClient(%this.client,'MsgItemPickup','',%i,%item.getId());
                                break;
                        }
                }
        }




datablock fxDTSBrickData(CreeRPGCraftBrickData : brick2x4FData)
{
category = "CRPG";
subCategory = "Bricks";

uiName = "Crafting Brick";
};

datablock TriggerData(CreeRPGCraft)
{
   tickPeriodMS = 100;
};

function fxDtsBrick::createCreeRPGCraftTrigger(%this,%data)
{

%t = new Trigger()
{
datablock = %data;
polyhedron = "0 0 0 1 0 0 0 -1 0 0 0 1"; //this determines the shape of the trigger
};
missionCleanup.add(%t);

%boxMax = getWords(%this.getWorldBox(), 3, 5);
%boxMin = getWords(%this.getWorldBox(), 0, 2);
%boxDiff = vectorSub(%boxMax,%boxMin);
%boxDiff = vectorAdd(%boxDiff,"0 0 0.2");
%t.setScale(%boxDiff);
%posA = %this.getWorldBoxCenter();
%posB = %t.getWorldBoxCenter();
%posDiff = vectorSub(%posA, %posB);
%posDiff = vectorAdd(%posDiff, "0 0 0.1");
%t.setTransform(%posDiff);

%this.trigger = %t;
%t.brick = %this;

return %t;
}


function CreeRPGCraftBrickData::onAdd(%this,%brick)
{
     %brick.scheduleNoQuota(0,"createCreeRPGCraftTrigger","CreeRPGCraft");
}

function CreeRPGCraftBrickData::onRemove(%this,%brick)
{
     if(isObject(%brick.trigger))
          %brick.trigger.delete();
}

function CreeRPGCraft::onEnterTrigger(%this,%trigger,%obj,%client)
{
commandToClient(%obj.client, 'OpenCraft');
%client.isInCCraft = 1;
parent::onEnterTrigger(%this, %trigger, %obj, %client);
}

function CreeRPGCraft::onLeaveTrigger(%this,%trigger,%obj,%client)
{
commandToClient(%obj.client, 'CloseCraft');
%client.isInCCraft = 0;
parent::onEnterTrigger(%this, %trigger, %obj, %client);
}

function CreeRPGCraft::onTickTrigger(%this,%trigger,%obj)
{
Parent::onTickTrigger(%this,%trigger);
}










function serverCmdCraftCopperBlade(%client)
{
if(%client.copperBars >= 3)
if(%client.isInCCraft == 1)
%client.copperBars -= 3;
%client.player.addNewItem(CopperSSwordItem);
}

function serverCmdCraftIronBlade(%client)
{
if(%client.isInCCraft == 1)
if(%client.copperBars >= 1)
if(isObject(%player = %client.player))
%client.copperBars -= 3;
%player.addNewItem(CopperSSwordItem);
}

function serverCmdCraftBronzeBlade(%client)
{
if(%client.isInCCraft == 1)
if(%client.copperBars >= 1)
if(isObject(%player = %client.player))
%client.copperBars -= 3;
%player.addNewItem(CopperSSwordItem);
}

function serverCmdCraftSteelBlade(%client)
{
if(%client.isInCCraft == 1)
if(%client.copperBars >= 1)
if(isObject(%player = %client.player))
%client.copperBars -= 3;
%player.addNewItem(CopperSSwordItem);
}

function serverCmdCraftHalaciteBlade(%client)
{
if(%client.isInCCraft == 1)
if(%client.copperBars >= 1)
if(isObject(%player = %client.player))
%client.copperBars -= 3;
%player.addNewItem(CopperSSwordItem);
}

Sorry for my sloppy code. Im a little new :/

Instead of making a new server command for each thing to craft, just make one general command with some variables.

I can't because a client is using them.


commandToServer('CraftCopperBlade');

commandToServer('CraftCopperBlade');
commandToServer('craft', "CopperBlade");


What should it look like then?

It's hard to write code for you when we have no idea how your inventory system is set up
You'd want to do what jes said. Your function would look something like this:

function serverCmdCraft(%client,item)
{
   //Check for required mats for %item, and deduct them
   %player.addNewItem(%item);
}


Also, realistically speaking, you should master the basics before you try something as complex as a gamemode
« Last Edit: October 28, 2013, 03:13:45 PM by Headcrab Zombie »

Your nested if statements do not stack like you think they do.  Use brackets.
When you write:

if(a)
if(b)
if(c)
do_this();
and_this();

It is interpreted by the compiler as:

if(a)
{
    if(b)
    {
        if(c)
        {
            do_this();
        }
    }
}
and_this();

function serverCmdCraft(%cl,%item1,%item2,%item3,%item4)
{
   %item = %item1 SPC %item2 SPC %item3 SPC %item;
   %item = trim(%item);
   if(%item $= "Item Thing")
   {
      //stuff here
   }
   else if(%item $= "This Item")
   {
      //stuff here
   }
   else
      messageClient(%cl,'',"\c6Unable to find the item you were looking for to craft.");
}


Advanced bot, make sure to store the trim'd string to a variable..
Thanks. Fixed.
« Last Edit: October 28, 2013, 09:48:06 PM by Advanced Bot »

Advanced bot, make sure to store the trim'd string to a variable..


A switch should generally be used instead of huge if...else sequences.
Actually, since everything is the same, no matter what item is crafted(just adding item to inv and removing materials) you could skip the switch, store the crafting information is an array or a data structure of some sort, and pull it from there, something like:

%player.addItem(%item)
for(%i=0;%i<Crafting.getMatCount(%item);%i++)
    %client.mat[Crafting.getMatName(%item,%i)] -= Crafting.getMatAmount(%item,%i);

The crafting method calls replaced with whatever data structure or array you'd use
« Last Edit: October 28, 2013, 11:31:31 PM by Headcrab Zombie »

Fixed it. Just messed around how it is setup and got it to work.

I have one more question. How do you use the GuiHealthBarHud? What are the variables?