Jump to content

DoPeriodicTask vs DoTaskInTime


Recommended Posts

I have two different components that I activate one after the other.
One uses DoTaskInTime to schedule a task for 30 seconds from now.
The other uses DoPeriodicTask to schedule a task to run every 0.1 seconds over a total of 30 seconds.
For some reason, the second task finishes all its iterations in 20 seconds instead of 30 like the first one.

The periodic task increases a number value in every iteration, from 0 to 1, in increments of 0.1 / 30.
When it reaches 1 it stops.
This should take 300 iterations, so with an interval of 0.1 it should take 30 seconds in total.

Even if the calculations in my code are wrong, it doesn't change the fact that the periodic task occurs too quickly.
I put debug printouts right before starting the periodic task, printing the interval, and in the tick function printing the progress.
The server log shows 15 prints per second, 1.5 times faster than it should be (which corresponds to the total time being 2/3 of what it should be).

If I increase the interval to 0.5 the total time for the periodic task increases to about 27 seconds.
I really have no idea what's going on here and how to fix it.

Here's the code for the component (not much here, really):


-- Fake component for enabling usage of the recharge
-- item slot animation from The Forge
local Rechargeable = Class(function(self, inst)
    self.inst = inst
	self.percent = 1
	self.recharge_time = 0
	self.interval = 0
end)


local function do_recharge(inst, self)
	self:SetPercent(math.min(1, self:GetPercent() + self.interval / self:GetRechargeTime()))
	print("[Mod] Percentage: " .. tostring(self:GetPercent()))
	if self:GetPercent() == 1 then
		self:StopRecharging()
	end
end


function Rechargeable:GetPercent()
	return self.percent
end

function Rechargeable:GetRechargeTime()
	return self.recharge_time
end

function Rechargeable:SetPercent(percent)
	self.percent = percent
	self.inst:PushEvent("rechargechange", { percent = self.percent })
end

function Rechargeable:SetInterval(interval)
	self.interval = interval
end

function Rechargeable:SetRechargeTime(recharge_time)
	self.recharge_time = recharge_time
	self.inst:PushEvent("rechargetimechange", { t = self.recharge_time })
end

function Rechargeable:StartRecharging(starting_percentage)
	if starting_percentage then
		self:SetPercent(starting_percentage)
	end
	
	print("[Mod] Starting percentage: " .. tostring(self:GetPercent()) .. " ; Inteval: " .. tostring(self.interval))
	if self.recharge_task == nil then
		self.recharge_task = self.inst:DoPeriodicTask(self.interval, do_recharge, nil, self)
	end
end

function Rechargeable:StopRecharging()
	if self.recharge_task ~= nil then
		self.recharge_task:Cancel()
		self.recharge_task = nil
	end
end

return Rechargeable

 

And here is the relevant debug output:

Spoiler

[00:38:00]: [Mod] Starting percentage: 0 ; Inteval: 0.1    
[00:38:00]: [Mod] Percentage: 0.0033333333333333    
[00:38:00]: [Mod] Percentage: 0.0066666666666667    
[00:38:00]: [Mod] Percentage: 0.01    
[00:38:00]: [Mod] Percentage: 0.013333333333333    
[00:38:00]: [Mod] Percentage: 0.016666666666667    
[00:38:00]: [Mod] Percentage: 0.02    
[00:38:00]: [Mod] Percentage: 0.023333333333333    
[00:38:00]: [Mod] Percentage: 0.026666666666667    
[00:38:00]: [Mod] Percentage: 0.03    
[00:38:00]: [Mod] Percentage: 0.033333333333333    
[00:38:01]: [Mod] Percentage: 0.036666666666667    
[00:38:01]: [Mod] Percentage: 0.04    
[00:38:01]: [Mod] Percentage: 0.043333333333333    
[00:38:01]: [Mod] Percentage: 0.046666666666667    
[00:38:01]: [Mod] Percentage: 0.05    
[00:38:01]: [Mod] Percentage: 0.053333333333333    
[00:38:01]: [Mod] Percentage: 0.056666666666667    
[00:38:01]: [Mod] Percentage: 0.06    
[00:38:01]: [Mod] Percentage: 0.063333333333333    
[00:38:01]: [Mod] Percentage: 0.066666666666667    
[00:38:01]: [Mod] Percentage: 0.07    
[00:38:01]: [Mod] Percentage: 0.073333333333333    
[00:38:01]: [Mod] Percentage: 0.076666666666667    
[00:38:01]: [Mod] Percentage: 0.08    
[00:38:01]: [Mod] Percentage: 0.083333333333333    
[00:38:02]: [Mod] Percentage: 0.086666666666667    
[00:38:02]: [Mod] Percentage: 0.09    
[00:38:02]: [Mod] Percentage: 0.093333333333333    
[00:38:02]: [Mod] Percentage: 0.096666666666667    
[00:38:02]: [Mod] Percentage: 0.1    
[00:38:02]: [Mod] Percentage: 0.10333333333333    
[00:38:02]: [Mod] Percentage: 0.10666666666667    
[00:38:02]: [Mod] Percentage: 0.11    
[00:38:02]: [Mod] Percentage: 0.11333333333333    
[00:38:02]: [Mod] Percentage: 0.11666666666667    
[00:38:02]: [Mod] Percentage: 0.12    
[00:38:02]: [Mod] Percentage: 0.12333333333333    
[00:38:02]: [Mod] Percentage: 0.12666666666667    
[00:38:02]: [Mod] Percentage: 0.13    
[00:38:02]: [Mod] Percentage: 0.13333333333333    
[00:38:03]: [Mod] Percentage: 0.13666666666667    
[00:38:03]: [Mod] Percentage: 0.14    
[00:38:03]: [Mod] Percentage: 0.14333333333333    
[00:38:03]: [Mod] Percentage: 0.14666666666667    
[00:38:03]: [Mod] Percentage: 0.15    
[00:38:03]: [Mod] Percentage: 0.15333333333333    
[00:38:03]: [Mod] Percentage: 0.15666666666667    
[00:38:03]: [Mod] Percentage: 0.16    
[00:38:03]: [Mod] Percentage: 0.16333333333333    
[00:38:03]: [Mod] Percentage: 0.16666666666667    
[00:38:03]: [Mod] Percentage: 0.17    
[00:38:03]: [Mod] Percentage: 0.17333333333333    
[00:38:03]: [Mod] Percentage: 0.17666666666667    
[00:38:03]: [Mod] Percentage: 0.18    
[00:38:03]: [Mod] Percentage: 0.18333333333333    
[00:38:04]: [Mod] Percentage: 0.18666666666667    
[00:38:04]: [Mod] Percentage: 0.19    
[00:38:04]: [Mod] Percentage: 0.19333333333333    
[00:38:04]: [Mod] Percentage: 0.19666666666667    
[00:38:04]: [Mod] Percentage: 0.2    
[00:38:04]: [Mod] Percentage: 0.20333333333333    
[00:38:04]: [Mod] Percentage: 0.20666666666667    
[00:38:04]: [Mod] Percentage: 0.21    
[00:38:04]: [Mod] Percentage: 0.21333333333333    
[00:38:04]: [Mod] Percentage: 0.21666666666667    
[00:38:04]: [Mod] Percentage: 0.22    
[00:38:04]: [Mod] Percentage: 0.22333333333333    
[00:38:04]: [Mod] Percentage: 0.22666666666667    
[00:38:04]: [Mod] Percentage: 0.23    
[00:38:04]: [Mod] Percentage: 0.23333333333333    
[00:38:05]: [Mod] Percentage: 0.23666666666667    
[00:38:05]: [Mod] Percentage: 0.24    
[00:38:05]: [Mod] Percentage: 0.24333333333333    
[00:38:05]: [Mod] Percentage: 0.24666666666667    
[00:38:05]: [Mod] Percentage: 0.25    
[00:38:05]: [Mod] Percentage: 0.25333333333333    
[00:38:05]: [Mod] Percentage: 0.25666666666667    
[00:38:05]: [Mod] Percentage: 0.26    
[00:38:05]: [Mod] Percentage: 0.26333333333333    
[00:38:05]: [Mod] Percentage: 0.26666666666667    
[00:38:05]: [Mod] Percentage: 0.27    
[00:38:05]: [Mod] Percentage: 0.27333333333333    
[00:38:05]: [Mod] Percentage: 0.27666666666667    
[00:38:05]: [Mod] Percentage: 0.28    
[00:38:05]: [Mod] Percentage: 0.28333333333333    
[00:38:06]: [Mod] Percentage: 0.28666666666667    
[00:38:06]: [Mod] Percentage: 0.29    
[00:38:06]: [Mod] Percentage: 0.29333333333333    
[00:38:06]: [Mod] Percentage: 0.29666666666667    
[00:38:06]: [Mod] Percentage: 0.3    
[00:38:06]: [Mod] Percentage: 0.30333333333333    
[00:38:06]: [Mod] Percentage: 0.30666666666667    
[00:38:06]: [Mod] Percentage: 0.31    
[00:38:06]: [Mod] Percentage: 0.31333333333333    
[00:38:06]: [Mod] Percentage: 0.31666666666667    
[00:38:06]: [Mod] Percentage: 0.32    
[00:38:06]: [Mod] Percentage: 0.32333333333333    
[00:38:06]: [Mod] Percentage: 0.32666666666667    
[00:38:06]: [Mod] Percentage: 0.33    
[00:38:06]: [Mod] Percentage: 0.33333333333333    
[00:38:07]: [Mod] Percentage: 0.33666666666667    
[00:38:07]: [Mod] Percentage: 0.34    
[00:38:07]: [Mod] Percentage: 0.34333333333333    
[00:38:07]: [Mod] Percentage: 0.34666666666667    
[00:38:07]: [Mod] Percentage: 0.35    
[00:38:07]: [Mod] Percentage: 0.35333333333333    
[00:38:07]: [Mod] Percentage: 0.35666666666667    
[00:38:07]: [Mod] Percentage: 0.36    
[00:38:07]: [Mod] Percentage: 0.36333333333333    
[00:38:07]: [Mod] Percentage: 0.36666666666667    
[00:38:07]: [Mod] Percentage: 0.37    
[00:38:07]: [Mod] Percentage: 0.37333333333333    
[00:38:07]: [Mod] Percentage: 0.37666666666667    
[00:38:07]: [Mod] Percentage: 0.38    
[00:38:07]: [Mod] Percentage: 0.38333333333333    
[00:38:08]: [Mod] Percentage: 0.38666666666667    
[00:38:08]: [Mod] Percentage: 0.39    
[00:38:08]: [Mod] Percentage: 0.39333333333333    
[00:38:08]: [Mod] Percentage: 0.39666666666667    
[00:38:08]: [Mod] Percentage: 0.4    
[00:38:08]: [Mod] Percentage: 0.40333333333333    
[00:38:08]: [Mod] Percentage: 0.40666666666667    
[00:38:08]: [Mod] Percentage: 0.41    
[00:38:08]: [Mod] Percentage: 0.41333333333333    
[00:38:08]: [Mod] Percentage: 0.41666666666667    
[00:38:08]: [Mod] Percentage: 0.42    
[00:38:08]: [Mod] Percentage: 0.42333333333333    
[00:38:08]: [Mod] Percentage: 0.42666666666667    
[00:38:08]: [Mod] Percentage: 0.43    
[00:38:08]: [Mod] Percentage: 0.43333333333333    
[00:38:09]: [Mod] Percentage: 0.43666666666667    
[00:38:09]: [Mod] Percentage: 0.44    
[00:38:09]: [Mod] Percentage: 0.44333333333333    
[00:38:09]: [Mod] Percentage: 0.44666666666667    
[00:38:09]: [Mod] Percentage: 0.45    
[00:38:09]: [Mod] Percentage: 0.45333333333333    
[00:38:09]: [Mod] Percentage: 0.45666666666667    
[00:38:09]: [Mod] Percentage: 0.46    
[00:38:09]: [Mod] Percentage: 0.46333333333333    
[00:38:09]: [Mod] Percentage: 0.46666666666667    
[00:38:09]: [Mod] Percentage: 0.47    
[00:38:09]: [Mod] Percentage: 0.47333333333333    
[00:38:09]: [Mod] Percentage: 0.47666666666667    
[00:38:09]: [Mod] Percentage: 0.48    
[00:38:09]: [Mod] Percentage: 0.48333333333333    
[00:38:10]: [Mod] Percentage: 0.48666666666667    
[00:38:10]: [Mod] Percentage: 0.49    
[00:38:10]: [Mod] Percentage: 0.49333333333333    
[00:38:10]: [Mod] Percentage: 0.49666666666667    
[00:38:10]: [Mod] Percentage: 0.5    
[00:38:10]: [Mod] Percentage: 0.50333333333333    
[00:38:10]: [Mod] Percentage: 0.50666666666667    
[00:38:10]: [Mod] Percentage: 0.51    
[00:38:10]: [Mod] Percentage: 0.51333333333333    
[00:38:10]: [Mod] Percentage: 0.51666666666667    
[00:38:10]: [Mod] Percentage: 0.52    
[00:38:10]: [Mod] Percentage: 0.52333333333333    
[00:38:10]: [Mod] Percentage: 0.52666666666667    
[00:38:10]: [Mod] Percentage: 0.53    
[00:38:10]: [Mod] Percentage: 0.53333333333333    
[00:38:11]: [Mod] Percentage: 0.53666666666667    
[00:38:11]: [Mod] Percentage: 0.54    
[00:38:11]: [Mod] Percentage: 0.54333333333333    
[00:38:11]: [Mod] Percentage: 0.54666666666667    
[00:38:11]: [Mod] Percentage: 0.55    
[00:38:11]: [Mod] Percentage: 0.55333333333333    
[00:38:11]: [Mod] Percentage: 0.55666666666667    
[00:38:11]: [Mod] Percentage: 0.56    
[00:38:11]: [Mod] Percentage: 0.56333333333333    
[00:38:11]: [Mod] Percentage: 0.56666666666667    
[00:38:11]: [Mod] Percentage: 0.57    
[00:38:11]: [Mod] Percentage: 0.57333333333333    
[00:38:11]: [Mod] Percentage: 0.57666666666667    
[00:38:11]: [Mod] Percentage: 0.58    
[00:38:11]: [Mod] Percentage: 0.58333333333333    
[00:38:12]: [Mod] Percentage: 0.58666666666667    
[00:38:12]: [Mod] Percentage: 0.59    
[00:38:12]: [Mod] Percentage: 0.59333333333333    
[00:38:12]: [Mod] Percentage: 0.59666666666667    
[00:38:12]: [Mod] Percentage: 0.6    
[00:38:12]: [Mod] Percentage: 0.60333333333333    
[00:38:12]: [Mod] Percentage: 0.60666666666667    
[00:38:12]: [Mod] Percentage: 0.61    
[00:38:12]: [Mod] Percentage: 0.61333333333333    
[00:38:12]: [Mod] Percentage: 0.61666666666667    
[00:38:12]: [Mod] Percentage: 0.62    
[00:38:12]: [Mod] Percentage: 0.62333333333333    
[00:38:12]: [Mod] Percentage: 0.62666666666667    
[00:38:12]: [Mod] Percentage: 0.63    
[00:38:12]: [Mod] Percentage: 0.63333333333333    
[00:38:13]: [Mod] Percentage: 0.63666666666667    
[00:38:13]: [Mod] Percentage: 0.64    
[00:38:13]: [Mod] Percentage: 0.64333333333333    
[00:38:13]: [Mod] Percentage: 0.64666666666667    
[00:38:13]: [Mod] Percentage: 0.65    
[00:38:13]: [Mod] Percentage: 0.65333333333333    
[00:38:13]: [Mod] Percentage: 0.65666666666667    
[00:38:13]: [Mod] Percentage: 0.66    
[00:38:13]: [Mod] Percentage: 0.66333333333333    
[00:38:13]: [Mod] Percentage: 0.66666666666667    
[00:38:13]: [Mod] Percentage: 0.67    
[00:38:13]: [Mod] Percentage: 0.67333333333333    
[00:38:13]: [Mod] Percentage: 0.67666666666667    
[00:38:13]: [Mod] Percentage: 0.68    
[00:38:13]: [Mod] Percentage: 0.68333333333333    
[00:38:14]: [Mod] Percentage: 0.68666666666667    
[00:38:14]: [Mod] Percentage: 0.69    
[00:38:14]: [Mod] Percentage: 0.69333333333333    
[00:38:14]: [Mod] Percentage: 0.69666666666667    
[00:38:14]: [Mod] Percentage: 0.7    
[00:38:14]: [Mod] Percentage: 0.70333333333333    
[00:38:14]: [Mod] Percentage: 0.70666666666667    
[00:38:14]: [Mod] Percentage: 0.71    
[00:38:14]: [Mod] Percentage: 0.71333333333333    
[00:38:14]: [Mod] Percentage: 0.71666666666667    
[00:38:14]: [Mod] Percentage: 0.72    
[00:38:14]: [Mod] Percentage: 0.72333333333333    
[00:38:14]: [Mod] Percentage: 0.72666666666667    
[00:38:14]: [Mod] Percentage: 0.73    
[00:38:14]: [Mod] Percentage: 0.73333333333333    
[00:38:15]: [Mod] Percentage: 0.73666666666667    
[00:38:15]: [Mod] Percentage: 0.74    
[00:38:15]: [Mod] Percentage: 0.74333333333333    
[00:38:15]: [Mod] Percentage: 0.74666666666667    
[00:38:15]: [Mod] Percentage: 0.75    
[00:38:15]: [Mod] Percentage: 0.75333333333333    
[00:38:15]: [Mod] Percentage: 0.75666666666667    
[00:38:15]: [Mod] Percentage: 0.76    
[00:38:15]: [Mod] Percentage: 0.76333333333333    
[00:38:15]: [Mod] Percentage: 0.76666666666667    
[00:38:15]: [Mod] Percentage: 0.77    
[00:38:15]: [Mod] Percentage: 0.77333333333333    
[00:38:15]: [Mod] Percentage: 0.77666666666667    
[00:38:15]: [Mod] Percentage: 0.78    
[00:38:15]: [Mod] Percentage: 0.78333333333333    
[00:38:16]: [Mod] Percentage: 0.78666666666666    
[00:38:16]: [Mod] Percentage: 0.79    
[00:38:16]: [Mod] Percentage: 0.79333333333333    
[00:38:16]: [Mod] Percentage: 0.79666666666666    
[00:38:16]: [Mod] Percentage: 0.8    
[00:38:16]: [Mod] Percentage: 0.80333333333333    
[00:38:16]: [Mod] Percentage: 0.80666666666666    
[00:38:16]: [Mod] Percentage: 0.81    
[00:38:16]: [Mod] Percentage: 0.81333333333333    
[00:38:16]: [Mod] Percentage: 0.81666666666666    
[00:38:16]: [Mod] Percentage: 0.82    
[00:38:16]: [Mod] Percentage: 0.82333333333333    
[00:38:16]: [Mod] Percentage: 0.82666666666666    
[00:38:16]: [Mod] Percentage: 0.83    
[00:38:16]: [Mod] Percentage: 0.83333333333333    
[00:38:17]: [Mod] Percentage: 0.83666666666666    
[00:38:17]: [Mod] Percentage: 0.84    
[00:38:17]: [Mod] Percentage: 0.84333333333333    
[00:38:17]: [Mod] Percentage: 0.84666666666666    
[00:38:17]: [Mod] Percentage: 0.85    
[00:38:17]: [Mod] Percentage: 0.85333333333333    
[00:38:17]: [Mod] Percentage: 0.85666666666666    
[00:38:17]: [Mod] Percentage: 0.86    
[00:38:17]: [Mod] Percentage: 0.86333333333333    
[00:38:17]: [Mod] Percentage: 0.86666666666666    
[00:38:17]: [Mod] Percentage: 0.87    
[00:38:17]: [Mod] Percentage: 0.87333333333333    
[00:38:17]: [Mod] Percentage: 0.87666666666666    
[00:38:17]: [Mod] Percentage: 0.88    
[00:38:17]: [Mod] Percentage: 0.88333333333333    
[00:38:18]: [Mod] Percentage: 0.88666666666666    
[00:38:18]: [Mod] Percentage: 0.89    
[00:38:18]: [Mod] Percentage: 0.89333333333333    
[00:38:18]: [Mod] Percentage: 0.89666666666666    
[00:38:18]: [Mod] Percentage: 0.9    
[00:38:18]: [Mod] Percentage: 0.90333333333333    
[00:38:18]: [Mod] Percentage: 0.90666666666666    
[00:38:18]: [Mod] Percentage: 0.91    
[00:38:18]: [Mod] Percentage: 0.91333333333333    
[00:38:18]: [Mod] Percentage: 0.91666666666666    
[00:38:18]: [Mod] Percentage: 0.92    
[00:38:18]: [Mod] Percentage: 0.92333333333333    
[00:38:18]: [Mod] Percentage: 0.92666666666666    
[00:38:18]: [Mod] Percentage: 0.93    
[00:38:18]: [Mod] Percentage: 0.93333333333333    
[00:38:19]: [Mod] Percentage: 0.93666666666666    
[00:38:19]: [Mod] Percentage: 0.94    
[00:38:19]: [Mod] Percentage: 0.94333333333333    
[00:38:19]: [Mod] Percentage: 0.94666666666666    
[00:38:19]: [Mod] Percentage: 0.95    
[00:38:19]: [Mod] Percentage: 0.95333333333333    
[00:38:19]: [Mod] Percentage: 0.95666666666666    
[00:38:19]: [Mod] Percentage: 0.96    
[00:38:19]: [Mod] Percentage: 0.96333333333333    
[00:38:19]: [Mod] Percentage: 0.96666666666666    
[00:38:19]: [Mod] Percentage: 0.97    
[00:38:19]: [Mod] Percentage: 0.97333333333333    
[00:38:19]: [Mod] Percentage: 0.97666666666666    
[00:38:19]: [Mod] Percentage: 0.98    
[00:38:19]: [Mod] Percentage: 0.98333333333333    
[00:38:20]: [Mod] Percentage: 0.98666666666666    
[00:38:20]: [Mod] Percentage: 0.99    
[00:38:20]: [Mod] Percentage: 0.99333333333333    
[00:38:20]: [Mod] Percentage: 0.99666666666666    
[00:38:20]: [Mod] Percentage: 1    
[00:38:20]: [Mod] Percentage: 1    

Like I said, 15 prints per second.

What's the problem here and how do I make DoPeriodicTask work like I expect?
I would have suspected server load but it's working faster than it should.

Thanks in advance.

Link to comment
Share on other sites

I'm pretty sure I read in a thread or a piece of game-code somewhere that the developers had also grown annoyed by DoPeriodicTask being inaccurate, so they started using something else. Can't remember what.

I believe the problem may stem from something like DoPeriodicTask not transferring the overshoot from one call to the next or something.

Example with 0.4 seconds elapsed per frame, just for clarity:
DoPeriodicTask(1.0, blabla)

0.4 -- frame 1
0.8 -- frame 2
1.2 -- frame 3; trigger, reset counter
0.4 -- frame 4; should be 0.6
0.8 -- frame 5
...

Not sure, though. This is an interesting read in an older change log where CarlZalph delivers some insight.

Link to comment
Share on other sites

DoPeriodicTask() and DoTaskInTime() have made of Lua coroutine. So basically, these functions also have Lua coroutine's property.
But they are hard-coded to run its tasks on each specific time. I'll say this as 'game tick.' All entities derived by TheSim uses this game tick which is 30tps. (1 game tick) = FRAMES = 0.0333... = 1/30

Note that we cannot figure out the exact reason why it actually needs to be 30fps as the source code of the game engine is not public. But if the game script is running without the game tick, any animation-related will not sync which means it cannot be controlled as you can see, though.

A function DoPeriodicTask() is defined in entityscript.lua.

local per = scheduler:ExecutePeriodic(time, fn, nil, initialdelay, self.GUID, self, ...)

And the issue is related to this line. In scheduler.lua

function Scheduler:ExecutePeriodic(period, fn, limit, initialdelay, id, ...)
    local list, nexttick = self:GetListForTimeFromNow(initialdelay or period)
    local periodic = Periodic(fn, period, limit, id, nexttick, ...)
    list[periodic] = true
    periodic.list = list
    return periodic
end

and self:GetListForTimeFromNow() decides when to start next thread.

function Scheduler:GetListForTimeFromNow(dt)
    local nowtick = GetTick()
    local wakeuptick = math.floor( (GetTime()+dt)/GetTickTime() )
    if wakeuptick <= nowtick then
        wakeuptick = nowtick+1
    end

    local list = scheduler.attime[wakeuptick]
    if not list then
        list = {}
        scheduler.attime[wakeuptick] = list
    end
    return list, wakeuptick
end

As you can see, 

local wakeuptick = math.floor( (GetTime()+dt)/GetTickTime() )

wakeuptick is divided and rounded off by GetTickTime(). So, this engrafts every thread into the game tick. 
This also means the minimum value of time(interval) argument is FRAMES.

if wakeuptick <= nowtick then
  wakeuptick = nowtick+1
end

And if wakeuptick is smaller than nowtick(when you set time argument lower than FRAMES or negative number), it will override to just next game tick.

 

But, if you print GetTickTime() in runtime, it is actually bigger than 1/30. That's because the server iterates other things, not only just counting the tick. So what the server just have done is, repeat the thread every 2 FRAMES, not 3. yeah, 0.333333... * 3 is actually bigger than 1 in real life.
So I think the best way to sync DoPeriodicTime() is to add a constant that is suitably lower than FRAMES.

Also, I'll say, just in case, you can implement your code with StartThread() and Sleep().

Link to comment
Share on other sites

Yeah, don't rely on the internal ticks to be consistent.

Spoiler

 

What you could be doing is to:

1) Record the timestamp for when the cooldown starts and add an offset for when the thing should go off of cooldown. (currenttime + 5seconds)

2) In the periodic timer to see how much charge is done, do (endtime - currenttime) and see if it's positive, if so then the cooldown is still in effect otherwise it is no longer in effect.

3) Use a single timer to fire off for the end of the duration to denote that the thing on cooldown is no longer on cooldown.

 

Since things can happen on the server and it can choke, you can't be guaranteed a constant tick rate with time.

If you really want 300 intervals, then calculate out how many intervals it should be and then run your code multiple times in a tick to catch up if need be.  They won't be spaced out properly in time, but when you're working with an unreliable timer to begin with it's not really possible to do perfectly.

Link to comment
Share on other sites

Thanks everyone.

When I saw that the timing was off I decided to just let the periodic task run whenever it wants to as long as it's fast enough (no problem there) and do the logic of progress and ending using starting time, total duration and current time, which is pretty much what @CarlZalph said.

I'll manage the rest around that.

Edited by Clopen
Link to comment
Share on other sites

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
 Share

×
  • Create New...