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

running a clock/loop and another thing in one api

Started by gopro_2027, 01 May 2013 - 10:38 AM
gopro_2027 #1
Posted 01 May 2013 - 12:38 PM
i have been making my first os on the regular computer. after the login screen it goes to the desktop i made that has a couple of different shortcuts to different apis that i made by pressing a single key. i want to add a clock in the bottom right corner of the desktop (which i already implemented in it) while the other things for the shortcuts are still able to be used/pressed. the problem is that when the clock is in the coding i cant press the shortcuts. here is the code:



term.clear()
term.setCursorPos(1 ,1 )
print ("[][][][][][][][][][][][][][][][][][][][][][][][][]")
print ("[]desktop                                       []")
print ("[]               HELLO MASTER                   []")
print ("[]            DO WHAT YOU PLEASE                []")
print ("[]    _________  ___________     __________     []")
print ("[]    [  'e'  ]  [   '-'   ]     [  'g'   ]     []")
print ("[]    [welcome]  [animation]     [rpg game]     []")
print ("[]                                              []")
print ("[]    ___________   ______         ______       []")
print ("[]    [  's'    ]   ['h' ]         [any ]       []")
print ("[]    [shut down]   [help]         [exit]       []")
print ("[]                                              []")
print ("[]                                              []")
print ("[]PRESS 'key'                                   []")
print ("[]______________________________________________[]")
print ("[]START|                          |er-76%|'t'ime[]")
print ("[][][][][][][][][][][][][][][][][][][][][][][][][]")
while true do -- beginning of clock
term.setCursorPos(36,16)
local nTime = os.time()
print( "Time: "..textutils.formatTime( nTime, false ) )
os.sleep(1)
end -- end of clock
local event, param1 = os.pullEvent ("char")
if param1 == "e" then
os.loadAPI ("hello")
else
end
if param1 == "-" then
os.loadAPI ("-")
else
end
if param1 == "g" then
os.loadAPI ("rpg")
else
end
if param1 == "s" then
os.loadAPI ("shutd")
else
end
if param1 == "h" then
os.loadAPI ("help")
else
end


the clock it shown where i labeled it. the gui looks funny but thats because of the weird spacing.
can anyone fix this up for me so that it works?
Edited by
Lyqyd #2
Posted 01 May 2013 - 01:07 PM
Split into new topic.
remiX #3
Posted 01 May 2013 - 01:19 PM
Your if blocks are wrong.
This is how if blocks must look:
if <condition> then
    -- do this
elseif <this condiction> then
    -- do this other thing
elseif <this other condition> then
    -- do this other other thing
else
    -- do that
end
H4X0RZ #4
Posted 01 May 2013 - 01:37 PM
You want to do clock and char handling at the same time?
Try this:

--Your menu stuff here

--Clock function
function clock()
while true do
nTime = os.clock()
term.setCursorPos(your pos)
term.write(textutils.formatTime(nClock, false))
end
end

--Char handling function
function char_handling()
while true do
local sEvent, sChar = os.pullEvent("char")
pressed_char = sChar
return
end
end

parallel.waitForAny(clock, char_handling)  -- to run these two functions at the same time

if pressed_char == "char1" then --replace the char in al lines
--do something
elseif pressed_char == "char2" then
--do something other
else
os.shutdown()
end

PS:
Sorry, if something is wrong. I tipped with my mobile ^^
remiX #5
Posted 01 May 2013 - 02:33 PM
Freack100, I think he would want a time that updates in an infinite loop until the user wants to exit the program.

That clock function will error with too long without yielding too