• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua recursive addEvent and how to stop it

kito2

www.masteria.net
Joined
Mar 9, 2009
Messages
3,764
Solutions
1
Reaction score
227
Location
Chile, Santiago
Hey,

I would like to create a recursive event but I am having problems on how to work on it:

Code:
local function recursion(var1, var2, var3)
    do var1 something
  
    do var2 another thing
  
    do var3 anything else
  
    addEvent(recursion, 90 * 60, var 1, var2, var3)

end

function onUse(player, item)

    if item.itemid == 82319112921 then
        addEvent(recursion, 90 * 60, var1, var2, va3)
    end

    return true
end

I don't know if this is the way to manage the recursion.... Could someone explain me please?

Thanks!
 
Hey,

I would like to create a recursive event but I am having problems on how to work on it:

Code:
local function recursion(var1, var2, var3)
    do var1 something
 
    do var2 another thing
 
    do var3 anything else
 
    addEvent(recursion, 90 * 60, var 1, var2, var3)

end

function onUse(player, item)

    if item.itemid == 82319112921 then
        addEvent(recursion, 90 * 60, var1, var2, va3)
    end

    return true
end

I don't know if this is the way to manage the recursion.... Could someone explain me please?

Thanks!
TFS?
 
Code:
local function recursion(var1, var2, var3)
   do var1 something
 
    do var2 another thing
 
    do var3 anything else
  
    if not x then
        addEvent(recursion, 90 * 60, var 1, var2, var3)
    end

end

function onUse(player, item)

    if item.itemid == 82319112921 then
        addEvent(recursion, 90 * 60, var1, var2, va3)
    end

    return true
end
Like this? If x it stops.
 
Code:
local function recursion(var1, var2, var3)
   do var1 something
 
    do var2 another thing
 
    do var3 anything else
 
    if not x then
        addEvent(recursion, 90 * 60, var 1, var2, var3)
    end

end

function onUse(player, item)

    if item.itemid == 82319112921 then
        addEvent(recursion, 90 * 60, var1, var2, va3)
    end

    return true
end
Like this? If x it stops.
This will work depending on what you need to do, however look at this example:
"A script that you pull a lever and 1 minute after if the lever is still active, place an item in x position."

If you pull the lever before the 1 minute nad pull again it will execute 2 times because it didn't stop the previous event from happening.

Topic:
Code:
GLOBAL_RECURSIONID = false

local function recursion(var1, var2, var3)
    do var1 something
 
    do var2 another thing
 
    do var3 anything else
 
    GLOBAL_RECURSIONID = addEvent(recursion, 90 * 60, var 1, var2, var3)
end

function onUse(player, item)

    if item.itemid == 82319112921 then
        GLOBAL_RECURSIONID = addEvent(recursion, 90 * 60, var1, var2, va3)
    end
 
    if item.itemid == 1230495 and GLOBAL_RECURSIONID then
        stopEvent(GLOBAL_RECURSIONID)
        GLOBAL_RECURSIONID = false
    end

    return true
end

If you need to know if the event was already executed, you need another global variable to store the time it was called + delay of event and check using os.mtime()
 
Last edited:
This will work depending on what you need to do, however look at this example:
"A script that you pull a lever and 1 minute after if the lever is still active, place an item in x position."

If you pull the lever before the 1 minute nad pull again it will execute 2 times because it didn't stop the previous event from happening.

Topic:
Code:
GLOBAL_RECURSIONID = false

local function recursion(var1, var2, var3)
    do var1 something
 
    do var2 another thing
 
    do var3 anything else
 
    GLOBAL_RECURSIONID = addEvent(recursion, 90 * 60, var 1, var2, var3)
end

function onUse(player, item)

    if item.itemid == 82319112921 then
        GLOBAL_RECURSIONID = addEvent(recursion, 90 * 60, var1, var2, va3)
    end
 
    if item.itemid == 1230495 and GLOBAL_RECURSIONID then
        stopEvent(GLOBAL_RECURSIONID)
        GLOBAL_RECURSIONID = false
    end

    return true
end

If you need to know if the event was already executed, you need another global variable to store the time it was called + delay of event and check using os.mtime()

Just made this code:

Code:
effectTile = false

function onStepIn(creature, item, position, fromPosition)
   
    if not creature:isPlayer() then
        return true
    end
   
    local player = Player(creature)
    local ground = Tile(position):getItemById(11504)
    local id
   
    if ground.actionid == 8760 then
        id = 19 -- gren
        ground:setAttribute(ITEM_ATTRIBUTE_ACTIONID, 8761)
    elseif ground.actionid == 8761 then
        id = 20 -- red
        ground:setAttribute(ITEM_ATTRIBUTE_ACTIONID, 8762)
    elseif ground.actionid == 8762 then
        id = 22 -- yellow
        ground:setAttribute(ITEM_ATTRIBUTE_ACTIONID, 8763)
    elseif ground.actionid == 8763 then
        id = nil
        ground:setAttribute(ITEM_ATTRIBUTE_ACTIONID, 8760)
        if effectTile then
            stopEvent(effecTile)
        end
        return true
    end
   
    effectTile = addEvent(effectTile, 2 * 1000, id, position)
   
    return true
end

function effectTile(id, position)
    doSendMagicEffect(position, id)
    addEvent(effectTile, 2 * 1000, id, position)
    return true
end

And I am getting this error:

0b94fdecca724e65a5e3c4968ba44a33.png


I can't understand why...

The idea is when player stepIn it keeps showing a effect till someone stepIn again and another magic effect should be sent. If it reachs actionid 8763 then the event is stopped.

Could you explain what is wrong?
 
your function and a variable both have the same name. It doesn't assume one is correct, it just picks the first one it sees. It's trying to use your variable effectTile instead of your function effectTile. Change the variable name to like et or something.
Also
stopEvent(effecTile)
lil typo there ;)
 
your function and a variable both have the same name. It doesn't assume one is correct, it just picks the first one it sees. It's trying to use your variable effectTile instead of your function effectTile. Change the variable name to like et or something.
Also
stopEvent(effecTile)
lil typo there ;)

looool that was so true!!

After doing that change, the stopEvent(variable_name) is not working... It is still being executed. How to stop all the events?
 
looool that was so true!!

After doing that change, the stopEvent(variable_name) is not working... It is still being executed. How to stop all the events?
can do it like this
Code:
local aids = {
    [8760] = {8761, 19},
    [8761] = {8762, 20},
    [8762] = {8763, 22},
    [8763] = {8760, nil}
}

local function effectTile(position, n)
    local ground = Tile(position):getGround()
    if n <= 4 then
        local aid = aids[ground.actionid]
        ground:setAttribute('actionid', aid[1])
        position:sendMagicEffect(aid[2])
        addEvent(effectTile, 1000 * 2, position, n+1)
    end
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if not player then
        return true
    end
    effectTile(position, 1)
    return true
end
n is number of times recursed
each time it recurses, 1 is added to n
it will only recurse if n is <= 4, so once it goes through 4 effects and 4 action ids it will stop because the addEvent cannot be executed since n would be 5, thus stopping the recursion
 
Back
Top