let LuaInventory.insert accept tags
Posted: Sat May 06, 2017 9:13 pm
since implementing the first additional parameters worked (viewtopic.php?f=65&t=36971) I would like to see LuaInventory.insert to accept the following parameter of SimpleItemStack:
An example:
I know this is already implemented. The first behavior happens with the SimpleItemStack, the second one is the one I want and allready happens, if a LuaItemStack is used.
A current workaround for this problem is a function like this, which I think is a little bit much work for a function, which is already implemented in C++ in a more optimized way.
Thanks,
Gerhard
- tags
- custom_description
An example:
Code: Select all
local inventory = game.player.get_inventory(1)
-- this is our initial condition
inventory.insert{name = "item-with-tags", count = 17}
-- we want to add a new item-with-tags
local new_item = {
name = "item-with-tags",
tags = {foo = 1, },
custom_description = {"item-description.foobar", 1, },
count = 8,
}
inventory.insert(new_item)
-- with the current implementation you get:
inventory.content == {
{name = "item-with-tags", count = 25, },
}
-- what I want is:
inventory.content == {
{name = "item-with-tags", count = 17, },
{name = "item-with-tags", tags = {foo = 1, }, count = 8, },
}
A current workaround for this problem is a function like this, which I think is a little bit much work for a function, which is already implemented in C++ in a more optimized way.
Code: Select all
local function inventory_add_item_with_tags(inventory, new_item)
local function stack_matches(stack, item) --[[ compares tags, ammo, health etc. ]] end
local inital_count = new_item.count -- or 1
local finished = false
for i = 1, #inventory do
local stack = inventory[i]
if stack_matches(stack, new_item) then
local before = stack.count
stack.count = before + new_item.count
local added = stack.count - before
new_item.count = new_item.count - added
if new_item.count == 0 then
finished = true
break
end
end
end
if not finished then
-- search for empty stacks
local stack = inventory[i]
if not stack.valid_for_read then
stack.set_stack(new_item)
stack.tags = new_item.tags
stack.custom_description = new_item.custom_desciption
new_item.count = new_item.count - stack.count
if new_item.count == 0 then
break
end
end
end
local inserted = inital_count - new_item.count
new_item.count = initial_count -- revert to original state
return inserted
end
Gerhard