Page 1 of 1

let LuaInventory.insert accept tags

Posted: Sat May 06, 2017 9:13 pm
by gheift
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:
  • tags
  • custom_description
reason: since item-with-tags which have a different set of tags are handled as different "items", it is not possible to add a item-with-tags with tags to an inventory if it already contains a stack of item-with-tags without tags in a simple way. Currently the tags will be ignored and the stack with the item-with-tags without tags will be increased.

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, },
}
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.

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
Thanks,
Gerhard

Re: let LuaInventory.insert accept tags

Posted: Sun May 07, 2017 1:51 am
by Rseding91
Added for 0.15.10.

Re: let LuaInventory.insert accept tags

Posted: Sun May 07, 2017 10:00 am
by gheift
Thanks!