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

TFS 1.X+ tfs 1.x doubt getNextPosition

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,210
Solutions
35
Reaction score
206
My player is in pos: "Position: 360, 734, 7"
And i'm looking for the south: "360, 735, 7"

firt code:
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerPos = player:getPosition()
    local direction = player:getDirection()
    local newPos = playerPos:getNextPosition(direction)
 
    print("You are in pos: "..playerPos.x..", "..playerPos.y..".")
    print("You are looking pos: "..newPos.x..", "..newPos.y..".")
    return true
end

this first code print incorrect newPos, look it:
1591449398713.png

and this second script, all prints correct, but why it occours??
i have only changed line 4.

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerPos = player:getPosition()
    local direction = player:getDirection()
    local newPos = player:getPosition():getNextPosition(direction)
 
    print("You are in pos: "..playerPos.x..", "..playerPos.y..".")
    print("You are looking pos: "..newPos.x..", "..newPos.y..".")
    return true
end
1591449509287.png


resume:
if i use this form, the script dont work fine:
LUA:
local direction = player:getDirection()
local playerPos = player:getPosition()
local newPos = playerPos:getNextPosition(direction)

and if i use this form, all works fine
LUA:
local direction = player:getDirection()
local newPos = player:getPosition():getNextPosition(direction)

but i don't know why it happens, could someone explain to me?
 
Solution
Ah, I understand now.
You're saving the player's position as a variable and attempting to print the original position value after it's already been changed via Position.getNextPosition.
Tables in Lua are references, if you pass a table to a function and edit it, the original is altered, not a copy of it. So when you save player:getPosition() to playerPos and then use playerPos:getNextPosition(direction), it edits playerPos (see self.x = ... and self.y = ... inside the function) and also returns it. So in the end after you use the next position, you're left with the intended next position being assigned to newPos, but playerPos is the same exact thing because it also got edited.
If you want...
You need to add return self to Position.getNextPosition before the last end in data/lib/core/position.lua.

Edit: Nevermind I completely skimmed this and misunderstood, can you show what your Position.getNextPosition functionlooks like?
 
You need to add return self to Position.getNextPosition before the last end in data/lib/core/position.lua.

Edit: Nevermind I completely skimmed this and misunderstood, can you show what your Position.getNextPosition functionlooks like?
here are my current getNextPosition:
LUA:
function Position:getNextPosition(direction, steps)
    local offset = Position.directionOffset[direction]
    if offset then
        steps = steps or 1
        self.x = self.x + offset.x * steps
        self.y = self.y + offset.y * steps
    end
    return self
end
 
Ah, I understand now.
You're saving the player's position as a variable and attempting to print the original position value after it's already been changed via Position.getNextPosition.
Tables in Lua are references, if you pass a table to a function and edit it, the original is altered, not a copy of it. So when you save player:getPosition() to playerPos and then use playerPos:getNextPosition(direction), it edits playerPos (see self.x = ... and self.y = ... inside the function) and also returns it. So in the end after you use the next position, you're left with the intended next position being assigned to newPos, but playerPos is the same exact thing because it also got edited.
If you want the original behavior to work as intended, you need to copy the position and edit the copy and return it instead:
LUA:
function Position:getNextPosition(direction, steps)
    local copy = Position(self.x, self.y, self.z)
    local offset = Position.directionOffset[direction]
    if offset then
        steps = steps or 1
        copy.x = copy.x + offset.x * steps
        copy.y = copy.y + offset.y * steps
    end
    return copy
end
 
Solution
Ah, I understand now.
You're saving the player's position as a variable and attempting to print the original position value after it's already been changed via Position.getNextPosition.
Tables in Lua are references, if you pass a table to a function and edit it, the original is altered, not a copy of it. So when you save player:getPosition() to playerPos and then use playerPos:getNextPosition(direction), it edits playerPos (see self.x = ... and self.y = ... inside the function) and also returns it. So in the end after you use the next position, you're left with the intended next position being assigned to newPos, but playerPos is the same exact thing because it also got edited.
If you want the original behavior to work as intended, you need to copy the position and edit the copy and return it instead:
LUA:
function Position:getNextPosition(direction, steps)
    local copy = Position(self.x, self.y, self.z)
    local offset = Position.directionOffset[direction]
    if offset then
        steps = steps or 1
        copy.x = copy.x + offset.x * steps
        copy.y = copy.y + offset.y * steps
    end
    return copy
end
I spent 3 hours trying to understand this shit
I thought I was missing the code, I didn't even think about looking at the function,
THX
Post automatically merged:

Ah, I understand now.
You're saving the player's position as a variable and attempting to print the original position value after it's already been changed via Position.getNextPosition.
Tables in Lua are references, if you pass a table to a function and edit it, the original is altered, not a copy of it. So when you save player:getPosition() to playerPos and then use playerPos:getNextPosition(direction), it edits playerPos (see self.x = ... and self.y = ... inside the function) and also returns it. So in the end after you use the next position, you're left with the intended next position being assigned to newPos, but playerPos is the same exact thing because it also got edited.
If you want the original behavior to work as intended, you need to copy the position and edit the copy and return it instead:
LUA:
function Position:getNextPosition(direction, steps)
    local copy = Position(self.x, self.y, self.z)
    local offset = Position.directionOffset[direction]
    if offset then
        steps = steps or 1
        copy.x = copy.x + offset.x * steps
        copy.y = copy.y + offset.y * steps
    end
    return copy
end
dont know why, but this new function getNextPosition bugged some things.
when i try to use /attr action,123 for exemple, return this message:

LUA:
10:59 Item not found.

if i back getNextPosition function to default, the /attr action works fine
 
Last edited:
I spent 3 hours trying to understand this shit
I thought I was missing the code, I didn't even think about looking at the function,
THX
Post automatically merged:


dont know why, but this new function getNextPosition bugged some things.
when i try to use /attr action,123 for exemple, return this message:

LUA:
10:59 Item not found.

if i back getNextPosition function to default, the /attr action works fine
It's probably because it's being used as intended in other scripts and wants to alter the original, not return a completely new copy of the next position.
You need to decide what to do next, either wrap that into a new function and just call it something different and call that when you need the copy or change all places where the original is used and make it use the copy instead of the original.
 
It's probably because it's being used as intended in other scripts and wants to alter the original, not return a completely new copy of the next position.
You need to decide what to do next, either wrap that into a new function and just call it something different and call that when you need the copy or change all places where the original is used and make it use the copy instead of the original.
i have created a new function getNextPosition with a new name and your code, and ill use it in specific code, it will works fine, thx man!
 
Back
Top