• 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/stopEvent remove item

CastorFlynn

Member
Joined
Aug 29, 2021
Messages
89
Reaction score
8
In this creaturescript code, when a creature is killed, a green vortex is created. If the same creature dies in the position of the green vortex, it turns purple and, with the same logic, turns red. I would like each vortex created/transformed to have 30 seconds before being removed. So if the green vortex was created, if 20sec passes and I transform it into purple it would have another 30sec until it is removed if it is not transformed into a red vortex. If it turns red, it would take another 30s until it is removed. In the current script the removal time is not "renewed/incremented".

Lua:
local scheduledEvents = {}

local function removeVortex(pos)
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)
    if vortex then
        vortex:remove()
    end
    scheduledEvents[pos] = nil  -- Clears the scheduled event by removing the vortex
end

local function scheduleVortexRemoval(pos)
    if scheduledEvents[pos] then
        stopEvent(scheduledEvents[pos])  -- Cancels the previous event if it exists
    end
    scheduledEvents[pos] = addEvent(removeVortex, 30 * 1000, pos)  -- Schedule a new removal event
end

local corruptedSoul = CreatureEvent("CorruptedSoul")

function corruptedSoul.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end

    local pos = targetMonster:getPosition()
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)

    if not vortex then
        Game.createItem(26394, 1, pos)
        scheduleVortexRemoval(pos)
    else
        if vortex:getId() == 26394 then
            vortex:transform(26395)
        elseif vortex:getId() == 26395 then
            vortex:transform(26396)
        end
        scheduleVortexRemoval(pos)
    end

    return true
end

corruptedSoul:register()
 
Logic looks ok from a brief glance. It might not like using Position as a key.

Try this(untested):
Lua:
local scheduledEvents = {}

local function removeVortex(pos, key)
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)
    if vortex then
        vortex:remove()
    end
    scheduledEvents[key] = nil  -- Clears the scheduled event by removing the vortex
end

local function scheduleVortexRemoval(pos)
    local key = pos.x .. ":" .. pos.y .. ":" .. pos.z
    if scheduledEvents[key] then
        stopEvent(scheduledEvents[key])  -- Cancels the previous event if it exists
    end
    scheduledEvents[key] = addEvent(removeVortex, 30 * 1000, pos, key)  -- Schedule a new removal event
end

local corruptedSoul = CreatureEvent("CorruptedSoul")

function corruptedSoul.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end

    local pos = targetMonster:getPosition()
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)

    if not vortex then
        Game.createItem(26394, 1, pos)
    else
        if vortex:getId() == 26394 then
            vortex:transform(26395)
        elseif vortex:getId() == 26395 then
            vortex:transform(26396)
        end
    end

    scheduleVortexRemoval(pos)
    return true
end

corruptedSoul:register()
 
Each time a vortex is created or transformed, scheduleVortexRemoval is called with a 30 second delay. This resets the timer to ensure the vortex will be removed after 30 seconds from the most recent transformation or creation.

Lua:
local scheduledEvents = {}

local function removeVortex(pos)
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)
    if vortex then
        vortex:remove()
    end
    scheduledEvents[pos] = nil
end

local function scheduleVortexRemoval(pos, delay)
    if scheduledEvents[pos] then
        stopEvent(scheduledEvents[pos])
    end
    scheduledEvents[pos] = addEvent(removeVortex, delay, pos)
end

local corruptedSoul = CreatureEvent("CorruptedSoul")

function corruptedSoul.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end

    local pos = targetMonster:getPosition()
    local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)

    if not vortex then
        Game.createItem(26394, 1, pos)
        scheduleVortexRemoval(pos, 30 * 1000)
    else
        if vortex:getId() == 26394 then
            vortex:transform(26395)
        elseif vortex:getId() == 26395 then
            vortex:transform(26396)
        end
        scheduleVortexRemoval(pos, 30 * 1000)
    end

    return true
end

corruptedSoul:register()
 
Back
Top