New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InvRef: Add convenience method add_list for bulk transfer of items #8466
Conversation
I think, this would be more useful if it worked between different inventories, too. |
5508653
to
3842ae7
Compare
Fixed src list being destroyed after the bulk transfer. Apparently, I was trying to push a non-existent list to the Lua stack, because I was using
I agree. I'll see what I can do. |
3842ae7
to
5872c9c
Compare
790262e
to
a270f68
Compare
52c3967
to
147a699
Compare
InventoryList *src_list = inv->getList(src_listname); | ||
|
||
// Create new list to store leftovers | ||
InventoryList *leftover_list = new InventoryList("leftover", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need it as pointer?
InventoryList leftover_list("leftover", src_list->getSize(), getServer(L)->getItemDefManager());
EDIT: and push_inventory_list_raw(L, &leftover_list);
const char *src_listname = luaL_checkstring(L, 3); | ||
|
||
InventoryList *dst_list = inv->getList(dst_listname); | ||
InventoryList *src_list = inv->getList(src_listname); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getList
may return nullptr
is that list does not exist. Check for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
So they're moved? The code does indicate that the list is copied,. local src = inv:get_list("main")
for i = 1, #src do
src[i] = dst:add_item("craft", src[i])
end
inv:set_list("main", src) |
@SmallJoker Correct - they're moved. After all, this is a convenience method to achieve exactly what the Lua code you posted does - bulk transfer of items from one list to another. The name might cause some confusion, though. |
@ClobberXD |
I too am starting to doubt the necessity of this convenience function, given the implementation effort. But that got me thinking, would it make sense to implement this in Lua and add it to builtin? |
Closing due to lack of solid use-cases and lack of core dev interest. |
InvRef:add_list(dst_listname, src_listname)
for bulk transfer of items from one inventory list to another.push_inventory_list_raw
(declared/defined inc_content.{h|cpp}
) to directly push anInventoryList *
object to the Lua stack. Existing functionpush_inventory_list
now makes use ofpush_inventory_list_raw
internally.To test:
luacmd
mod.The above chat-command transfers all items in
main
to the crafting grid and sets the leftover back tomain
. Since there were more than 9 unique items inmain
, there's bound to be at least one item as leftover.Closes #7663. Tested, works as expected.