Since it might be useful and someone searched a function like this.
You can get all items by a certain id or get all items of a player by leaving "id" empty. => getAllItemsById(cid)
getAllItemsById(cid [, id])
-- returns an array of the items found like: ret = {{itemid = 2160, type = 1, etc}, {itemid = 2222, type = 1, etc}} -> storing the normal item arrays
-- if id is set in the parameter it will only returns those items with that id in the array
It will return an array with the items. Example:
getAllItemsBySlot(cid, slot [, id])
-- returns all items in a slot (return array same as above function)
-- especially useful to detect all backpack items
-- also id parameter is optional so you can only search a certain id in the backpack, for example get all his swords
Modifications:
The script will not return the backpacks/containers it is scanning, incase you also want the backpacks returned in the arrays, add the following:
After:
table.insert(containers, sitem.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, sitem) end
After:
table.insert(containers, tmp.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, tmp) end
You can get all items by a certain id or get all items of a player by leaving "id" empty. => getAllItemsById(cid)
getAllItemsById(cid [, id])
-- returns an array of the items found like: ret = {{itemid = 2160, type = 1, etc}, {itemid = 2222, type = 1, etc}} -> storing the normal item arrays
-- if id is set in the parameter it will only returns those items with that id in the array
Code:
function getAllItemsById(cid, id)
local containers = {}
local items = {}
for i = CONST_SLOT_FIRST, CONST_SLOT_LAST do
local sitem = getPlayerSlotItem(cid, i)
if sitem.uid > 0 then
if isContainer(sitem.uid) then
table.insert(containers, sitem.uid)
elseif not(id) or id == sitem.itemid then
table.insert(items, sitem)
end
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
table.insert(items, tmp)
end
end
table.remove(containers, 1)
end
return items
end
It will return an array with the items. Example:
Code:
for _, item in pairs(getAllItemsById(cid, 2222)) do
doPlayerSay(cid, "I got an item with id " .. item.itemid .. ".")
end
getAllItemsBySlot(cid, slot [, id])
-- returns all items in a slot (return array same as above function)
-- especially useful to detect all backpack items
-- also id parameter is optional so you can only search a certain id in the backpack, for example get all his swords
Code:
function getAllItemsBySlot(cid, slot, id)
local containers = {}
local items = {}
local sitem = getPlayerSlotItem(cid, i)
if sitem.uid > 0 then
if isContainer(sitem.uid) then
table.insert(containers, sitem.uid)
elseif not(id) or id == sitem.itemid then
table.insert(items, sitem)
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
table.insert(items, tmp)
end
end
table.remove(containers, 1)
end
return items
end
Modifications:
The script will not return the backpacks/containers it is scanning, incase you also want the backpacks returned in the arrays, add the following:
After:
table.insert(containers, sitem.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, sitem) end
After:
table.insert(containers, tmp.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, tmp) end
Last edited: