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

Check for Player Position

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
Hi,

I have all the code I need but I only need to implement a simple if statement, however I have no idea how to do that.

I want to know how to check-- how the player is positioned to the target, i.e.

if [Player Pos Right From Target]

else if [Player Pos Left From Target]

etc
 
Code:
local target = getCreatureTarget(cid)
local targetpos = getCreaturePosition(target)

if getPlayerPosition(cid, {x=targetpos.x + 1, y=targetpos.y + 1, z=targetpos.z}) then ...
Not sure which TFS are you using, it should work for 0.3, 0.4.
 
Code:
local target = getCreatureTarget(cid)
local targetpos = getCreaturePosition(target)

if getPlayerPosition(cid, {x=targetpos.x + 1, y=targetpos.y + 1, z=targetpos.z}) then ...
Not sure which TFS are you using, it should work for 0.3, 0.4.

TFS 1.0

I am not sure why but this is the error, I don't know why it is a boolean value

Lua Script Error: [Test Interface]
data/spells/scripts/test/test1.lua
data/spells/scripts/test/test1.lua:5: attempt to index local 'targetpos' (a boolean value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/test/test1.lua:5: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/test/test1.lua
data/spells/scripts/test/test1.lua:8: '<eof>' expected near 'end'

Code:

Lua:
-- Check for Player Pos

local target = getCreatureTarget(cid)
local targetpos = getCreaturePosition(target)
if getPlayerPosition(cid, {x=targetpos.x + 1, y=targetpos.y + 1, z=targetpos.z}) then
    print("ddd")
end
 
lib/core/position.lua
Lua:
offsets = {
    [DIRECTION_NORTH] = {x = 0, y = -1},
    [DIRECTION_EAST] = {x = 1, y = 0},
    [DIRECTION_SOUTH] = {x = 0, y = 1},
    [DIRECTION_WEST] = {x = -1, y = 0},
    [DIRECTION_SOUTHWEST] = {x = -1, y = 1},
    [DIRECTION_SOUTHEAST] = {x = 1, y = 1},
    [DIRECTION_NORTHWEST] = {x = -1, y = -1},
    [DIRECTION_NORTHEAST] = {x = 1, y = -1}
}

function Position:setDirectionOffset(dir)
    local tmp = offsets[dir]
    if not tmp then
        return self
    end
    return Position(self.x + tmp.x, self.y + tmp.y, self.z)
end

Lua:
local target = player:getTarget()
if not target then
    return false
end

local targetPosition = target:getPosition()
local playerPosition = player:getPosition()
for i = 0, 3 do
    if targetPosition:setDirectionOffset(i) == playerPosition then
        print("k")
        break
    end
end
 
lib/core/position.lua
Lua:
offsets = {
    [DIRECTION_NORTH] = {x = 0, y = -1},
    [DIRECTION_EAST] = {x = 1, y = 0},
    [DIRECTION_SOUTH] = {x = 0, y = 1},
    [DIRECTION_WEST] = {x = -1, y = 0},
    [DIRECTION_SOUTHWEST] = {x = -1, y = 1},
    [DIRECTION_SOUTHEAST] = {x = 1, y = 1},
    [DIRECTION_NORTHWEST] = {x = -1, y = -1},
    [DIRECTION_NORTHEAST] = {x = 1, y = -1}
}

function Position:setDirectionOffset(dir)
    local tmp = offsets[dir]
    if not tmp then
        return self
    end
    return Position(self.x + tmp.x, self.y + tmp.y, self.z)
end

Lua:
local target = player:getTarget()
if not target then
    return false
end

local targetPosition = target:getPosition()
local playerPosition = player:getPosition()
for i = 0, 3 do
    if targetPosition:setDirectionOffset(i) == playerPosition then
        print("k")
        break
    end
end

It's TFS 1.0, I don't have this file (/core/position.lua), but the same thing here, even though it's 'player' in error, I've been trying to change it to creature but it didn't work

Lua Script Error: [Test Interface]
data/spells/scripts/test/test1.lua
data/spells/scripts/test/test1.lua:2: attempt to index global 'player' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/test/test1.lua:2: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/test/test1.lua

code:
Lua:
-- Check for Player Pos
local target = player:getTarget()
if not target then
    return false
end
local targetPosition = target:getPosition()
local playerPosition = player:getPosition()
for i = 0, 3 do
    if targetPosition:setDirectionOffset(i) == playerPosition then
        print("k")
        break
    end
end
 
where are you using the script?
the code is meant to be used inside of a function
in tfs 1.0 you have to construct a player object in order to use player methods
after the function you're using (it most likely has cid in the arguments) put local player = Player(cid)
put the direction offset code inside of global.lua instead then, but change function Position:setDirectionOffset(dir) to function Position.setDirectionOffset(self, dir)
 
where are you using the script?
the code is meant to be used inside of a function
in tfs 1.0 you have to construct a player object in order to use player methods
after the function you're using (it most likely has cid in the arguments) put local player = Player(cid)
put the direction offset code inside of global.lua instead then, but change function Position:setDirectionOffset(dir) to function Position.setDirectionOffset(self, dir)

this one is executed on 'function onCastSpell(cid, var)'

///EDIT

The first one started to work but it prints 'ddd' no matter the direction

Lua:
    -- Check for Player Pos
    local target = getCreatureTarget(cid)
    local targetpos = getCreaturePosition(target)
    if getPlayerPosition(cid, {x=targetpos.x + 1, y=targetpos.y + 1, z=targetpos.z}) then
        print("ddd")
    else
        print("no")
    end


--------
The second one, on the other hand another error with offset, could be not specified ?

attempt to call method 'setDirectionOffset' (a nil value)

Lua:
    -- Check for Player Pos
    local target = getCreatureTarget(cid)
    if not target then
        return false
    end
    local targetPosition = getCreaturePosition(target)
    local playerPosition = getPlayerPosition(cid)
    for i = 0, 3 do
        if targetPosition:setDirectionOffset(i) == playerPosition then
            print("k")
            break
        end
    end

//EDIT2

After adding to the global.lua

Lua Script Error: [Main Interface]
data/global.lua
data/global.lua:790: table index is nil
stack traceback:
[C]: in function '__newindex'
data/global.lua:790: in main chunk
[Warning - ScriptingManager::loadScriptSystems] Can not load data/global.lua
 
Last edited:
//Fixed errors;

- remove square brackets from global variables, i.e. [DIRECTION_NORTH] -> DIRECTION_NORTH

@Xeraphus

This code below, prints "k" when I am on the same position as target, not sure if this is how it meant to be. How do you check if you're standing north from target?

Lua:
    for i = 0, 3 do
        if targetPosition:setDirectionOffset(i) == playerPosition then
            print("k")
            break
        end
    end

This one doesn't work |: (also removes important part of the loop, (i))

Lua:
    for i = 0, 3 do
        if targetPosition:setDirectionOffset(DIRECTION_NORTH) == playerPosition then
            print("k")
            break
        end
    end


Global Lua:

Lua:
offsets = {
    DIRECTION_NORTH = {x = 0, y = -1},
    DIRECTION_EAST = {x = 1, y = 0},
    DIRECTION_SOUTH = {x = 0, y = 1},
    DIRECTION_WEST = {x = -1, y = 0},
    DIRECTION_SOUTHWEST = {x = -1, y = 1},
    DIRECTION_SOUTHEAST = {x = 1, y = 1},
    DIRECTION_NORTHWEST = {x = -1, y = -1},
    DIRECTION_NORTHEAST = {x = 1, y = -1}
}
function Position:setDirectionOffset(dir)
    local tmp = offsets[dir]
    if not tmp then
        return self
    end
    return Position(self.x + tmp.x, self.y + tmp.y, self.z)
end
 
Last edited:
//Fixed errors;

- remove square brackets from global variables, i.e. [DIRECTION_NORTH] -> DIRECTION_NORTH

@Xeraphus

This code below, prints "k" when I am on the same position as target, not sure if this is how it meant to be. How do you check if you're standing north from target?

Lua:
    for i = 0, 3 do
        if targetPosition:setDirectionOffset(i) == playerPosition then
            print("k")
            break
        end
    end

This one doesn't work |: (also removes important part of the loop, (i))

Lua:
    for i = 0, 3 do
        if targetPosition:setDirectionOffset(DIRECTION_NORTH) == playerPosition then
            print("k")
            break
        end
    end


Global Lua:

Lua:
offsets = {
    DIRECTION_NORTH = {x = 0, y = -1},
    DIRECTION_EAST = {x = 1, y = 0},
    DIRECTION_SOUTH = {x = 0, y = 1},
    DIRECTION_WEST = {x = -1, y = 0},
    DIRECTION_SOUTHWEST = {x = -1, y = 1},
    DIRECTION_SOUTHEAST = {x = 1, y = 1},
    DIRECTION_NORTHWEST = {x = -1, y = -1},
    DIRECTION_NORTHEAST = {x = 1, y = -1}
}
function Position:setDirectionOffset(dir)
    local tmp = offsets[dir]
    if not tmp then
        return self
    end
    return Position(self.x + tmp.x, self.y + tmp.y, self.z)
end
because you removed the square brackets when you weren't supposed to.
now the table contains string indexes, might as well have ["DIRECTION_NORTH"] instead of [0] (DIRECTION_NORTH is an enum which represents 0)
 
Back
Top