This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

Named timer function

Started by Exerro, 26 December 2012 - 11:42 PM
Exerro #1
Posted 27 December 2012 - 12:42 AM
Ok ive been messing around with making a new function that has named timers but it keeps throwing an error :l
heres the code:
Spoiler

fps = 20
function startTimer(name,time)
table.insert(timers,{name,time})
end
function updateTimers()
if #timers > 0 then
  for i = 1,#timers do
   timers[i].time = timers[i].time-(1/fps)
  end
end
end
function checkTimerOutput()
timersDone = {}
for i = 1,#timers do
  if timers[i].time <= 0 then --error here
   table.insert(timersDone,timers[i].name)
  end
end
if #timersDone > 0 then
  os.queueEvent("Timer",timersDone)
end
end
function main()
term.clear()
secretfunctionname:P.print(1,2,"hello","hi!","boom")
startTimer("ben",3)
event, name = os.pullEvent()
term.clear()
secretfunctionname:P.print(1,1,event,name)
loopBreaker = true
end
k = 0
function update()
k = k+1
checkTimerOutput()
updateTimers()
sleep(1/sky.clock.fps)
secretfunctionname:P.write(1,1,k)
end
while loopBreaker ~= true do
parallel.waitForAny(update,main)
end

I really dont know whats wrong
remiX #2
Posted 27 December 2012 - 01:30 AM
Can you please post the error too… Is it attempt to call nil?
KaoS #3
Posted 27 December 2012 - 01:49 AM
you are using the table "timers" when you have not defined it as a table. add

local timers={}
to the top of your code
Exerro #4
Posted 27 December 2012 - 02:17 AM
this isnt the whole code btw…im making a library thing like love2d and the table timers is defined at the top (forgot to add that into the code here)
all of the "clock" library is here:
Spoiler

local args = {...}
sky.clock = {}
sky.clock.fps = 20
sky.clock.timers = {}
function sky.clock.startTimer(name,time) -- wont work
table.insert(sky.clock.timers,{name,time})
end
function sky.clock.setFPS(fps)
sky.clock.fps = fps
end
function sky.clock.updateTimers() -- broken
if #sky.clock.timers > 0 then
  for i = 1,#sky.clock.timers do
   if sky.clock.timers[i].time ~= nil then
	sky.clock.timers[i].time = sky.clock.timers[i].time-(1/sky.clock.fps)
   end
  end
end
end
function sky.clock.checkTimerOutput() -- broken
sky.clock.timersDone = {}
for i = 1,#sky.clock.timers do
  if sky.clock.timers[i].time ~= nil and sky.clock.timers[i].time <= 0 then
   table.insert(sky.clock.timersDone,sky.clock.timers[i].name)
   sky.clock.timers[i] = nil
  end
end
if #sky.clock.timersDone > 0 then
  os.queueEvent("skyTimer",sky.clock.timersDone)
end
end

the error is something like attempt to compare nil with number
KaoS #5
Posted 27 December 2012 - 02:19 AM
we need the full code to assist you…
Exerro #6
Posted 27 December 2012 - 02:28 AM
all code here…the timer stuff is in BlueSky/clock.lua test.lua is where it runs BlueSky.lua is the code that puts it all together
ChunLing #7
Posted 27 December 2012 - 04:11 PM
We also would prefer to have the line number of the error. The error message should specify which string (file) cause the error on what line. A tip, [Bios]:666 (or whatever) is the clever program that notices your mistakes, it is not the source of the mistake.