Difference between revisions of "InvRef"

From Minetest Developer Wiki
Jump to navigation Jump to search
(Add UnofficialLua warning)
(point to more up to date places)
 
Line 1: Line 1:
 
{{UnofficialLua}}
 
{{UnofficialLua}}
Reference to an inventory.
+
Reference to an inventory. See the [https://minetest.gitlab.io/minetest/class-reference/#invref Lua API documentation] for more info.
 
 
== Methods ==
 
{| class="wikitable collapsible sortable"
 
!Method
 
!Description
 
|-
 
| <source enclose="none">is_empty("listname")</source>
 
| return true if list is empty
 
|-
 
| <source enclose="none">get_size("listname")</source>
 
| get size of a list
 
|-
 
| <source enclose="none">set_size("listname", size)</source>
 
| set size of a list, avoid e.g. <source enclose="none">, 7*3)</source>, use <source enclose="none">, 21) --7*3</source> instead to avoid confusion with [[formspec#list]]
 
|-
 
| <source enclose="none">get_width("listname")</source>
 
| get width of a list
 
|-
 
| <source enclose="none">set_width("listname", width)</source>
 
| set width of list; currently used for crafting
 
|-
 
| <source enclose="none">get_stack("listname", i)</source>
 
| get a copy of stack index i in list
 
|-
 
| <source enclose="none">set_stack("listname", i, stack)</source>
 
| copy stack to index i in list
 
|-
 
| <source enclose="none">get_list("listname")</source>
 
| return full list
 
|-
 
| <source enclose="none">set_list("listname", list)</source>
 
| set full list (size will not change)
 
|-
 
| <source enclose="none">get_lists()</source>
 
| return full lists. Ex: {main={ItemStack,ItemStack...}, craft={..},hand={..}...}
 
|-
 
| <source enclose="none">set_lists(lists)</source>
 
| set full lists. Ex: {main={ItemStack,ItemStack...}, craft={..},hand={..}...}
 
|-
 
| <source enclose="none">[InvRef]add_item("listname", stack)</source>
 
| add item somewhere in list, returns leftover ItemStack
 
|-
 
| <source enclose="none">room_for_item("listname", stack)</source>
 
| returns true if the stack of items can be fully added to the list
 
|-
 
| <source enclose="none">contains_item("listname", stack)</source>
 
| returns true if the stack of items can be fully taken from the list
 
|-
 
| <source enclose="none">remove_item("listname", stack)</source>
 
| take as many items as specified from the list, returns the items that were actually removed (as an ItemStack)
 
|}
 
  
 
== Caveats ==
 
== Caveats ==
* A common mistake is to get an [[ItemStack]] from an inventory, change it, and expect the changes to carry through to the inventory. '''ItemStacks returned by and passed to inventory methods are ''not'' live objects''', but are passed "by copy".  In other words, if you get an ItemStack result and change it, the inventory will ''not'' be modified.  You have to call one of the inventory manipulator methods (e.g. <code>inv:set_stack(listName, changedStack)</code>) for your changes to be reflected back in the inventory. This is also true of <source enclose="none">object:get_wielded_item()</source> and <source enclose="none">object:set_wielded_item(...)</source>, which just get and set the inventory stack the player has selected on the hotbar.
+
* A common mistake is to get an [[ItemStack]] from an inventory, change it, and expect the changes to carry through to the inventory. See [https://rubenwardy.com/minetest_modding_book/en/quality/common_mistakes.html#set-itemstacks-after-changing-them Set ItemStacks after changing them] in the modding book.
  
 
[[Category:Objects]]
 
[[Category:Objects]]

Latest revision as of 08:41, 25 October 2022

Mbox warning.png This page contains unofficial, low-quality Lua API documentation and is likely to be outdated or wrong. Do not rely on it!
For the official and up-to-date documentation, see Lua API Documentation.
Mbox warning.png This page has been proposed for deletion for the following reason: "Contains unofficial and potentially outdated, redundant and inconsistent Lua API information"
If you don't think that this page should be deleted, please explain why on the talk page.

Reference to an inventory. See the Lua API documentation for more info.

Caveats