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

Solved [TFS 1.2] How Far? (addEvent)

Ovnyx

Member
Joined
Jul 25, 2017
Messages
163
Solutions
2
Reaction score
7
hiho!
i was taking a look this rainbow outfit, posted here:
[How-to] Using addEvent()

then i was thiking if there is like 50-60 people online in a server and most of them are wearing this outfit, all this "cicles" (recursion) on each player wont generate a lot of lag? or how can i calculate the amount of running events at the same time that i can have?

i would appreciate if someone could clarify this. :)

Thanks in advice!
Ovnyx

PD: Make me know if this kind of posts are against the rules, Ty
 
Solution
you can't calculate how many events running at a time unless you save them all within a table and use the # operator on the table
but then that would take up a ton of memory so don't do it
anyways you very rarely ever need to worry about performance issues using lua, it's one of the fastest lightweight languages out there
the script won't lag your server
you can't calculate how many events running at a time unless you save them all within a table and use the # operator on the table
but then that would take up a ton of memory so don't do it
anyways you very rarely ever need to worry about performance issues using lua, it's one of the fastest lightweight languages out there
the script won't lag your server
 
Solution
Please note that creating an event within an event could cause the Lua stack to overflow, test it thoroughly or else, if it overflows, all scripts will likely stop working.
 
Please note that creating an event within an event could cause the Lua stack to overflow, test it thoroughly or else, if it overflows, all scripts will likely stop working.
hi,
thanks for this info, this happends when the escape condition of the recursion its incorrect?
 
hi,
thanks for this info, this happends when the escape condition of the recursion its incorrect?
I believe just calling enough times will cause a stack overflow because the information about the last "nested" call is stored on the stack (eventual parameters, address of return and returned values).
 
I believe just calling enough times will cause a stack overflow because the information about the last "nested" call is stored on the stack (eventual parameters, address of return and returned values).

Hmm interesting, because i was having that error, i was getting a stack overflow error with an event, but i fixed it, and the problem was that the scape condition was wrong.

im going to test deeper my script until im 100% sure that it is not going to overflow.

thanks for this info and your interest! :)
 
pretty you'd only get stack overflow if you were using addEvent with 0ms and recursion
nothing to really worry about, no one's dumb enough to use addEvent with that low of a delay in an infinite loop
 
pretty you'd only get stack overflow if you were using addEvent with 0ms and recursion
nothing to really worry about, no one's dumb enough to use addEvent with that low of a delay in an infinite loop

Hmmm i understand, well I never thought to do that, but now i know i cant do it :D
thank you a lot guys!
 
Recursion makes the code beauty and small, but has bad performance.
It can cause stack over flow by filling all the stack.
I mean, if you are using recursion where something never finishes (like the outfit, that will never finish unless the players logs out), it could cause stack over flow and crash your server.
Is nice to use recursion on things that finishes, for example, a spell effect that executes itself each 1 second for 10 seconds.

Also, about the addEvent, I think everything has a limit.
On Java language, the tables limit is int32_t, which means a limit of 4,294,967,296 elements.
I think that's the case on C++ also.
If that's the case, you will be able to store until 4,294,967,296 events (theoretically, since your hosting computer would have to support this huge amount of simultaneous events).

Another note, too fast events, like 50 ms each execution, is bad because each execution will happen a trade of protocol data between all players on the game screen depending of what you are doing.
Example, let's says you make a spell that removes 0.2% of health each 50ms until he dies, your server is in war, a lot of players on game screen and all are with this debuff effect.
That means we would need to send the health change of EACH PLAYER to EACH PLAYER'S CLIENT for EACH SPELL and EACH 50ms, etc. That's is NOT recommended (I tried do to a spell like that, and it freezes a lot). On TFS 1.2+, if the packet's limit comes on, the player will simply be kicked unless you increase the packet's limit on config.lua (but increase this it's not recommended, you should avoid several too quickly events).

A tip, is that if you are not using a event anymore, you should stop it, otherwise it will be there until the time to execution becomes.
Let's says you created some events on login that executes something in 1 hour and you don't need them anymore because they only executes if that player is online and your player is not online anymore.
I mean, the event checks if player is online on the execution's act, but does not removes it when he logs out.
Now, let's says a lot of players logs out and logs in several times.
Probably will have a lot of events that will do nothing because they are not online anymore, but is still counting to be executed in 1 hour.
The correct would be: remove the events on player logout.
How you do that: store the id of addEvent and use stopEvent for stop it.
Example (it works on all TFS releases):
-- Registering the event and storing it on a player's storage (probably on login, or where you want to register the event)
local hour = 1 * 60 * 60 * 1000
local myEventId = addEvent(myFunction, hour, myFirstFunctionsParam, mySecondFunctionsParam)
player:setStorageValue(7777, myEventId) -- (or setPlayerStorageValue(cid, 7777, myEventId) depending of your TFS version)
-- Stopping the event (probably on logout, or where you want to unregister the event)
local myEventId = player:getStorageValue(7777) -- (or getPlayerStorageValue(cid, 7777) depending of your TFS version)
if myEventId ~= -1 then
stopEvent(myEventId)
end

In resume, I recommend to use recursion for things that wont be executing for a long time, also avoid to use events that executes several times too quickly, and remember to be always careful to keep cleaning your unecessary events.
 
Last edited:
Back
Top