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

Lua addEvent problem, it's getting an old posistion of a target

Ahilphino

Excellent OT User
Joined
Jun 5, 2013
Messages
1,667
Solutions
1
Reaction score
734
ok so i am not at home right now but i have written a script explaining my problem

Code:
function onCastSpell(creature, var)
local cid = creature:getId()
  local player = Player(cid)
  local targ = player:getTarget()
    addEvent(doSendDistanceShoot, 3000, player:getPosition(), targ:getPosition(), CONST_ANI_REDSTAR)
    return true
end

ok i know its shit code but look at this, when i use this spell after 3 seconds it should hit the target with an asssassin star, HOWEVER if the target has moved during those 3 seconds, the asssassin star will shoot from where i was 3 seconds ago to where the target was 3 seconds ago. How can I fix this and make it so the casters and targets posistions gets updated after those 3 seconds? So that it shoots from my current posistion and to where the target is right now?
 
Last edited:
The function doSendDistanceShoot is executed after 3 seconds, but it gets the parameters from the moment it executes addEvent, so you have to get the position in the function that is executed after 3 seconds by the function addEvent.
Code:
local function doSendTimedDistanceShoot(cid, target)
     local player, target = Player(cid), Creature(target)
     if player and target then
         player:getPosition():sendDistanceEffect(target:getPosition(), CONST_ANI_REDSTAR)
     end
end

Code:
addEvent(doSendTimedDistanceShoot, 3000, player.uid, target.uid)
 
fck man sex me right now

The function doSendDistanceShoot is executed after 3 seconds, but it gets the parameters from the moment it executes addEvent, so you have to get the position in the function that is executed after 3 seconds by the function addEvent.
Code:
local function doSendTimedDistanceShoot(cid, target)
     local player, target = Player(cid), Creature(target)
     if player and target then
         player:getPosition():sendDistanceEffect(target:getPosition(), CONST_ANI_REDSTAR)
     end
end

Code:
addEvent(doSendTimedDistanceShoot, 3000, player.uid, target.uid)

Ok so I manged to add this what u did with some twweaks to some of my more advanced spells but could u show me an example how u would do it in this? I can't figure it out :/

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -0.1, 0, -0.3, 0)

local function secondShot(cid, position, targetPosition)
    if not Creature(cid) then return end

    position:sendDistanceEffect(targetPosition, CONST_ANI_ENERGY)
    doCombat(cid, combat, positionToVariant(targetPosition))
end



local directions = {
    {DIRECTION_NORTH, DIRECTION_SOUTH},
    {DIRECTION_WEST, DIRECTION_EAST}
}
local additionalTargets = 2

local function doBolt(cid, var, targetId, fromPosition)
    local caster = Creature(cid)
    local target = Creature(targetId)

    if not caster or not target then
        return
    end

    fromPosition:sendDistanceEffect(target:getPosition(), CONST_ANI_ENERGY)
    doCombat(cid, combat, var)

    local oldTarget
        local spectators = Game.getSpectators(target:getPosition(), false, false, 3, 3, 3, 3)
        for i = 1, #spectators do
        for j = #spectators, 1, -1 do
            if spectators[j].uid == cid or spectators[j].uid == target.uid then
                table.remove(spectators, j)
            end
        end

        if #spectators == 0 then
            return
        end

        oldTarget = target
        target = spectators[#spectators]
        addEvent(secondShot, i * 900, cid, oldTarget:getPosition(), target:getPosition())
    end
end

function onCastSpell(creature, var)
    local direction = creature:getDirection()
    local position = creature:getPosition()

    local positions = {}
    for i = 1, #directions do
        if isInArray(directions[i], direction) then
            local newPosition
            for _, dir in pairs(directions[i]) do
                newPosition = Position(position)
                newPosition:getNextPosition(dir)
                table.insert(positions, newPosition)
            end
        end
    end

    for i = 1, #positions do
        position:sendDistanceEffect(positions[i], CONST_ANI_ENERGY)
        positions[i]:sendMagicEffect(CONST_ME_ENERGYHIT)
        -- can only be used if it is ensured that a target is needed for the spell
        addEvent(doBolt, 100, creature.uid, var, creature:getTarget().uid, positions[i])
    end
    return true
end
 
Last edited by a moderator:
Ok so I manged to add this what u did with some twweaks to some of my more advanced spells but could u show me an example how u would do it in this? I can't figure it out :/

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -0.1, 0, -0.3, 0)

local function secondShot(cid, position, targetPosition)
    if not Creature(cid) then return end

    position:sendDistanceEffect(targetPosition, CONST_ANI_ENERGY)
    doCombat(cid, combat, positionToVariant(targetPosition))
end



local directions = {
    {DIRECTION_NORTH, DIRECTION_SOUTH},
    {DIRECTION_WEST, DIRECTION_EAST}
}
local additionalTargets = 2

local function doBolt(cid, var, targetId, fromPosition)
    local caster = Creature(cid)
    local target = Creature(targetId)

    if not caster or not target then
        return
    end

    fromPosition:sendDistanceEffect(target:getPosition(), CONST_ANI_ENERGY)
    doCombat(cid, combat, var)

    local oldTarget
        local spectators = Game.getSpectators(target:getPosition(), false, false, 3, 3, 3, 3)
        for i = 1, #spectators do
        for j = #spectators, 1, -1 do
            if spectators[j].uid == cid or spectators[j].uid == target.uid then
                table.remove(spectators, j)
            end
        end

        if #spectators == 0 then
            return
        end

        oldTarget = target
        target = spectators[#spectators]
        addEvent(secondShot, i * 900, cid, oldTarget:getPosition(), target:getPosition())
    end
end

function onCastSpell(creature, var)
    local direction = creature:getDirection()
    local position = creature:getPosition()

    local positions = {}
    for i = 1, #directions do
        if isInArray(directions[i], direction) then
            local newPosition
            for _, dir in pairs(directions[i]) do
                newPosition = Position(position)
                newPosition:getNextPosition(dir)
                table.insert(positions, newPosition)
            end
        end
    end

    for i = 1, #positions do
        position:sendDistanceEffect(positions[i], CONST_ANI_ENERGY)
        positions[i]:sendMagicEffect(CONST_ME_ENERGYHIT)
        -- can only be used if it is ensured that a target is needed for the spell
        addEvent(doBolt, 100, creature.uid, var, creature:getTarget().uid, positions[i])
    end
    return true
end
Would be easier if you tell us what that second spell should do. How it should look in game? Is it 'bolt that bounces between close targets'?
 
Would be easier if you tell us what that second spell should do. How it should look in game? Is it 'bolt that bounces between close targets'?

well yes its just a ball that bounces between targets, the only issue with it though is the problem i mentioned earlier (if the target moves, it's goin to miss)
while i managed to do what limos mentioned in the first post and apply it to some of my other scirpts, i have some trouble to do it for this so i would appreciate if someone could show how they would do it for that script
 
Code:
addEvent(secondShot, i * 900, cid, oldTarget:getPosition(), target:getPosition())
Get the positions in the function secondShot and here just add the creatureids (oldTarget.uid and target.uid) instead of the positions.
 
nevermind! I think i got it :P will be trying to fix it myself :D

nevermind lol =_=

been trying for 1hour now im lua retard
but how can i get the posistion of oldTarget in the function secondShot? since oldTarget is in another function i dunno how i can get his posistion since it will just return nil
 
Last edited by a moderator:
When you add parameters for the function in addEvent, you can use them in the function, what matters is the position of the parameters, so in the function you can call it something else.
Code:
addEvent(secondShot, i * 900, cid, oldTarget.uid, target.uid)
Here the function secondShot has 3 parameters, first one is cid, second is creatureid of oldTarget, third is creatureid of target.

Now in the function secondShot you see 3 parameters.
Code:
local function secondShot(cid, position, targetPosition)
     -- blabla
end
You can just call oldTarget and target or something since they aren't positions anymore.
Code:
local function secondShot(cid, oldTarget, target)
     -- blabla
end
Then use these creatureids to get the position of oldTarget and target.
Code:
local oldTarget, target = Creature(oldTarget), Creature(target)
if oldTarget and target then  
     oldTarget:getPosition():sendDistanceEffect(target:getPosition(), CONST_ANI_ENERGY)
     -- blabla
 
Back
Top