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

os.startTimer not doing the time I specify

Started by Neywiny, 03 May 2015 - 01:33 AM
Neywiny #1
Posted 03 May 2015 - 03:33 AM
I currently have this as my code (only posted cuz y'all always ask for it), look into the main loop for my problem.
Spoiler

for k,v in pairs(peripheral.getNames()) do
t = peripheral.getType(v)
if t == "BigReactors-Reactor" then
  r = peripheral.wrap(v)
  --print("found reactor!")
elseif t == "monitor" then
  m = peripheral.wrap(v)
  --print("found monitor!")
end
end
b = {}
tno = {"getControlRodName","getCoolantType","getControlRodLevel","getControlRodLocation","getHotFluidType"}
m.clear()
m.setCursorPos(1,0)
m.setTextScale(1)
function mprint(text)
xx,yy = m.getCursorPos()
m.setCursorPos(1,yy+1)
m.write(text)
end
function addButton(x,y,text,func)
b[x..y] = {x,y,text,func}
end
function testTable(k,t)
for i,y in pairs(t) do
  if y == k then
   return true
  end
end
return false
end
function rawDiagnostics()
mprint("raw diagnostics!")
for k,v in pairs(r) do
  tf = string.sub(k,0,2)
  if tf == "ge" or tf == "is" then
   if testTable(k,tno) == false then
    x,y,z = v()
    str = x
    if y then
	 str = str..","..y
    end
    if z then
	 str = str..","..z
    end
    mprint(k..": "..tostring(str))
   else
    mprint(k..": args needed")
   end
  end
end
end
function checkxy(x, y)
   for name, data in pairs(B)/>/> do
	  if y>=data[2] and  y <= data[2] then
		 if x>=data[1] and x<= data[1]+#tostring(data[3]) then
		    data[4]()
		    return true
		 end
	  end
   end
   return false
end
function toggleActive()
r.setActive(not r.getActive())
end
function draw()
for h,j in pairs(B)/>/> do
  m.setCursorPos(j[1],j[2])
  m.write(j[3])
end
end
function addText(x,y,text)
--b[text] =
end
while true do
b = {}
addButton(2,2,r.getActive(),toggleActive)
m.clear()
draw()
timer = os.startTimer(0.5)
a,side,x,y = os.pullEvent()
if a == "monitor_touch" then
  checkxy(x,y)
elseif a == "timer" then
  timer = os.startTimer(10)
  print("update!",os.time())
end
end
as you can see, it should start a timer for 0.5 sec at the start then 10 sec every time after, however it looks like it pulls the timer every possible computer tick, since the printing of the os.time prints ~every tenth of a second. I need to slow that down to the specified time, which will eventually be 0.5 sec.

this has happened for another program of mine, but it wasn't a problem for that one's application.
Edited on 03 May 2015 - 01:34 AM
KingofGamesYami #2
Posted 03 May 2015 - 03:41 AM
You'll want to add this to your statement:


elseif a == "timer" and side == timer then
 --#do stuff

Otherwise, it's going to use any timer event, not just the ones you generate.
Neywiny #3
Posted 03 May 2015 - 03:48 AM
thanks, I was experimenting with the side of the timer since it seemed to be going up at an alarming rate. thanks for the answer, trying it out now.
Bomb Bloke #4
Posted 03 May 2015 - 03:48 AM
That, and you'll want to rig things so that you don't generate a new timer for every event that occurs.

If you feel that you must make a new timer on every event, cancel the old timers first.
Neywiny #5
Posted 03 May 2015 - 03:52 AM
yeah I just both answers into account, removing the creation of a new timer in the elseif, and testing, and it runs smoothly now. will report back if I need any more help with it.