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

function getCreatureInRange(type, fromPos, toPos, toGet)

Joined
Apr 17, 2008
Messages
1,922
Solutions
1
Reaction score
188
Location
Venezuela
Tested and working on TFS 0.3.6

Here i'm releasing my function...

Useful to get the count or name from creatures in a certain range

Here is...
Lua:
function getCreatureInRange(type, fromPos, toPos, toGet, itemid)

local types = 
{
	["player"] = isPlayer,
	["monster"] = isMonster,
	["npc"] = isNpc,
	["creature"] = isCreature
}
local tmp = {}

	local type = types[type]
	if(not type) then
		print('[getCreatureInRange]>> Unknow type')
		return 0
	end

	local thing = nil
	for x = fromPos.x, toPos.x do
		for y = fromPos.y, toPos.y do
			for z = fromPos.z, toPos.z do
				for s = 1, 253 do
					local position = {x = x, y = y, z = z, stackpos = s}
					thing = getTileThingByPos(position)
					if(type(thing.uid) == true) then
						table.insert(tmp, thing.uid)
					end
				end
			end
		end
	end
	if(toGet == "count") then
		return table.maxn(tmp)
	elseif(toGet == "name") then
		return tmp
	else
		print('[getCreatureInRange]>> Unknow creature to get')
		return 0
	end
	return true
end

So... How to use?

Here is an example to how to use to get the count:
Lua:
--in the selected area, there is a demon, a troll and a fire elemental, so...

local fromPos = {x = 100, y = 100, z = 7, stackpos = 1}
local toPos = {x = 150, y = 150, z = 7, stackpos = 1}

local t = getCreatureInRange("monster", fromPos, toPos, "count")

print('>> There are ' .. t .. ' monsters)
Code:
>> There are 3 monsters

How to get the names:
Lua:
--to get the names in the selected area:

local fromPos = {x = 100, y = 100, z = 7, stackpos = 1}
local toPos = {x = 150, y = 150, z = 7, stackpos = 1}

local t = getCreatureInRange("monster", fromPos, toPos, "name")

for i = 1, table.maxn(t) do
	print('>> The names are ' .. getCreatureName(t[i]))
end
Code:
>> The names are Demon
>> The names are Troll
>> The names are Fire Elemental

The names are returned in a table.

Note:
* If you write "creature" in type, function will return names or count of all creatures (monsters, players and npc)
 
Last edited:
Dang Dark!
This is nice. :thumbup:

Rep+, I rarely give rep :p
 
its like shawak's function, but reduced
@chawk: i almost reach your green bars!
 
sorry but refresh but is very useful and i shoveling to this thread lot of minutes xd
 
Back
Top