Jump to content




running a clock/loop and another thing in one api


  • You cannot reply to this topic
4 replies to this topic

#1 gopro_2027

  • New Members
  • 1 posts

Posted 01 May 2013 - 11:38 AM

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, 01 May 2013 - 12:08 PM.
added code tags


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 01 May 2013 - 12:07 PM

Split into new topic.

#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 01 May 2013 - 12: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


#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 01 May 2013 - 12: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 ^^

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 01 May 2013 - 01: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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users