From 01bd232577d09ef9b3509bad7fef09ad743e8075 Mon Sep 17 00:00:00 2001 From: Zak Blystone Date: Mon, 10 Feb 2020 19:25:41 -0800 Subject: [PATCH] - Added some basic timer functionality --- gamemodes/bpdefs/content/timer.txt | 43 +++++++++++++++++++ lua/blueprints/module/sh_bpcompiledmodule.lua | 13 +++--- 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 gamemodes/bpdefs/content/timer.txt diff --git a/gamemodes/bpdefs/content/timer.txt b/gamemodes/bpdefs/content/timer.txt new file mode 100644 index 00000000..4193b91f --- /dev/null +++ b/gamemodes/bpdefs/content/timer.txt @@ -0,0 +1,43 @@ +LIB TIMER +{ + SPECIAL Create, SHARED + { + IN Exec, PN_Exec, #Starts the timer + IN Delay, PN_Number, #How long until the timer finishes + IN Key, PN_Any, #Uniquely identifies the timer + OUT Thru, PN_Exec, #Called after the timer finishes + LATENT + CODE + { + __bpm.delay(tostring($3), $2, function() @graph(#_1) end) + goto popcall + } + } + SPECIAL CreateBoxed, SHARED + { + IN Exec, PN_Exec, #Starts the timer + IN Delay, PN_Number, #How long until the timer finishes + IN Key, PN_Any, #Uniquely identifies the timer + IN Value, PN_Any, #Value to pass through the timer + OUT Thru, PN_Exec + OUT Value, PN_Any + INFORM 4,6 + LATENT + CODE + { + __bpm.delay(tostring($3), $2, function(x) #2 = x @graph(#_1) end, $4) + goto popcall + } + } + SPECIAL Cancel, SHARED + { + IN Exec, PN_Exec, #Cancels the timer + IN Key, PN_Any, #Unique ID of the timer to cancel + OUT Thru, PN_Exec, #Called after the timer is cancelled + CODE + { + __bpm.delayKill($2) + #1 + } + } +} \ No newline at end of file diff --git a/lua/blueprints/module/sh_bpcompiledmodule.lua b/lua/blueprints/module/sh_bpcompiledmodule.lua index d3ffca56..cd45876d 100644 --- a/lua/blueprints/module/sh_bpcompiledmodule.lua +++ b/lua/blueprints/module/sh_bpcompiledmodule.lua @@ -388,10 +388,11 @@ __bpm.delayExists = function(key) for i=#__self.delays, 1, -1 do if __self.delays[i].key == key then return true end end return false end -__bpm.delay = function(key, delay, func) - for i=#__self.delays, 1, -1 do if __self.delays[i].key == key then table.remove(__self.delays, i) end end - __self.delays[#__self.delays+1] = { key = key, func = func, time = delay } +__bpm.delay = function(key, delay, func, ...) + __bpm.delayKill(key) + __self.delays[#__self.delays+1] = { key = key, func = func, time = delay, args = {...} } end +__bpm.delayKill = function(key) for i=#__self.delays, 1, -1 do if __self.delays[i].key == key then table.remove(__self.delays, i) end end end __bpm.onError = function(msg, mod, graph, node) end __bpm.makeGUID = function() local d,b,g,m=os.date"*t",function(x,y)return x and y or 0 end,system,bit @@ -413,9 +414,9 @@ fragments["update"] = function(args) function meta:update()]] .. x .. [[ self:netUpdate() for i=#self.delays, 1, -1 do - self.delays[i].time = self.delays[i].time - FrameTime() - if self.delays[i].time <= 0 then - local s,e = pcall(self.delays[i].func) + local d = self.delays[i] d.time = d.time - FrameTime() + if d.time <= 0 then + local s,e = pcall(d.func, unpack(d.args)) if not s then self.delays = {} __bpm.onError(e, 0, __dbggraph or -1, __dbgnode or -1) end if type(e) == "number" then self.delays[i].time = e else table.remove(self.delays, i) end end