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

addEvent & stopEvent

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
74
hi everyone!

just wondering if there is a way to check whether addEvent has been initialized and is currently running, example:

LUA:
if (test) then -- "test" is not a boolean so I cannot do that :(
    stopEvent(test)
end

local test = addEvent(
       function()
       -- my function goes here
       end, 15000)
 
Solution
no way, I can't do that since the function can be executed by multiple players at once.

I just did this on start and hopefully that won't crash my server

LUA:
   stopEvent(eventId)
Another way to handle this is to use a global table but instead of resetting all the event id's you just stop the ones which were assigned by the creature who executed them.
LUA:
-- same as before
test = {} -- defined in global.lua

-- defined inside the script
-- get the creature's id and store it in cid
local cid = creature:getId()
-- if the cid of the creature of test's index is not a table
if type(test[cid]) ~= 'table' then
    -- add one
    test[cid] = {}
end
-- see if test[cid] holds any values
if next(test[cid]) then
    -- cycle through all event...
the way you have is correct, it doesn't have to be a boolean, it returns the even id like complete blank said
all you need is that id
if there's no id the event is not running, that's why what you put should work just fine
 
ok thanks everyone.

by mistake I forgot to change the title as it was from my draft :oops:

--
nope that if didnt work
 
Last edited:
Not sure what distribution you're using, but I noticed addEvent/stopEvent only works in the same scriptinterface (at least on OTServ). IDK about tfs though, but you could, if it's in the same interface, use global storage value to set and check event id
 
You can use anything global to set the event id to and check it later, either use a global table or as @ond suggested you can use a global storage.

As a global table
LUA:
test = {} -- defined in global.lua

-- see if test holds any values
if next(test) then
    -- cycle through all event id's
    for i = 1, #test do
        -- and stop them
        stopEvent(test[i])
        -- write over the id's
        test[i] = nil
    end
end

-- insert an event id everytime this addEvent executes
table.insert(test, addEvent(
       function()
       -- my function goes here
       end, 15000)
       )

As a global storage
LUA:
local test = 123456

-- check to see if the event id has been set
local evenId = Game.getStorageValue(test)
if eventId > 0 then
    stopEvent(eventId)
    Game.setStorageValue(test, -1) 
end

-- set the event id to the global storage
Game.setStorageValue(test, addEvent(
       function()
       -- my function goes here
       end, 15000)
       )
Both methods have their plus and minuses but in the end the best method is the one you write yourself.
 
Last edited:
You can use anything global to set the event id to and check it later, either use a global table or as @ond suggested you can use a global storage.

As a global table
LUA:
test = {} -- defined in global.lua

-- see if test holds any values
if next(test) then
    -- cycle through all event id's
    for i = 1, #test do
        -- and stop them
        stopEvent(test[i])
        -- write over the id's
        test[i] = nil
    end
end

-- insert an event id everytime this addEvent executes
table.insert(test, addEvent(
       function()
       -- my function goes here
       end, 15000)
       )

As a global storage
LUA:
local test = 123456

-- check to see if the event id has been set
local evenId = Game.getStorageValue(test)
if eventId > 0 then
    stopEvent(eventId)
    Game.setStorageValue(test, -1)
end

-- set the event id to the global storage
Game.setStorageValue(test, addEvent(
       function()
       -- my function goes here
       end, 15000)
       )
Both methods have their plus and minuses but in the end the best method is the one you write yourself.

no way, I can't do that since the function can be executed by multiple players at once.

I just did this on start and hopefully that won't crash my server

LUA:
   stopEvent(eventId)
 
no way, I can't do that since the function can be executed by multiple players at once.

I just did this on start and hopefully that won't crash my server

LUA:
   stopEvent(eventId)
Another way to handle this is to use a global table but instead of resetting all the event id's you just stop the ones which were assigned by the creature who executed them.
LUA:
-- same as before
test = {} -- defined in global.lua

-- defined inside the script
-- get the creature's id and store it in cid
local cid = creature:getId()
-- if the cid of the creature of test's index is not a table
if type(test[cid]) ~= 'table' then
    -- add one
    test[cid] = {}
end
-- see if test[cid] holds any values
if next(test[cid]) then
    -- cycle through all event id's
    for i = 1, #test[cid] do
        -- and stop them
        stopEvent(test[cid][i])
        -- write over the id's
        test[cid][i] = nil
    end
end
-- insert an event id everytime this addEvent executes
table.insert(test[cid], addEvent(
       function()
       -- my function goes here
       end, 15000)
       )
 
Solution
Another way to handle this is to use a global table but instead of resetting all the event id's you just stop the ones which were assigned by the creature who executed them.
LUA:
-- same as before
test = {} -- defined in global.lua

-- defined inside the script
-- get the creature's id and store it in cid
local cid = creature:getId()
-- if the cid of the creature of test's index is not a table
if type(test[cid]) ~= 'table' then
    -- add one
    test[cid] = {}
end
-- see if test[cid] holds any values
if next(test[cid]) then
    -- cycle through all event id's
    for i = 1, #test[cid] do
        -- and stop them
        stopEvent(test[cid][i])
        -- write over the id's
        test[cid][i] = nil
    end
end
-- insert an event id everytime this addEvent executes
table.insert(test[cid], addEvent(
       function()
       -- my function goes here
       end, 15000)
       )

I like this
 
Back
Top