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

time and menu simultanously?

Started by H4X0RZ, 21 June 2013 - 10:29 AM
H4X0RZ #1
Posted 21 June 2013 - 12:29 PM
Hello all,
I triy to make a program, that has a menu and shows the time at once.
But if I run it in a parallel it wont work!
Code

local function menu()
--some crappy menu things
end

local function time()
While true do
term.setCursorPos(1,1)
term.write(textutils.formatTime(os.time(), true)
sleep(10)
end
end
parallel.waitForAny(menu, time)
whats wrong?
ElvishJerricco #2
Posted 21 June 2013 - 12:48 PM
First, tabs in your code would be nice.

Second, your "while" is capitalized but I'm assuming it's not like that in your code, because that would be an obvious problem.

Third, your menu function is looping infinitely, right? And waiting for events to update itself? If not, then menu is finishing and waitForAny is finishing because of that.

Finally, make sure your menu code isn't drawing over the time text after the time gets written.
TheOddByte #3
Posted 21 June 2013 - 12:50 PM
Well it only runs one time
Try todo this instead

while true do
  parallel.waitForAny(menu,time)
end

EDIT: Damn ninja's..
H4X0RZ #4
Posted 21 June 2013 - 12:56 PM
First, tabs in your code would be nice.

Second, your "while" is capitalized but I'm assuming it's not like that in your code, because that would be an obvious problem.

Third, your menu function is looping infinitely, right? And waiting for events to update itself? If not, then menu is finishing and waitForAny is finishing because of that.

Finally, make sure your menu code isn't drawing over the time text after the time gets written.
1. I know, but hard to do with mobile :D/>
2. It's because I'm at my mobile
3. Yes it loops infinite
4.It isn't drawing over the time
theoriginalbit #5
Posted 21 June 2013 - 12:58 PM
is that a deliberate sleep(10) for the time loop? or do you have it as sleep(1) in the code?
H4X0RZ #6
Posted 21 June 2013 - 01:05 PM
is that a deliberate sleep(10) for the time loop? or do you have it as sleep(1) in the code?

In my real code I have sleep(.025) :@
LBPHacker #7
Posted 21 June 2013 - 01:44 PM
If you think about it, a Minecraft day contains 24000 ticks = 1200 seconds (= 20 minutes), while a real day contains 1440 minutues - that means that the formatted time returned by textutils.formatTime changes in every 1200 / 1440 = 1 / 1,2 = 0,833333 seconds. Use that instead of 0,025.
Bomb Bloke #8
Posted 21 June 2013 - 06:37 PM
In this case, it might be easier to set your menu code to wait for a keypress event or a timer event. If it gets either, then it updates the display - changing the selected line and the displayed time. Whenever the timer event gets caught it'd set a new one rigged to expire within a very short period.

I'm also suspecting the current menu code is interfering.