guiismiti
Well-Known Member
- Joined
- May 19, 2014
- Messages
- 315
- Solutions
- 3
- Reaction score
- 68
Hi again,
This should be simple, but I've been stuck for a while now.
Like I said in another post a few days ago, I'm using depot 34 to send loots, for practicality - I'm too lazy to loot dead stuff every single time I kill it
Since I'm even more lazy than that, I created a talkaction "!loot" - if you stand in front of a loot depot, it will pick all stuff you can carry (limited by space or cap).
What it should do:
So, I have used the search function and I found this for scanning the depot
https://otland.net/threads/function-get-all-items-by-id-get-all-items-in-slot-by-id.171981/
And this for adding the items in backpacks
https://otland.net/threads/addcontaineritems-container-items.166104/
I have also taken a look at doNpcSellItem in npc.lua, but that's not what I'm using.
I adjusted the scan function to match my needs (code below).
Here is the code
Here are the problems I have so far (I'm stuck...)
Thanks in advance.
This should be simple, but I've been stuck for a while now.
Like I said in another post a few days ago, I'm using depot 34 to send loots, for practicality - I'm too lazy to loot dead stuff every single time I kill it

Since I'm even more lazy than that, I created a talkaction "!loot" - if you stand in front of a loot depot, it will pick all stuff you can carry (limited by space or cap).
What it should do:
- Scan the loot depot and create a table {{id = item id, count = item count}};
- Add the items in the table to the player, and remove it from depot as soon as they are created in the table.
So, I have used the search function and I found this for scanning the depot
https://otland.net/threads/function-get-all-items-by-id-get-all-items-in-slot-by-id.171981/
And this for adding the items in backpacks
https://otland.net/threads/addcontaineritems-container-items.166104/
I have also taken a look at doNpcSellItem in npc.lua, but that's not what I'm using.
I adjusted the scan function to match my needs (code below).
Here is the code
Code:
function onSay(player, words, param)
local lookPos = player:getPosition()
lookPos:getNextPosition(player:getDirection())
local depotItem = lookPos:getTile():getItemByType(ITEM_TYPE_DEPOT)
if depotItem ~= nil then
local depot = player:getDepotChest(34, true)
if not depot then
return true
end
local items = getDepotItems(depot)
local container = doCreateItemEx(1988, 1)
container = addContainerItems(getThing(container), items)
doPlayerAddItemEx(player, container, true)
for i = 1, #items do
print(items[i].id, " count ", items[i].count)
end
else
player:say('Stand in front of an opened loot depot.', TALKTYPE_MONSTER_SAY, false, player)
end
return true
end
function getDepotItems(container)
local containers = {} --a table for this function only, stores the containers inside the investigated container
local items = {} --a table that stores all the items, both from the investigated container and the containers inside it
local itcount = 1
local sitem = container
if sitem.uid > 0 then
if isContainer(sitem.uid) then
table.insert(containers, sitem.uid)
elseif not(id) or id == sitem.itemid then
if sitem.type > 0 then
itcount = sitem.type
else
itcount = 1
end
table.insert(items, {id = sitem.itemid, count = itcount})
end
end
while #containers > 0 do
for k = (getContainerSize(containers[1]) - 1), 0, -1 do
local tmp = getContainerItem(containers[1], k)
if isContainer(tmp.uid) then
table.insert(containers, tmp.uid)
elseif not(id) or id == tmp.itemid then
if tmp.type > 0 then
itcount = tmp.type
else
itcount = 1
end
table.insert(items, {id = tmp.itemid, count = itcount})
end
end
table.remove(containers, 1)
end
return items --returns all the items + count from the investigated container except for containers. Includes the items inside these containers
end
function addContainerItems(container, items)
local items_mod = {}
for _, it in ipairs(items) do
if(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 = (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
Here are the problems I have so far (I'm stuck...)
- There are functions that aren't being recognized (maybe because this is a talkaction script and the other scripts are not). removeItem(id, amount) and isItemStackable are not being recognized. I don't need isItemStackable anymore, I worked it around;
- I'm not sure removeItem is the right function to remove an item from the depot after it's been added to the player, so, the items are not being removed yet. I need to know which function to use.
- When using the talkaction, the player gets a backpack with 1x of all the items listed - the count of the stackable items is not being considered.
Thanks in advance.