• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Create item at location, not always player:addItem - problem with browse field

Fermantor

Active Member
Joined
Dec 16, 2009
Messages
209
Solutions
4
Reaction score
33
Location
Germany
tl;dr wanna create item where trigger item is located (not player:addItem). But laying on the ground floor with browse field open doesn't detect as parent:isTile(). How to fix?

Hello, I wanna add a feature that is more like RL tibia where when you create a new item, it isn't always is added to the player, but created where the script is triggered.

For example, you loot a shiny stone and use the pick on the stone in the corpse. Instead of adding the new item to the player, I want to add it to the monster, if there are slots free.

My first try of this function looks like this:
Lua:
function createItem(item, newitem, amount)
    local parent = item:getParent()
    local result
    item:remove(1)
    if parent:isTile() then -- check if item is laying on the ground
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) --create new item on the ground
    elseif parent:getEmptySlots() ~= 0 then -- item is in a container => check if there are slots free
        result = parent:addItem(newitem, amount and amount or 1) -- add item to container
    elseif parent:getTopParent():isPlayer() then -- no slots are free => check if container is from a player
        result = parent:getTopParent():addItem(newitem, amount and amount or 1) -- add Player item
    else -- container is full and owner not a player
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) -- create item on the ground
    end
    return result
end

It works perfectly fine and detects if the item is laying on the ground or is inside a corpse/container, until I browse field the item on the ground. Then it won't detect the tile again, and thinks it is inside a container with 30 slots. How can I debug this?
 
Solution
tl;dr wanna create item where trigger item is located (not player:addItem). But laying on the ground floor with browse field open doesn't detect as parent:isTile(). How to fix?

Hello, I wanna add a feature that is more like RL tibia where when you create a new item, it isn't always is added to the player, but created where the script is triggered.

For example, you loot a shiny stone and use the pick on the stone in the corpse. Instead of adding the new item to the player, I want to add it to the monster, if there are slots free.

My first try of this function looks like this:
Lua:
function createItem(item, newitem, amount)
    local parent = item:getParent()
    local result
    item:remove(1)
    if parent:isTile() then --...
tl;dr wanna create item where trigger item is located (not player:addItem). But laying on the ground floor with browse field open doesn't detect as parent:isTile(). How to fix?

Hello, I wanna add a feature that is more like RL tibia where when you create a new item, it isn't always is added to the player, but created where the script is triggered.

For example, you loot a shiny stone and use the pick on the stone in the corpse. Instead of adding the new item to the player, I want to add it to the monster, if there are slots free.

My first try of this function looks like this:
Lua:
function createItem(item, newitem, amount)
    local parent = item:getParent()
    local result
    item:remove(1)
    if parent:isTile() then -- check if item is laying on the ground
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) --create new item on the ground
    elseif parent:getEmptySlots() ~= 0 then -- item is in a container => check if there are slots free
        result = parent:addItem(newitem, amount and amount or 1) -- add item to container
    elseif parent:getTopParent():isPlayer() then -- no slots are free => check if container is from a player
        result = parent:getTopParent():addItem(newitem, amount and amount or 1) -- add Player item
    else -- container is full and owner not a player
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) -- create item on the ground
    end
    return result
end

It works perfectly fine and detects if the item is laying on the ground or is inside a corpse/container, until I browse field the item on the ground. Then it won't detect the tile again, and thinks it is inside a container with 30 slots. How can I debug this?
Here's my edited function of yours.
my green text is ugly. 🤷‍♂️

Lua:
function createItem(item, newitemId, amount)
    local parent = item:getParent()
    local topParent = item:getTopParent()
    local result
    item:remove(1)
    
    -- if item is a container and container has an empty slot
    -- // incomplete function -> doesn't check item quantity or if result could stack with other items in the container.
    -- //                     -> simple function instead of complex because it's not worth the effort / resources to check all scenario's
    if not parent:isTile() and ItemType(parent:getId()):isContainer() and parent:getEmptySlots() ~= 0 then
        result = parent:addItem(newitemId, amount and amount or 1) -- create item in container
        
    -- if item is on a player
    -- // this check is second, because we want the item to be created in the initial container if possible.
    elseif Player(topParent) then
        result = topParent:addItem(newitemId, amount and amount or 1, true) -- create item on player or ground under player (true = canDropOnGround)
        
    -- if parent is not a player or a container with no slots or item is on the ground
    -- // basically a failsafe to make sure the item get's created
    else
        result = Game.createItem(newitemId, amount and amount or 1, parent:getPosition()) -- create item on the ground
    end
    
    return result
end
 
Solution
tl;dr wanna create item where trigger item is located (not player:addItem). But laying on the ground floor with browse field open doesn't detect as parent:isTile(). How to fix?

Hello, I wanna add a feature that is more like RL tibia where when you create a new item, it isn't always is added to the player, but created where the script is triggered.

For example, you loot a shiny stone and use the pick on the stone in the corpse. Instead of adding the new item to the player, I want to add it to the monster, if there are slots free.

My first try of this function looks like this:
Lua:
function createItem(item, newitem, amount)
    local parent = item:getParent()
    local result
    item:remove(1)
    if parent:isTile() then -- check if item is laying on the ground
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) --create new item on the ground
    elseif parent:getEmptySlots() ~= 0 then -- item is in a container => check if there are slots free
        result = parent:addItem(newitem, amount and amount or 1) -- add item to container
    elseif parent:getTopParent():isPlayer() then -- no slots are free => check if container is from a player
        result = parent:getTopParent():addItem(newitem, amount and amount or 1) -- add Player item
    else -- container is full and owner not a player
        result = Game.createItem(newitem, amount and amount or 1, parent:getPosition()) -- create item on the ground
    end
    return result
end

It works perfectly fine and detects if the item is laying on the ground or is inside a corpse/container, until I browse field the item on the ground. Then it won't detect the tile again, and thinks it is inside a container with 30 slots. How can I debug this?
Here's my edited function of yours.
my green text is ugly. 🤷‍♂️

Lua:
function createItem(item, newitemId, amount)
    local parent = item:getParent()
    local topParent = item:getTopParent()
    local result
    item:remove(1)
   
    -- if item is a container and container has an empty slot
    -- // incomplete function -> doesn't check item quantity or if result could stack with other items in the container.
    -- //                     -> simple function instead of complex because it's not worth the effort / resources to check all scenario's
    if not parent:isTile() and ItemType(parent:getId()):isContainer() and parent:getEmptySlots() ~= 0 then
        result = parent:addItem(newitemId, amount and amount or 1) -- create item in container
       
    -- if item is on a player
    -- // this check is second, because we want the item to be created in the initial container if possible.
    elseif Player(topParent) then
        result = topParent:addItem(newitemId, amount and amount or 1, true) -- create item on player or ground under player (true = canDropOnGround)
       
    -- if parent is not a player or a container with no slots or item is on the ground
    -- // basically a failsafe to make sure the item get's created
    else
        result = Game.createItem(newitemId, amount and amount or 1, parent:getPosition()) -- create item on the ground
    end
   
    return result
end
Did my code resolve your issue?
 
Yo, thanks for your effort. I haven't tried it yet because I found another script in the files that seemed to work fine so far.
Anyway I am very grateful for your answer.
 
Back
Top