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

problem with variable and position

poe6man3

Member
Joined
Aug 6, 2020
Messages
87
Solutions
1
Reaction score
12
Lua:
local function getTargetPosition(creature)
    local targetPos = creature:getTarget():getPosition()
    print (targetPos.y)
    local targetNorthPos = targetPos
    targetNorthPos.y = targetNorthPos.y - 1
    print (targetPos.y)
    local targetSouthPos = targetPos
    targetSouthPos.y = targetSouthPos.y + 1
   
    targetNorthPos:sendMagicEffect(50)
    targetSouthPos:sendMagicEffect(30)
end


function onCastSpell(creature, variant)
    getTargetPosition(creature)
end

Can somebody tell my why variable targetPos i being changed when i created new variables which holds the position of the main variable and making changes to the new ones ?
 
Solution
Lua:
local function getTargetPosition(creature)
    local targetPos = creature:getTarget():getPosition()
    print (targetPos.y)
    local targetNorthPos = targetPos
    targetNorthPos.y = targetNorthPos.y - 1
    print (targetPos.y)
    local targetSouthPos = targetPos
    targetSouthPos.y = targetSouthPos.y + 1
  
    targetNorthPos:sendMagicEffect(50)
    targetSouthPos:sendMagicEffect(30)
end


function onCastSpell(creature, variant)
    getTargetPosition(creature)
end

Can somebody tell my why variable targetPos i being changed when i created new variables which holds the position of the main variable and making changes to the new ones ?
Long story short, because it's a table.

You need to create a new position, with the...
Lua:
local function getTargetPosition(creature)
    local targetPos = creature:getTarget():getPosition()
    print (targetPos.y)
    local targetNorthPos = targetPos
    targetNorthPos.y = targetNorthPos.y - 1
    print (targetPos.y)
    local targetSouthPos = targetPos
    targetSouthPos.y = targetSouthPos.y + 1
  
    targetNorthPos:sendMagicEffect(50)
    targetSouthPos:sendMagicEffect(30)
end


function onCastSpell(creature, variant)
    getTargetPosition(creature)
end

Can somebody tell my why variable targetPos i being changed when i created new variables which holds the position of the main variable and making changes to the new ones ?
Long story short, because it's a table.

You need to create a new position, with the information from targetPos instead of calling targetPos directly.
Lua:
local function getTargetPosition(creature)
    local targetPos = creature:getTarget():getPosition()
    
    local targetNorthPos = Position(targetPos.x, targetPos.y, targetPos.z)
    targetNorthPos.y = targetNorthPos.y - 1

    local targetSouthPos = Position(targetPos.x, targetPos.y, targetPos.z)
    targetSouthPos.y = targetSouthPos.y + 1
    
    targetNorthPos:sendMagicEffect(50)
    targetSouthPos:sendMagicEffect(30)
end


function onCastSpell(creature, variant)
    getTargetPosition(creature)
end

Or if you want the script shorter...
Lua:
local function getTargetPosition(creature)
    local targetPos = creature:getTarget():getPosition()    
    Position(targetPos.x, targetPos.y - 1, targetPos.z):sendMagicEffect(50)
    Position(targetPos.x, targetPos.y + 1, targetPos.z):sendMagicEffect(30)
end


function onCastSpell(creature, variant)
    getTargetPosition(creature)
end
 
Solution
Back
Top