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

[Question] 2 things at same time

Started by Erickom, 31 March 2012 - 06:10 PM
Erickom #1
Posted 31 March 2012 - 08:10 PM
Hello!
I have made a program, more like a desktop, with shortcuts to my programs using a menu(as seen here). At the top of the desktop there is a hour bar who uses os.time. It actually works, but the hour only updates when i press a key(arrow up/arrow down). My doubt is, can I make the hour update, while, at the same time, the program waits me press a key?

If is there any english error, sorry, I'm still training my english.
ComputerCraftFan11 #2
Posted 31 March 2012 - 08:33 PM
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">YES< NO") else write (" YES >NO<") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end


In the code, it has:
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end



That means before it continues, it waits for you to press a button, so it stops the timer and everything else
Advert #3
Posted 31 March 2012 - 08:59 PM
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">YES< NO") else write (" YES >NO<") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end


In the code, it has:
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end



That means before it continues, it waits for you to press a button, so it stops the timer and everything else

Please, use code tags.

You can use timers:


local updateTimer = os.startTimer()
...
<inside your loop checking for keys>
if event == "timer" and param1 == updateTimer then
 updateYourHourBar()
 updateTimer = os.startTimer(1) -- Update it every second.
elseif event == "key" and blah then
...
end
Something like that.

I can't guess how you've done your code though, so you'll have to post it if you want more informative help.
MysticT #4
Posted 31 March 2012 - 09:09 PM
You can use a timer to do that:

local timer = os.startTimer(1)
while true do
  local evt, arg = os.pullEvent()
  if evt == "key" then
	do_menu(arg)
  elseif evt == "timer" then
	update_timer()
	if arg == timer then
	  timer = os.startTimer(1)
	end
  end
end
So it updates the timer each second, and your menu works at the same time.

Edit: oops, too late :o/>/>
ComputerCraftFan11 #5
Posted 31 March 2012 - 09:11 PM
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">YES< NO") else write (" YES >NO<") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end


In the code, it has:
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end



That means before it continues, it waits for you to press a button, so it stops the timer and everything else

Please, use code tags.

You can use timers:


local updateTimer = os.startTimer()
...
<inside your loop checking for keys>
if event == "timer" and param1 == updateTimer then
updateYourHourBar()
updateTimer = os.startTimer(1) -- Update it every second.
elseif event == "key" and blah then
...
end
Something like that.

I can't guess how you've done your code though, so you'll have to post it if you want more informative help.

My post + code tags

[color=#000000][font=helvetica, arial, sans-serif]a[/font][/color][color=#666600][font=helvetica, arial, sans-serif],[/font][/color][color=#000000][font=helvetica, arial, sans-serif] b[/font][/color][color=#666600][font=helvetica, arial, sans-serif]=[/font][/color][color=#000000][font=helvetica, arial, sans-serif]os[/font][/color][color=#666600][font=helvetica, arial, sans-serif].[/font][/color][color=#000000][font=helvetica, arial, sans-serif]pullEvent[/font][/color][color=#666600][font=helvetica, arial, sans-serif]()[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]while[/font][/color][color=#000000][font=helvetica, arial, sans-serif] a[/font][/color][color=#666600][font=helvetica, arial, sans-serif]~=[/font][/color][color=#008800][font=helvetica, arial, sans-serif]"key"[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]do[/font][/color][color=#000000][font=helvetica, arial, sans-serif] a[/font][/color][color=#666600][font=helvetica, arial, sans-serif],[/font][/color][color=#000000][font=helvetica, arial, sans-serif] b[/font][/color][color=#666600][font=helvetica, arial, sans-serif]=[/font][/color][color=#000000][font=helvetica, arial, sans-serif]os[/font][/color][color=#666600][font=helvetica, arial, sans-serif].[/font][/color][color=#000000][font=helvetica, arial, sans-serif]pullEvent[/font][/color][color=#666600][font=helvetica, arial, sans-serif]()[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]end[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]if[/font][/color][color=#000000][font=helvetica, arial, sans-serif] b[/font][/color][color=#666600][font=helvetica, arial, sans-serif]==[/font][/color][color=#006666][font=helvetica, arial, sans-serif]203[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]and[/font][/color][color=#000000][font=helvetica, arial, sans-serif] n[/font][/color][color=#666600][font=helvetica, arial, sans-serif]==[/font][/color][color=#006666][font=helvetica, arial, sans-serif]2[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]then[/font][/color][color=#000000][font=helvetica, arial, sans-serif] n[/font][/color][color=#666600][font=helvetica, arial, sans-serif]=[/font][/color][color=#006666][font=helvetica, arial, sans-serif]1[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]end[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]if[/font][/color][color=#000000][font=helvetica, arial, sans-serif] b[/font][/color][color=#666600][font=helvetica, arial, sans-serif]==[/font][/color][color=#006666][font=helvetica, arial, sans-serif]205[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]and[/font][/color][color=#000000][font=helvetica, arial, sans-serif] n[/font][/color][color=#666600][font=helvetica, arial, sans-serif]==[/font][/color][color=#006666][font=helvetica, arial, sans-serif]1[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]then[/font][/color][color=#000000][font=helvetica, arial, sans-serif] n[/font][/color][color=#666600][font=helvetica, arial, sans-serif]=[/font][/color][color=#006666][font=helvetica, arial, sans-serif]2[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]end[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]if[/font][/color][color=#000000][font=helvetica, arial, sans-serif] b[/font][/color][color=#666600][font=helvetica, arial, sans-serif]==[/font][/color][color=#006666][font=helvetica, arial, sans-serif]28[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]then[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]print[/font][/color][color=#666600][font=helvetica, arial, sans-serif]([/font][/color][color=#008800][font=helvetica, arial, sans-serif]""[/font][/color][color=#666600][font=helvetica, arial, sans-serif])[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]break[/font][/color][color=#000000][font=helvetica, arial, sans-serif] [/font][/color][color=#000088][font=helvetica, arial, sans-serif]end[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]end[/font][/color]
[color=#000088][font=helvetica, arial, sans-serif]
Erickom #6
Posted 31 March 2012 - 10:32 PM
Thanks for all, I will test these now.

I can't guess how you've done ysur code though, so you'll have to post it if you want more informative help.
Oh, here it is: (I wrote mostly in Portuguese, I hope you understand)

function OS()
meuid = KOMOS.boot()
sUsuario, lAdm, menu = KOMOS.login()
opcoes = {}
opcoes[1] = ("Painel de controle")
opcoes[2] = ("Programas")
opcoes[3] = ("CraftOS")
opcoes[4] = ("Logoff")
opcoes[5] = ("Sair")
escolha = {}
escolha[1] = function()
term.setCursorPos(1, 1)
Painelctrl.iniciar(meuid, lAdm, sUsuario)
enter = false
end
escolha[2] = function()
term.setCursorPos(1, 1)
print ("Em desenvolvimento.")
print ("Pressione enter para voltar ao menu..")
tecla = read()
enter = false
end
escolha[3] = function()
term.setCursorPos(1, 1)
print ("Digite 'retornar' para voltar ao KOMOS.")
sleep(2)
menu = false
enter = false
craftos = true
end
escolha[4] = function()
menu = false
enter = false
sUsuario, lAdm, menu, n = KOMOS.login()
end
escolha[5] = function()
term.setCursorPos(1, 1)
menu = false
enter = false
end
while menu == true do
term.clear()term.setCursorPos(1, 1)
local nTime = os.time()
  KOMOS.desktop()
  print ("===================================================")
  term.setCursorPos(1, 2)
  print ("|| KOMOS -- Computador "..meuid.."			"..textutils.formatTime(nTime, false).."   ||")
  term.write("|| Logado como "..sUsuario..".   ")
if lAdm == true then
  term.write("	  Administrador")
end
term.setCursorPos(49, 3)
write("||")
term.setCursorPos(1, 5)
for writeop = 1, 5, 1 do
  if writeop == n then
   print ("|| <"..opcoes[writeop]..">")
  else
   print ("||  "..opcoes[writeop].." ")
  end
end
  local a, b = os.pullEventRaw()
if a == "key" then
  if b == 208 and n >= 1 then
   n = n + 1
  end
  if b == 200 and n <= 5 then n = n - 1 end
  if b == 28 then enter = true end
  if n == 0 then n = 6
  elseif n == 6 then n = 1 end
end
if enter == true then
  term.clear()
  escolha[n]()
end
end
if craftos == true then
else
if craftos == true then
term.setCursorPos(1, 1)
print ("CraftOS 1.3")
else
KOMOS.desktop()
term.setCursorPos(15, 9)
print ("Desligando computador..")
sleep(2)
os.shutdown()
end
end
end
Advert #7
Posted 31 March 2012 - 10:43 PM
Thanks for all, I will test these now.

I can't guess how you've done ysur code though, so you'll have to post it if you want more informative help.
Oh, here it is: (I wrote mostly in Portuguese, I hope you understand)
<snip>

The method I described should be able to do what you want, but it'll render the whole screen again. That's not really a problem, though.