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

[LUA] Help with combining 2 loops

Started by weezer22, 20 January 2013 - 04:34 AM
weezer22 #1
Posted 20 January 2013 - 05:34 AM
Hi, I'm new here and to coding in general.
So I've got 2 functions that work fine seperetaly, but I cant figure out how to combine them properly.

The first function displays the time on the screen and gets updated every second.

function clock()
clock = true
while clock do
  term.clear()
  term.setCursorPos(10,6)
  local time = os.time()
  time = textutils.formatTime(time, true)
  print('The time is: '..time)
  os.sleep(1)
  break
end
end

The second function disables the clock when enter is pressed.

function keyread()
while true do
  local sEvent, param = os.pullEvent('key')
  if sEvent == 'key' then
   if param == 28 then
	clock = false
	term.clear()
	term.setCursorPos(10,6)
	print('Clock stopped')
	term.restore()
   end
  end
end
end

I've tried quite a few things and this is the closest I've been to make it work.

Here, the time is shown, but only gets updated when any key is pressed. Enter stops the clock as intended.

clock = true
while true do
local sEvent, param = os.pullEvent('key')
if sEvent == 'key' then
   if param == 28 then
	clock = false
	term.clear()
	term.setCursorPos(10,6)
	print('Clock stopped')
	term.restore()
	break
  else
   while clock do
	term.clear()
	term.setCursorPos(10,6)
	local time = os.time()
	time = textutils.formatTime(time, true)
	print('The time is: '..time)
	os.sleep(1)
	break
   end
  end
end
end

Any help is much appreciated :)/>
ikke009 #2
Posted 20 January 2013 - 05:44 AM
It stops because when you do os.pullEvent("key") the program waits untill a key gets pressed.
What you need is os.startTimer(), every timer you create has its unique id

something like

timerID = os.startTimer(1)
ev,id = os.pullEvent()
if ev == "timer" and id == timerID then
  --clocks moves 1 second
  timerID = os.startTimer(1)
elseif ev == "key" and id == 28 then
  --stops the clock
end
KaoS #3
Posted 20 January 2013 - 09:21 AM
ikke009 has the right idea. here is the code


repeat
os.startTimer(1)
local evt={os.pullEvent()}
term.clear()
term.setCursorPos(1,1)
print("the time is: "..textutils.formatTime(os.time(),true))
until evt[1]=="key" and evt[2]==28
print("clock stopped)
weezer22 #4
Posted 20 January 2013 - 10:26 AM
Thanks!

KaoS, I used your code and it works great.
I had it first like this (without the curly brackets)

...
local evt=os.pullEvent()
...
But pressing enter didn't work then. Can you say why that is?
remiX #5
Posted 20 January 2013 - 11:05 AM
Putting it into 'curly' brackets just stores the returned values into a table. So it can be accessed like so:
evt[1] -- is the event name
evt[2] -- is the first param
evt[3] -- second param

The way you're doing it you will need to do this:

local evt, p1, p2, p3 = os.pullEvent()
if p1 == keys.enter then
-- enter key
end
theoriginalbit #6
Posted 20 January 2013 - 11:09 AM
Putting it into 'curly' brackets just stores the returned values into a table. So it can be accessed like so:
evt[1] -- is the event name
evt[2] -- is the first param
evt[3] -- second param

The way you're doing it you will need to do this:

local evt, p1, p2, p3 = os.pullEvent()
if p1 == keys.enter then
-- enter key
end
Its also good when you are expecting different events which all have a different parameter count… some return just 1 value ( the event ) and some can return 4… so putting them in { } and allows the code to look nice and not be too confusing…
remiX #7
Posted 20 January 2013 - 11:16 AM
Putting it into 'curly' brackets just stores the returned values into a table. So it can be accessed like so:
evt[1] -- is the event name
evt[2] -- is the first param
evt[3] -- second param

The way you're doing it you will need to do this:

local evt, p1, p2, p3 = os.pullEvent()
if p1 == keys.enter then
-- enter key
end
Its also good when you are expecting different events which all have a different parameter count… some return just 1 value ( the event ) and some can return 4… so putting them in { } and allows the code to look nice and not be too confusing…

Yeah, I always store them into a table. Was just showing him the way without doing so.

Also, I forgot to make it check if evt == "key" then :P/> woops
weezer22 #8
Posted 20 January 2013 - 11:32 AM
Ok, thanks!