Difference between revisions of "InvRef"

From Minetest Developer Wiki
Jump to navigation Jump to search
(Caveat about "non-live" ItemStacks)
(→‎Methods: use a wikitable)
Line 2: Line 2:
  
 
== Methods ==
 
== Methods ==
* <source enclose="none">is_empty("listname")</source>: return true if list is empty
+
{| class="wikitable collapsible sortable"
* <source enclose="none">get_size("listname")</source>: get size of a list
+
!Method
* <source enclose="none">set_size("listname", size)</source>: set size of a list
+
!Description
* <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">is_empty("listname")</source>
* <source enclose="none">get_stack("listname", i)</source>: get a copy of stack index i in list
+
| return true if list is empty
* <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">get_size("listname")</source>
* <source enclose="none">set_list("listname", list)</source>: set full list (size will not change)
+
| get size of a list
* <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">set_size("listname", size)</source>
* <source enclose="none">contains_item("listname", stack)</source>: returns true if the stack of items can be fully taken from the list
+
| set size of a 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)
+
|-
 +
| <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">[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 ==

Revision as of 20:21, 2 May 2014

Reference to an inventory.

Methods

Method Description
is_empty("listname") return true if list is empty
get_size("listname") get size of a list
set_size("listname", size) set size of a list
get_width("listname") get width of a list
set_width("listname", width) set width of list; currently used for crafting
get_stack("listname", i) get a copy of stack index i in list
set_stack("listname", i, stack) copy stack to index i in list
get_list("listname") return full list
set_list("listname", list) set full list (size will not change)
[InvRef]add_item("listname", stack) add item somewhere in list, returns leftover ItemStack
room_for_item("listname", stack) returns true if the stack of items can be fully added to the list
contains_item("listname", stack) returns true if the stack of items can be fully taken from the list
remove_item("listname", stack) take as many items as specified from the list, returns the items that were actually removed (as an ItemStack)

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. inv:set_stack(listName, changedStack)) for your changes to be reflected back in the inventory. This is also true of object:get_wielded_item() and object:set_wielded_item(...), which just get and set the inventory stack the player has selected on the hotbar.