This piece of code was actualy writen to handle items founding, using diffrent callbacks.. Instead of writing boring parts all the time, I decided to write something that allowed me doing this faster
Maybe some people would use it, below I will post example how actualy use it: )
Example:
Cya. : )
@edit some function base on this :
Maybe some people would use it, below I will post example how actualy use it: )
Code:
--[[------------]
[[[[[[ By Tarjei]]]]
--[[------------]]
ItemCheck = {
positions = nil, --{position, itemid}
callbacks = {} --{call succes, call fauiliure}
}
function ItemCheck:new()
local obj = {}
obj.positions = {}
obj.callbacks = {}
self.__index = self
return setmetatable(obj,self)
end
function ItemCheck:add(position,...)
if type(position) ~= "table" then
return false
end
table.insert(self.positions, {position = position, items = arg})
return
end
local CALLBACK_SUCCES = 1
local CALLBACK_ERROR = 2
function ItemCheck:setCallback(calltype, func,...)
if(type(func) == "function" )then
self.callbacks[calltype] = {func = func, arg = arg}
end
end
function ItemCheck:execute()
local succes = true
local printError = false
local err = " s"
for k,pair in pairs(self.positions) do
for j, item in pairs(pair.items) do
if type(item) == "table" then
local tItem = getTileItemById(pair.position, item.id)
print(tItem.uid," ", tItem.type)
if tItem.uid == nil or tItem.uid <= 0 then
succes = false
err = err .. "Item: ".. item.id.." not found at Pos: x = "..pair.position.x.." y="..pair.position.y.." z="..pair.position.z.."; "
break
end
if (tItem.type < item.count) then
err = err .. "Item: ".. item.id.." >> not enought count at Pos: x = "..pair.position.x.." y="..pair.position.y.." z="..pair.position.z.."; "
succes = false
break
end
end
end
end
print(err)
if succes then
local arguments = self.callbacks[CALLBACK_SUCCES].arg
self.callbacks[CALLBACK_SUCCES].func(unpack(arguments))
else
local arguments = self.callbacks[CALLBACK_SUCCES].arg
self.callbacks[CALLBACK_ERROR].func(unpack(arguments))
if printError then
print(err)
end
end
end
Example:
Code:
function onSay(cid, words, param)
local check = ItemCheck:new() --CREATES NEW ITEMCHCHECK OBJECT
check:add(getCreaturePosition(cid), {id = 2143, count = 3}, {id = 2160, count = 5})
check:add({x=1345,y=1345,z=7}, {id = 2143, count = 3}, {id = 2160, count = 5}, {id = 2160, count = 5},{id = 2160, count = 5})
check:setCallback(CALLBACK_SUCCES, function(cid, position) -- Function that's called when all items are found :)
doSendMagicEffect(position,31)
end, cid, getCreaturePosition(cid))
check:setCallback(CALLBACK_ERROR, function(cid) -- Otherwise calling this doCreatureSay(cid,"ERRROROROROROR",1)
end, cid)
check:execute() -- trigers checking items : P
return TRUE
end
Cya. : )
@edit some function base on this :
Lua:
function areItemsOnPositions(...)
--callback is
-- {pos, id, count} --structure of each argument
ret = true
local check = ItemCheck:new() --CREATES NEW ITEMCHCHECK OBJECT
for k, v in pairs(arg) do
check:add(v[1], {id = v[2], count = v[3] ~= nil and v[3] or 1})
end
check:setCallback(CALLBACK_SUCCES, function() end)
check:setCallback(CALLBACK_ERROR, function() -- Otherwise calling this
ret = false
end)
check:execute() -- trigers checking items : P
return ret
end
Last edited: