Hi, recently I discovered my old function wich I was excited about when I was newbie in scripting. It was parsing tree of backpacks that locates items by specific actionid you give to function. I was so annoyed by unprofesionality of this code that I rewrote this and release it now to public. Maybe someone will find it usefull in some kind of quests or when you make events perhaps.
Here is the code.
As you can see it comes to 2 functions getItemsByActionId(cid,action) and getItemsByItemidAndActionId(cid,itemid, action).
They both returns array with uids of fiting items.
But lets give some example.
Have fun with this simple code, I hope you can find it usefull somehow.
Cheers.
Here is the code.
Lua:
Locate = {
}
function Locate:new()
local obj = {}
--specification
obj.action = nil
obj.itemid = nil
---------------
obj.found = {}
self.__index = self
setmetatable(obj,self)
return obj
end
function Locate:setParam(name, value)
if name == "action" then
self.action = value
elseif name == "itemid" then
self.itemid = value
elseif name == "uid" then
self.uid = value
end
end
function Locate:insert(v)
table.insert(self.found, v)
end
function Locate:checkContainer(uid)
local size = (isContainer(uid) and getContainerSize(uid) or 0)
for i = 0, size do
local tmp = getContainerItem(uid, i)
self:compare(tmp)
--print(getItemName(tmp.uid))
if type(tmp) ~= "boolean" and isContainer(tmp.uid) then
self:checkContainer(tmp.uid)
end
end
end
function Locate:compare(item)
local checkItemId = (self.itemid ~= nil)
if checkItemId then
if (item.itemid == self.itemid and self.action~= nil and item.actionid == self.action) then
self:insert(item.uid)
end
else
if (self.action~= nil and item.actionid == self.action) then
self:insert(item.uid)
end
end
end
function Locate:startLocating(cid)
for x = 1,10 do
local slotItem = getPlayerSlotItem(cid,x)
self:compare(slotItem)
if isContainer(slotItem.uid) == true then
self:checkContainer(slotItem.uid)
end
end
doCreatureSay(cid,table.maxv(self.found).." foud items",1)
end
function getItemsByActionId(cid,action)
local find = Locate:new()
find:setParam("action", action)
find:startLocating(cid)
return find.found
end
function getItemsByItemidAndActionId(cid,itemid, action)
local find = Locate:new()
find:setParam("action", action)
find:setParam("itemid", itemid)
find:startLocating(cid)
return find.found
end
As you can see it comes to 2 functions getItemsByActionId(cid,action) and getItemsByItemidAndActionId(cid,itemid, action).
They both returns array with uids of fiting items.
But lets give some example.
Lua:
for k, v in pairs(getItemsByActionId(cid,2000)) do
print("Hey my uid is: "..v)
end
Have fun with this simple code, I hope you can find it usefull somehow.
Cheers.