Anonymous

In-game Functions: Difference between revisions

From Melvor Idle
→‎Remove Item from Bank: Document solution for removing items with invalid quantities
(→‎Attributes: Provide further information for 'found' parameter of addItemByID function, and add warning for bank slot tokens)
(→‎Remove Item from Bank: Document solution for removing items with invalid quantities)
 
Line 37: Line 37:
The removeItemQuantityByID function can be used to remove any item from the bank
The removeItemQuantityByID function can be used to remove any item from the bank
  <syntaxhighlight lang="js">game.bank.removeItemQuantityByID(itemID, quantity, removeItemCharges)</syntaxhighlight>
  <syntaxhighlight lang="js">game.bank.removeItemQuantityByID(itemID, quantity, removeItemCharges)</syntaxhighlight>
Note that if an item's quantity is in an invalid state, such as <syntaxhighlight lang="js" inline>NaN</syntaxhighlight> or <syntaxhighlight lang="js" inline>Infinity</syntaxhighlight>, then this function will not be able to remove that item from the bank. For any such items, use the below snippet instead:
{{SpoilerBox
|color=default
|title=Code
|text=First, enter the below into the console:
<syntaxhighlight lang="js">function removeItemByID(itemID) {
const item = game.items.getObjectByID(itemID);
if (item === undefined)
throw new Error(
`Error removing item from bank by id. Item with id: ${ itemID } is not registered.`
);
const bankItem = game.bank.items.get(item);
if (bankItem === undefined)
throw new Error(
`Tried to remove quantity from bank, but item is not in bank.`
);
bankItem.quantity = 1;
game.bank.removeItemQuantity(item, 1, true);
}</syntaxhighlight>
After this, invoke the newly-created function with the appropriate item ID to remove items from the bank. For example: <syntaxhighlight lang="js" inline>removeItemByID('melvorD:Oak_Logs');</syntaxhighlight>
}}


=== Attributes ===
=== Attributes ===
Line 51: Line 73:


=== Examples ===
=== Examples ===
  <syntaxhighlight lang="js">game.bank.removeItemQuantityByID("melvorD:Oak_Logs", 10);</syntaxhighlight>
  <syntaxhighlight lang="js">game.bank.removeItemQuantityByID('melvorD:Oak_Logs', 10);</syntaxhighlight>
The above code will result in 10 {{ItemIcon|Oak Logs}} being removed from the bank.
The above code will result in 10 {{ItemIcon|Oak Logs}} being removed from the bank.