Skip to content

Commit

Permalink
- Added some basic timer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakBlystone committed Feb 11, 2020
1 parent a5e9236 commit 01bd232
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
43 changes: 43 additions & 0 deletions gamemodes/bpdefs/content/timer.txt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
13 changes: 7 additions & 6 deletions lua/blueprints/module/sh_bpcompiledmodule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 01bd232

Please sign in to comment.