The timer created by createTimer does not seem to be accurate enough?

Options
I am developing a plugin, the timer created by createTimer does not seem to be accurate enough. For example, the time interval I pass in is 40ms, but the time interval between the two callbacks I try to print is about 45~47ms.


Answers

  • Meep
    Meep ✭✭✭
    edited September 2022
    Options

    This is intended, internally they are using System.Threading.Timer for this. It happens even if you create your own timers on a fresh PoolFiber (albeit to a lesser extent). There is always going to be some time lost in-between timers. Especially inside of the plugin whereby timer actions are put onto the same fiber which all of the game operations are being enqueued on. The chances for differing deltas will increase depending on if the current operation being processed in the sequence has a lot of server logic or not. However, it tries its best to be as close as possible and it's a small sacrifice to make for thread safety.

    Timers are a good way of executing some code and delaying execution. Their delta times stay largely consistent even with thousands of messages being exchanged in a single room. However, you should not use them for pinpoint precision invocations if possible.