• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

addContainerItems(container, items)

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Reaction score
186
How it works?

it adds table containing items to a backpack if backpack slots finished it add a new backpack in the main backpack and continue adding items.




Parameters?

container: argument of type thing , so if you have uid of bag you can get it in from of thing using getThing(uid)

items: table consist of table items in the form of {id = , count = }




Returns ?

A uid of the main backpack.




Function

Lua:
function addContainerItems(container,items)
    local items_mod = {}
    for _, it in ipairs(items) do
        if( isItemStackable(it.id) and it.count > 100) then
            local c = it.count
            while( c > 100 ) do
                table.insert(items_mod,{id = it.id,count = 100})
                c = c - 100
            end
            if(c > 0) then
                table.insert(items_mod,{id = it.id,count = c})
            end
        else
            table.insert(items_mod,{id = it.id,count = 1})
        end
    
    end
    local free = getContainerCap(container.uid) - (getContainerSize(container.uid) )
    local count = math.ceil(#items_mod/ free)
    local main_bp = container.uid
    local insert_bp = main_bp
    local counter = 1
    for c,it in ipairs(items_mod) do
        local _c = isItemStackable(it.id) and (it.count > 100 and 100 or it.count) or 1
        if count > 1 then
            if (counter < free) then
                doAddContainerItem(insert_bp, it.id, _c)
            else
                insert_bp = doAddContainerItem(insert_bp, container.itemid, 1)
                count = (#items_mod)-(free-1)
                free = getContainerCap(insert_bp) 
                count = math.ceil(count/ free)
                doAddContainerItem(insert_bp, it.id, _c)
                counter = 1
            end
            counter = counter + 1
        else
                doAddContainerItem(insert_bp, it.id, _c)
        end
    end
    return main_bp
end




Example:

1)lets say you want to add 4600 crystal coins to player and give it to him in a bag

Lua:
function onSay(cid, words, param, channel)
    local items = {{id = 2160, count = 4600}}
    local container = doCreateItemEx(1987, 1)
    container = addContainerItems(getThing(container),items)
    doPlayerAddItemEx(cid, container, true)
        
    
    return true
end

2) lets say you want to add 4600 crystal coins to player main bp in the inventory slot
Lua:
function onSay(cid, words, param, channel)
    local items = {{id = 2160, count = 4600}}


    addContainerItems(getPlayerSlotItem(cid,CONST_SLOT_BACKPACK),items)
    
    return true
end


Note : the item count only works on stackable items, you can easily edit the first for loop to make it works on non-stackable items too.

Hope it is useful!
 
Useful Script and nice!
 
I looks nice, but this have been released somewhere else before, I think.
Btw, the last example (2), it's the same as "doPlayerAddItem" hah xd
 
I looks nice, but this have been released somewhere else before, I think.
Btw, the last example (2), it's the same as "doPlayerAddItem" hah xd

No it is not !! I told you if the main backpack slots finished it add a new backpack in the main backpack then continue adding in the newly created bp ....
 
Back
Top