Use addEvent properly with loops (+ XTRA FUNCTION)
Hello...
I've seen quite a lots of scripts looping addEvent in a bad way. This will most likely cause your server to lag!
Have you ever noticed that you made a spell with over 30 "animated" effects that will lag your server? Well, one reason could be that you have looped addEvent for this spell. Not just spells but anything using addEvent! I'll show you what I mean.
Bad:
Why ? Because now you have registered 40 events, and imagine if this script was executed 10 times at once (e.g. a spell that is casted by 10 players at once) that would result in 400 active events!!
400 !!!
The good way, which is used by people as well but minority indeed:
Now this will only create 1 event each, and when that event is over it will run another event until it has ran 40 times! This way you dont have 40 active events at once, waiting to be executed. You simply have just one single event calling another event.
Conclusion: Instead of having 40 active events for 4 seconds you will have 1 event each 100ms for 4 minutes!
Hope you understood my point
I will give you some functions I just made, to make these functions easier to make! You won't need to think too much First you will need to add these functions to functions.lua:
Then here is example of usage (talkaction):
So all you have to do is to write continue(delay, repeatTimes, parameters) ! You don't need to write the function name, it will know that itself! You don't need any counter in your function, it will count itself!
Thanks,
Colandus
Hello...
I've seen quite a lots of scripts looping addEvent in a bad way. This will most likely cause your server to lag!
Have you ever noticed that you made a spell with over 30 "animated" effects that will lag your server? Well, one reason could be that you have looped addEvent for this spell. Not just spells but anything using addEvent! I'll show you what I mean.
Bad:
Lua:
function doThatShit()
print("Helo Vorld")
end
for i = 1, 40 do
addEvent(doThatShit, i * 100)
end
Why ? Because now you have registered 40 events, and imagine if this script was executed 10 times at once (e.g. a spell that is casted by 10 players at once) that would result in 400 active events!!
400 !!!
The good way, which is used by people as well but minority indeed:
Lua:
function doThatShit(n)
print("Helo Vorld")
if(n > 0) then
addEvent(doThatShit, 100, n - 1)
end
end
addEvent(doThatShit, i * 100, 40)
Now this will only create 1 event each, and when that event is over it will run another event until it has ran 40 times! This way you dont have 40 active events at once, waiting to be executed. You simply have just one single event calling another event.
Conclusion: Instead of having 40 active events for 4 seconds you will have 1 event each 100ms for 4 minutes!
Hope you understood my point
I will give you some functions I just made, to make these functions easier to make! You won't need to think too much First you will need to add these functions to functions.lua:
Lua:
-----------------
-- By Colandus --
_CONT = {}
function continue(delay, n, ...)
local func = debug.getinfo(2).func
if(not _CONT[func]) then
_CONT[func] = true
addEvent(repeatContinue, delay, func, delay, n, ...)
end
end
function repeatContinue(func, delay, n, ...)
if(n > 1) then
func(...)
addEvent(repeatContinue, delay, func, delay, n - 1, ...)
else
_CONT[func] = nil
end
end
Then here is example of usage (talkaction):
Lua:
function doThatShit(cid)
if(not isCreature(cid)) then
return
end
doCreatureSay(cid, "HiHO", 1)
-- repeat 4 times (will only repeat 4 times because it count the current call as 1!) each second
continue(1000, 5, cid)
end
function onSay(cid, words, param, channel)
doThatShit(cid)
return true
end
So all you have to do is to write continue(delay, repeatTimes, parameters) ! You don't need to write the function name, it will know that itself! You don't need any counter in your function, it will count itself!
Thanks,
Colandus
Last edited: