Jump to content

DoTaskInTime order


Recommended Posts

@Maris, tid bit of code you're probably wanting to study.

function EntityScript:DoTaskInTime(time, fn, ...)    --print ("DO TASK IN TIME", time, self)    if not self.pendingtasks then        self.pendingtasks = {}    end        local per = scheduler:ExecuteInTime(time, fn, self.GUID, self, ...)    self.pendingtasks[per] = true    per.onfinish = task_finish -- function() if self and self.pendingtasks then self.pendingtasks[per] = nil end end    return perendfunction Scheduler:ExecuteInTime(timefromnow, fn, id, ...)    return self:ExecutePeriodic(timefromnow, fn, 1, nil, id, ...)endfunction Scheduler:ExecutePeriodic(period, fn, limit, initialdelay, id, ...)    local periodic = Periodic(fn, period, limit, id, ...)    local list = self:GetListForTimeFromNow(initialdelay or period)    list[periodic] = true    periodic.list = list    return periodicendfunction 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 listendfunction Scheduler:Run()	for k, v in pairs(self.waking) do		v:SetList(self.running)	end	self.waking = {}	for k, v in pairs(self.running) do    		if coroutine.status(v.co) == "dead" then			--The task is finished. kill it!            task:SetList(nil)            self.tasks[v.co] = nil		else 			local success, yieldtype, yieldparam = coroutine.resume(v.co, v.param)			if success and coroutine.status(v.co) ~= "dead" then				if yieldtype == HIBERNATE then                    v:SetList(self.hibernating)                elseif yieldtype == SLEEP then                    yieldparam = math.floor(yieldparam)                    local list = self.waitingfortick[yieldparam]                    if not list then                        list = GetNewList()                        self.waitingfortick[yieldparam] = list                    end                    v:SetList(list)				end			else                v:SetList(nil)                v.retval = yieldtype                if not success then                    print (debug.traceback(v.co, "\nCOROUTINE "..tostring(v.id).." SCRIPT CRASH:\n".. tostring(yieldtype)))                    self:KillTask(v)                    return                end                self:KillTask(v)                			end		end	endend
Link to comment
Share on other sites

@Maris,

 

Input:

AddPrefabPostInit("wilson", function(inst)	print("Start!")	local function fn()		inst:DoTaskInTime(0, function() print(1) end)		inst:DoTaskInTime(0, function() print(2) end)		inst:DoTaskInTime(math.random(), fn)	end	inst:DoTaskInTime(0, fn)end)

Output:

[00:00:41]: Start![00:00:41]: 2	[00:00:41]: 1	[00:00:42]: 1	[00:00:42]: 2	[00:00:43]: 1	[00:00:43]: 2	[00:00:43]: 2	[00:00:43]: 1	[00:00:43]: 1	[00:00:43]: 2	[00:00:44]: 1	[00:00:44]: 2	[00:00:44]: 2	[00:00:44]: 1	[00:00:45]: 1	[00:00:45]: 2	[00:00:46]: 1	[00:00:46]: 2	[00:00:46]: 1	[00:00:46]: 2	[00:00:46]: 1	[00:00:46]: 2	[00:00:47]: 2	[00:00:47]: 1	[00:00:47]: 1	[00:00:47]: 2	[00:00:48]: 1	[00:00:48]: 2	[00:00:48]: 1	[00:00:48]: 2	[00:00:49]: 1	[00:00:49]: 2	[00:00:49]: 2	[00:00:49]: 1	[00:00:50]: 1	[00:00:50]: 2	[00:00:50]: 1	[00:00:50]: 2	[00:00:51]: 2	[00:00:51]: 1	[00:00:51]: 2	[00:00:51]: 1	[00:00:52]: 2	[00:00:52]: 1	[00:00:52]: 1	[00:00:52]: 2	[00:00:53]: 1	[00:00:53]: 2	[00:00:54]: 1	[00:00:54]: 2	[00:00:55]: 1	[00:00:55]: 2	[00:00:55]: 1	[00:00:55]: 2	[00:00:55]: 1	[00:00:55]: 2	[00:00:55]: 1	[00:00:55]: 2	[00:00:56]: 1	[00:00:56]: 2	[00:00:57]: 1	[00:00:57]: 2	[00:00:57]: 1	[00:00:57]: 2	[00:00:58]: 1	[00:00:58]: 2	[00:00:58]: 1	[00:00:58]: 2	[00:00:58]: 2	[00:00:58]: 1	[00:00:58]: 2	[00:00:58]: 1	[00:00:59]: 2	[00:00:59]: 1	[00:00:59]: 2	[00:00:59]: 1	[00:01:00]: 1	[00:01:00]: 2	[00:01:00]: 1	[00:01:00]: 2	[00:01:00]: 1	[00:01:00]: 2	[00:01:01]: 2	[00:01:01]: 1	[00:01:02]: 1	[00:01:02]: 2	[00:01:03]: 1	[00:01:03]: 2	[00:01:03]: 1	[00:01:03]: 2	[00:01:03]: 2	[00:01:03]: 1	[00:01:04]: 2	[00:01:04]: 1	[00:01:04]: 2	[00:01:04]: 1	[00:01:05]: 1	[00:01:05]: 2	[00:01:06]: 2	[00:01:06]: 1	[00:01:06]: 2	[00:01:06]: 1	[00:01:07]: 1	[00:01:07]: 2	[00:01:07]: 2	[00:01:07]: 1	[00:01:08]: 1	[00:01:08]: 2	[00:01:09]: 2	[00:01:09]: 1	[00:01:09]: 2	[00:01:09]: 1	[00:01:10]: 2	[00:01:10]: 1	[00:01:10]: 2	[00:01:10]: 1	[00:01:11]: 2	[00:01:11]: 1	[00:01:11]: 2	[00:01:11]: 1	[00:01:11]: 1	[00:01:11]: 2	[00:01:11]: 2	[00:01:11]: 1	[00:01:11]: 1	[00:01:11]: 2	[00:01:12]: 2	[00:01:12]: 1	[00:01:12]: 2	[00:01:12]: 1	[00:01:12]: 2	[00:01:12]: 1	[00:01:12]: 1	[00:01:12]: 2	[00:01:13]: 2	[00:01:13]: 1	[00:01:14]: 1	[00:01:14]: 2	[00:01:14]: 1	[00:01:14]: 2	[00:01:15]: 2	[00:01:15]: 1	[00:01:15]: 1	[00:01:15]: 2	[00:01:16]: 1	[00:01:16]: 2	[00:01:16]: 2	[00:01:16]: 1	[00:01:17]: 1	[00:01:17]: 2[00:01:17]: 1	[00:01:17]: 2

Link to comment
Share on other sites

@DarkXero, crucial pinpoint data. I simply removed the math.random() task because it is irrelevant and changed the last DoTaskInTime to DoPeriodicTask.

 

[00:21:36]: Start!
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:36]: 1
[00:21:36]: 2
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 1
[00:21:37]: 2
[00:21:37]: 2
[00:21:37]: 1
[00:21:37]: 2
Edited by Kzisor
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...