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

isInArea, loops not needed!

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
218
Location
Sweden
Hello, I've noticed alot of people use loops to check if someone's in an area. Sure it's needed sometimes, but not as often as you use it.

When shall you use loops for checking areas? Well, never because you shall use my mapArea heuaheuah xD But well that's classed as looping too! But seriously, when loops should be used is if you want to for example, check how many creatures is in an area, or to see if any players remain in an area.

However, when you are going to check if a certain creature is in an area, for example when you use a rune you only want the rune to be used in depot... Then you do not loop through the depot area to check if the player's in there.

Here's how you'd do:
PHP:
fromPos = {x=455, y=344, z=7}
toPos = {x=499, y=389, z=7}
if isInArea(getPlayerPosition(cid), fromPos, toPos) then
    -- Action! ;D
end

And the isInArea function:
PHP:
-- Function by Colandus!
function isInArea(pos, fromPos, toPos)
    if pos.x >= fromPos.x and pos.x <= toPos.x then
        if pos.y >= fromPos.y and pos.y <= toPos.y then
            if pos.z >= fromPos.z and pos.z <= toPos.z then
                return true
            end
        end
    end
    return false
end
No loops, just if-statements ;)

Also, if you don't like it to return true and false, just change it to TRUE and FALSE :p


Enjoy,
Colandus
 
Hehe, no problems, but it's sad I must spread the link before someone notice it ;(

And yeah, the function itself goes to global.lua and where you want to use it is your choise :)
 
I already saw it before, but didn't reply. So you didn't noticed that:p I think many people forget to reply...
 
Error's.
Code:
[02/06/2008  21:16:02] Lua Script Error: [CreatureScript Interface] 
[02/06/2008  21:16:02] data/creaturescripts/scripts/tokens.lua

[02/06/2008  21:16:02] data/global.lua:669: attempt to index local 'pos' (a number value)
[02/06/2008  21:16:02] Warning: [Event::loadScript] Can not load script. data/creaturescripts/scripts/tokens.lua

Global.lua.
Code:
function isInArea(pos, fromPos, toPos)
    if pos.x >= fromPos.x and pos.x <= toPos.x then
        if pos.y >= fromPos.y and pos.y <= toPos.y then
            if pos.z >= fromPos.z and pos.z <= toPos.z then
                return true
            end
        end
    end
    return false
end

Script.
Code:
fromPos = {x=455, y=344, z=7}
toPos = {x=499, y=389, z=7}
local loot = 6527
if isInArea(getPlayerPosition(cid), fromPos, toPos) then
doPlayerAddItem(killer,loot,1)
end

Whats wrong? =(
 
Back
Top