• 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!

ItemCheck -> Checking if items are on specific positions. Have fun with it xD

tarjei

Necronian Engineer
Joined
May 25, 2008
Messages
505
Reaction score
126
Location
Poland
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 :p

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:
Nice, it must be part of the official functions

How u add this to your OT?

Tnx a lot!
(y)(y)

-Obsdark-
 
I used it in some quest I belive , can't remember XD
However it can be used as a simple function if you write one :)
 
Yes but, i just put it like part of the script? it doesn't a function to call from another scripts?

i mean, it would be easier that way aren't it? but it's also very cool if you say to me

"no, just copy-paste on the script u wanna use it"
(or something like)

¡Have a nice day & Tnx a lot!
(y)(y)


-Obsdark-
 
The best way of using it is to paste the code to libs (or some global file) then you can easily create make calls to it from each script ^^
Then you just do like in example XD

- - - Updated - - -

Added sample function.
 
Back
Top