• 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 Use addEvent properly with loops

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
218
Location
Sweden
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:
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 :D 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:
Will update later with a function that will help you make easier "loops" :)

Edit: Updated ;)
 
Last edited:
Don't be shy, just say it's very useful for you. Because it is, even for me.
 
Thanks for this, will help with future tasks...

Question:
Where did you go Colandus?
All these months, you were gone, now your back?!

Welcome Back :thumbup:
 
@colandus.
I already know about this make one year ^^
I developed the first talking tps with this, 2 addEvents
And my botCleanner use it too, my otservLUAIRCbot use addEvent too ^^
This its really usefull
 
I'm not telling people "Look function addEvent!! is new HEH"... What I am telling them is to use it the PROPER way!

Also I provided a function to ease these stuff :p
 
@colandus
yes i know, i did not say that you said ._.
I am saying that already know this.
And this is VERRY.10¹³ unsefull in otservers.
 
The last code ? Doubt so :p

Also I'm sure you've used it, but I'm also pretty sure you used to bad way too. However, I don't care if you've used it or not, all I care about is that you know now that you're supposed to do it that way (even though you might have known it before).
 
The last code ? Doubt so :p

Also I'm sure you've used it, but I'm also pretty sure you used to bad way too. However, I don't care if you've used it or not, all I care about is that you know now that you're supposed to do it that way (even though you might have known it before).

I don't used the last way, but I used somethink simple like this before:
Lua:
local msgs = {
	{text = "Hi", time = 5000},
	{text = "Everyone", time = 10000}
}

function braodcastMsg(parameters)
	local i = parameters.i
	doBroadcastMessage(msgs[i].text, 22)
end

for i = 1, #msgs do
	addEvent(braodcastMsg[i], msgs.time[i], {i = i})
end

In my war server the map change script are working so, :thumbup:.
 
Now all you do is to show me an addEvent script, it has nothing to do with this tutorial :p

The script was about when you use addEvent wrong. This one only loop through 2 values so it won't make any big deal, unless there are 10+ texts xD
 
Now all you do is to show me an addEvent script, it has nothing to do with this tutorial :p

The script was about when you use addEvent wrong. This one only loop through 2 values so it won't make any big deal, unless there are 10+ texts xD

This was an example, you can easy add 100+ msgs.
 
If you add 100 messages then you certainly do not understand what this tutorial is about. You better read it properly not just check codes cuz you really don't get it. You do exactly what this tutorial tells you not to do!

The reason is that if you 100 messages it will create 100 addEvents. But we only need 1!
Lua:
local msgs = {
    {text = "Hi", time = 5000},
    {text = "Everyone", time = 6000},
    {text = "How", time = 7000},
    {text = "Are", time = 8000},
    {text = "You", time = 9000},
    {text = "Doing?", time = 10000}
}

function broadcastMsg(current)
    current = current or 0
    doBroadcastMessage(msgs[current].text, 22)

    local next = (current + 1) % #msgs
    addEvent(broadcastMsg, next.time, next)
end

broadcastMsg()

See there, now I did not make a loop for each message. My script would have called for only 1 single addEvent at once, while yours would call for as many as there are messages (with my messages table you would have had 6 active events, with a table with 100 messages you would have 100 active events).

That's what I told you earlier, that I was sure you didn't understand what this tutorial really is! That's why you read and not just check my codes.
 
Last edited:
If you add 100 messages then you certainly do not understand what this tutorial is about. You better read it properly not just check codes cuz you really don't get it. You do exactly what this tutorial tells you not to do!

The reason is that if you 100 messages it will create 100 addEvents. But we only need 1!
Lua:
local msgs = {
    {text = "Hi", time = 5000},
    {text = "Everyone", time = 6000},
    {text = "How", time = 7000},
    {text = "Are", time = 8000},
    {text = "You", time = 9000},
    {text = "Doing?", time = 10000}
}

function broadcastMsg(current)
    current = current or 0
    doBroadcastMessage(msgs[current].text, 22)

    local next = (current + 1) % #msgs
    addEvent(broadcastMsg, next.time, next)
end

broadcastMsg()

See there, now I did not make a loop for each message. My script would have called for only 1 single addEvent at once, while yours would call for as many as there are messages (with my messages table you would have had 6 active events, with a table with 100 messages you would have 100 active events).

That's what I told you earlier, that I was sure you didn't understand what this tutorial really is! That's why you read and not just check my codes.

Now I understand it, ;).
 
Back
Top