Jump to content

About timers and getTickCount


redditing

Recommended Posts

  • Moderators
8 hours ago, redditing said:

1. Are the server-side and client-side timers dangerous and why?

Timers in general can become never ending, if not programmed correctly. That is why you should use them with care.

It is not as if timers are dangerous by themself. But when they become never ending, there is a risk that your code might create a new infinity timer with the same task.

Which leads up to creating a memory leak in the form of infinity amount of timers. Each timer uses CPU and memory.

But that ain't the biggest issue. The tasks/functions attached to the timers are also becoming infinity.

So for example if the task is `setElementData(vehicle, "fuel", <some value>)`, this function is using bandwidth (and CPU) for all clients/players. When this becomes infinity, there is no bandwidth for other things and a network error occurs. (server will die at a given moment if it continues for too long)

 

8 hours ago, redditing said:

2. Is it possible to do the function GetTickCount () timer?

getTickCount is something passive, you need to combine that with an timer/onClientRender, to make it active.

 

Active (timer + getTickCount)

https://gitlab.com/IIYAMA12/draw-distance/-/blob/master/scripts/timer_c.lua

 

Passive (getTickCount)

https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

 

 

  • Thanks 1
Link to comment
11 minutes ago, IIYAMA said:

Timers in general can become never ending, if not programmed correctly. That is why you should use them with care.

It is not as if timers are dangerous by themself. But when they become never ending, there is a risk that your code might create a new infinity timer with the same task.

Which leads up to creating a memory leak in the form of infinity amount of timers. Each timer uses CPU and memory.

But that ain't the biggest issue. The tasks/functions attached to the timers are also becoming infinity.

So for example if the task is `setElementData(vehicle, "fuel", <some value>)`, this function is using bandwidth (and CPU) for all clients/players. When this becomes infinity, there is no bandwidth for other things and a network error occurs. (server will die at a given moment if it continues for too long)

 

getTickCount is something passive, you need to combine that with an timer/onClientRender, to make it active.

 

Active (timer + getTickCount)

https://gitlab.com/IIYAMA12/draw-distance/-/blob/master/scripts/timer_c.lua

 

Passive (getTickCount)

https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

 

 

And I would also like to know why setElementData is dangerous and can it be replaced somehow with e.g. a table?

Link to comment
  • Moderators
34 minutes ago, redditing said:

And I would also like to know why setElementData is dangerous and can it be replaced somehow with e.g. a table?

Clientside

The dangerous part of elementdata is that the programmer might forget that each time it is set (on clientside), it will synchronize with the server and after that it will also synchronize with all other players.

The following code is in my opinion to be considered dangerous:

addEventHandler("onClientRender", root, 
function () 
	setElementData(localPlayer, "test", getTickCount())  
end)

No synchronization steps are skipped (even if the same is the same), therefore if can stack up until no other messages can be send and network error occurs after the buffer is filled for too long.

 

Same goes for serverside:

setTimer(function () 
	setElementData(root, "test", getTickCount())  
end, 50, 0)

 

---------

 

Elementdata are be replace-able by tables, that will excluding the synchronization part as well as the event system.

You can also disable synchronization:

setElementData(root, "test", getTickCount(), false) -- false = disable

 

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...