• 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!

Problem with AddItem with Container Full

donabimbo

Member
Joined
May 1, 2023
Messages
27
Reaction score
9
GitHub
donabimbo
I use a TFS 1.5 from Nekiro.
Greetings, I am having a problem with the Additem when I have a full backpack.
I'm looking at what the problem is in the sources, and I can't find it.

I just use a simple script:
Lua:
player:addItem(2160, 1)
player:say("Item!")

Just that, no getFreeCapacity() or getEmptySlots().

I bring you an example images:
Recording 2023-04-30 at 21.05.54.gif

When you don't have space in your backpack, what you should do is leave the item at your feet, but it seems to do absolutely nothing.
 
player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])

canDropOnMap is false by default.
By omitting the check, the item has no-where to go, so it disappears as you see here.

The functionality you are wanting would be achieved by changing that param to true.
player:addItem(2160, 1, true)

The alternative way, would be to check if the item is added to the player correctly, but requires more code.

Lua:
local givenItem = player:addItem(2160, 1)
if not givenItem then
    print("Item not given successfully.")
end

At this point you could add additional checks to see if it's a capacity issue or a space issue.

Either way, I think the initial answer above gives you what you wanted, so I won't expand on the alternative option.
 
player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])

canDropOnMap is false by default.
By omitting the check, the item has no-where to go, so it disappears as you see here.

The functionality you are wanting would be achieved by changing that param to true.
player:addItem(2160, 1, true)

The alternative way, would be to check if the item is added to the player correctly, but requires more code.

Lua:
local givenItem = player:addItem(2160, 1)
if not givenItem then
    print("Item not given successfully.")
end

At this point you could add additional checks to see if it's a capacity issue or a space issue.

Either way, I think the initial answer above gives you what you wanted, so I won't expand on the alternative option.
It didn't work for me, it keeps doing the same thing, and the print doesn't send anything to the console either.
Check the canDropOnMap, in the sources to check if they were correct and they are by default, I don't know what this problem is due to.
 
It didn't work for me, it keeps doing the same thing, and the print doesn't send anything to the console either.
Check the canDropOnMap, in the sources to check if they were correct and they are by default, I don't know what this problem is due to.
Show your current script.
 
Show your current script.
Code:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)

    player:addItem(2160, 1, true)
    player:say("Item!")
    
    local givenItem = player:addItem(2160, 1)
    if not givenItem then
    print("Item not given successfully.")
    end
    
    return true
end
action:id(2345)
action:register()
@Xikini
I didn't understand this here:
Code:
player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])
was this an example?

Also, it's not something that bothers me so much since I can use the getEmptySlots function
But I have a problem, the problem is that if I don't have at least one space in my backpack, the items don't stack.

Example image:
Recording 2023-05-01 at 16.19.58.gif

Since they are coins, they can be stacked, but since they do not have a minimum space, they do not leave more.

Script:
Code:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)

    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)           
    if not backpack or backpack:getEmptySlots(false) < 1 then
        player:say("Your main backpack is full. You need to free up 1 available slots.")
        return true
    end
    
    player:addItem(2160, 1)
    player:say("Item!")
    
    return true
end

action:id(2345)
action:register()

I don't know which player would have a well-filled backpack, but it's just in case it happens XD.
 
@Xikini
I didn't understand this here:
Code:
player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])
was this an example?

Also, it's not something that bothers me so much since I can use the getEmptySlots function
But I have a problem, the problem is that if I don't have at least one space in my backpack, the items don't stack.

Example image:
View attachment 75274

Since they are coins, they can be stacked, but since they do not have a minimum space, they do not leave more.

Script:
Code:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)

    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)         
    if not backpack or backpack:getEmptySlots(false) < 1 then
        player:say("Your main backpack is full. You need to free up 1 available slots.")
        return true
    end
  
    player:addItem(2160, 1)
    player:say("Item!")
  
    return true
end

action:id(2345)
action:register()

I don't know which player would have a well-filled backpack, but it's just in case it happens XD.
This is all you should need

Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)
    -- player:addItem(2160, 1) -- won't drop on the floor
    player:addItem(2160, 1, true) -- will drop on the floor (if no empty space on the player)
    player:say("Item!")   
    return true
end

action:id(2345)
action:register()
 
This is all you should need

Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)
    -- player:addItem(2160, 1) -- won't drop on the floor
    player:addItem(2160, 1, true) -- will drop on the floor (if no empty space on the player)
    player:say("Item!")  
    return true
end

action:id(2345)
action:register()
Yes yes, I had put it I think it was not seen, here I send it again

Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)

    player:addItem(2160, 1, true)
    player:say("Item!")
    
    local givenItem = player:addItem(2160, 1)
    if not givenItem then
    print("Item not given successfully.")
    end
    
    return true
end
action:id(2345)
action:register()

In the same way, the same thing keeps happening, as if nothing happened with true.
That's why I commented out the getEmptySlots() alternative.
 
Yes yes, I had put it I think it was not seen, here I send it again

Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)

    player:addItem(2160, 1, true)
    player:say("Item!")
   
    local givenItem = player:addItem(2160, 1)
    if not givenItem then
    print("Item not given successfully.")
    end
   
    return true
end
action:id(2345)
action:register()

In the same way, the same thing keeps happening, as if nothing happened with true.
That's why I commented out the getEmptySlots() alternative.
I'm very confused.

I've seen all 3 of nekiro's downgrades and they have the same function as main tfs repo.
Are you sure you used the exact code I sent above?

I'll post it here as well.
Please try it exactly as posted and share a video if it's not working.
Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)
    player:addItem(2160, 1, true)
    player:say("item 2160, amount 1)   
    return true
end

action:id(2345)
action:register()
 
I'm very confused.

I've seen all 3 of nekiro's downgrades and they have the same function as main tfs repo.
Are you sure you used the exact code I sent above?

I'll post it here as well.
Please try it exactly as posted and share a video if it's not working.
Lua:
local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)
    player:addItem(2160, 1, true)
    player:say("item 2160, amount 1) 
    return true
end

action:id(2345)
action:register()



It doesn't work, it doesn't drop the item on the ground.

Recording 2023-05-02 at 17.24.54.gif
 
It doesn't work, it doesn't drop the item on the ground.

View attachment 75292
Alright, try this.

Lua:
local function customPlayerAddItem(playerId, itemId, amount, canDropOnGround)
    local player = Player(playerId)
    if not player then
        return "player does not exist"
    end
    local item = player:addItem(itemId, amount)
    if not item then
        if not canDropOnGround then
            Game.createItem(itemId, amount, player:getPosition())
        else
            if player:getFreeCapacity() < item:getWeight() then
                return "not enough capacity"
            else
                return "no empty space"
            end
        end
    end
    return true
end

local action = Action()

function action.onUse(player, item, itemEx, fromPosition, toPosition, isHotkey)
    local newItem = customPlayerAddItem(player:getId(), 2160, 1, true)
    -- local newItem = customPlayerAddItem(player:getId(), 2160, 1, false) -- use this if you want to ensure it goes into players inventory
    if newItem == true then
        player:say("item 2160, amount 1")
    else
        player:say(newItem)
    end
    return true
end

action:id(2345)
action:register()
 
Last edited:
I dunno what else to try. lmao
This is certainly looking like a source issue at this point.
Yes, yes, I used some sources totally without modifications, and it doesn't have that problem, it's my thing that maybe I put something wrong in the sources or something similar, in the same way, thanks for the help, I appreciate your time.

EDIT: It's not something worrisome but I want to know the solution, but it's okay, no problem.
 
@Xikini
Edit: After about 2 weeks
After a while I discovered the problem that the objects did not fall on your feet with the full capacity.

I had added the Reward System.
This line is the one that caused the problem:

Now:
Lua:
if (ret != RETURNVALUE_NOERROR && toCylinder->getItem() && toCylinder->getItem()->getID() != ITEM_REWARD_CONTAINER) {
        return ret;
}

Before:
Lua:
if (ret != RETURNVALUE_NOERROR) {
        return ret;
}

What would be in: Game.ccp
ReturnValue Game::internalAddItem(Cylinder* toCylinder, Item* item, int32_t index, uint32_t flags, bool test, uint32_t& remainderCount)

I don't know what I should put exactly :/
 
Last edited:
Back
Top