Author Topic: Item Not Adding [Solved]  (Read 1261 times)

Code: [Select]
if(%obj.getClassName() $= "Item")
{
for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
{
if(!%player.tool[%i] && (!%freeSlot))
{
%freeSlot = %i;
}

if(%player.tool[%i] == %obj.dataBlock.getID())
{
%freeSlot = -1;

break;
}
}

if(%freeSlot)
{
%player.tool[%i] = %obj.dataBlock.getID();
%player.weaponCount++;
messageClient(%player.client, 'MsgItemPickup', '' , %i, %obj.dataBlock.getID());
}

%obj.delete();

return;
}
I cannot figure out why the item won't add. Help anyone?
« Last Edit: October 16, 2012, 02:16:56 PM by jes00 »

remove the "break;" from
Code: [Select]
if(%player.tool[%i] == %obj.dataBlock.getID())
{
%freeSlot = -1;

break;
}
and put it in
Code: [Select]
if(!%player.tool[%i] && (!%freeSlot))
{
%freeSlot = %i;
}
You are exiting the loop when you find a non-empty slot. you should only exit when you find an empty slot, or you hit the maxTools variable.

remove the "break;" from
Code: [Select]
if(%player.tool[%i] == %obj.dataBlock.getID())
{
%freeSlot = -1;

break;
}
and put it in
Code: [Select]
if(!%player.tool[%i] && (!%freeSlot))
{
%freeSlot = %i;
}
You are exiting the loop when you find a non-empty slot. you should only exit when you find an empty slot, or you hit the maxTools variable.
Than how am I supposed to stop it from adding the item if you already have a duplicate?

OHHH i see now why you did that. But still, if i remember correctly "if(-1)" returns true.
anyways, your problem was the
Code: [Select]
%player.tool[%i] = %obj.dataBlock.getID();You used tool[%i] not tool[%freeslot]. Why it doesnt add is because %i is equal to maxTools so technically, it is the slot right after the last inventory slot.

so this would be the correct code. changed a little bit for efficency.
Code: [Select]
if(%obj.getClassName() $= "Item")
{
for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
{
if(!%player.tool[%i])
{
%freeSlot = %i;
}
if(%player.tool[%i] == %obj.dataBlock.getID())
{
//Already has item, abort abort
return;
}
}
if(%freeSlot)
{
%player.tool[%freeSlot] = %obj.dataBlock.getID();
%player.weaponCount++;
messageClient(%player.client, 'MsgItemPickup', '' , %i, %obj.dataBlock.getID());
}

%obj.delete();

return;
}

OHHH i see now why you did that.
anyways, your problem was the
Code: [Select]
%player.tool[%i] = %obj.dataBlock.getID();You used tool[%i] not tool[%freeslot]. Why it doesnt add is because %i is equal to maxTools so technically, it is the slot right after the last inventory slot.

so this would be the correct code. changed a little bit for efficency.
Code: [Select]
if(%obj.getClassName() $= "Item")
{
for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
{
if(!%player.tool[%i])
{
%freeSlot = %i;
}
if(%player.tool[%i] == %obj.dataBlock.getID())
{
//Already has item, abort abort
return;
}
}
if(%freeSlot)
{
%player.tool[%freeSlot] = %obj.dataBlock.getID();
%player.weaponCount++;
messageClient(%player.client, 'MsgItemPickup', '' , %i, %obj.dataBlock.getID());
}

%obj.delete();

return;
}
Thank you.

But still, if i remember correctly "if(-1)" returns true.
You don't remember correctly.

EDIT: Once I changed messageClient(%player.client, 'MsgItemPickup', '' , %i, %obj.dataBlock.getID()); to messageClient(%player.client, 'MsgItemPickup', '' , %freeSlot, %obj.dataBlock.getID()); it worked.
« Last Edit: October 16, 2012, 02:15:10 PM by jes00 »