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

How To Refresh A Clock

Started by YuvonDovah, 23 September 2013 - 10:08 AM
YuvonDovah #1
Posted 23 September 2013 - 12:08 PM
Spoiler[attachment=1336:2013-09-23_16.59.58.png]
Please open image in new tab to see clearer.

So my problem is that when I have the time on my OS, in the top right hand of the screen in the picture, it doesn't refresh. Whenever I click it does refresh but I would like it to refresh automatically every second. I know that I can do this with the os.startTimer(3) command but am not sure how to implement it into my code and I have no idea how to use it.


function Time()
local time = os.time()
time = textutils.formatTime(time, true)
term.setCursorPos(47,1)
print(""..time)
end


Spoiler
--Desktop

slc = 0
tBarC = 2048
tBartC = 1
backColor = 32768
term.setBackgroundColor(backColor)
term.clear()
contextX = 0
contextY = 0
function titleBar()
  term.setCursorPos(1,1)
  term.setBackgroundColor(tBarC)
  term.setTextColor(tBartC)
  term.clearLine()
  term.setCursorPos(3, 1)
  print("[Main Menu]")
end
function drawDesktop()
  term.setBackgroundColor(backColor)
  term.clear()
  bground = paintutils.loadImage("background_1")
  paintutils.drawImage(bground,1, 1)
  term.setBackgroundColor(2048)
  term.setTextColor(1)
  term.setCursorPos(3,3)
  print("[Help]")
  titleBar()

end

function drawMenu1()
term.setTextColor(1)
term.setBackgroundColor(8)
term.setCursorPos(3,2)
print("           ")
term.setCursorPos(3,3)
print(" -Shutdown ")
term.setCursorPos(3,4)
print(" -Restart  ")
term.setCursorPos(3,5)
print("           ")
end
function drawMenu2()
term.setBackgroundColor(8)
term.setTextColor(1)
term.setCursorPos(contextX, contextY)
print("              ")
term.setCursorPos(contextX, contextY+1)
print(" -Edit GUI    ")
term.setCursorPos(contextX, contextY+2)
print(" -FileManager ")
term.setCursorPos(contextX, contextY+3)
print("              ")
end

drawDesktop()
while true do
local event, button, X, Y = os.pullEventRaw()
  if slc == 0 then
    if event == "mouse_click" then
      if X >=3 and X <=13 and Y==1 and button ==1 then
      drawMenu1()
      slc = 1
        elseif X >=3 and X <=8 and Y >=3 and Y <=3 and button == 1 then
        shell.run("help")
          elseif X >= 1 and Y >=2 and button == 2 then slc = 2
            if X >=38 then
            contextX = 38
            end
            if Y >=14 then
            contextY = 14
            end
            if X <= 38 then
            contextX = X
            end
            if Y <= 14 then
            contextY = Y
            end
            drawMenu2()
        else
        drawDesktop()
      end
    end
   elseif slc == 1 then
     if X >=1 and X <=11 and button == 1 and Y== 3 then slc = 0 
       os.shutdown()
       elseif X>=1 and X<=11 and Y==4 and button ==1 then slc = 0
       os.reboot()
       else
       slc = 0
       drawDesktop()

     end 
  elseif slc == 2 then
        if X >= contextX and X <= contextX+13 and Y==contextY+1 and button == 1 then slc = 0
        shell.run("edit","gui")
        drawDesktop()
          elseif X>= contextX and X <=contextX+13 and Y==contextY+2 and button == 1 then slc = 0
            shell.run("Help")
            drawDesktop()
              else slc = 0
              drawDesktop()
        end
  end
end
BigTwisty #2
Posted 23 September 2013 - 12:48 PM
First off, it would be EXTREMELY helpful to see the rest of your code, as you may be overwriting your clock output elsewhere.

Second, it is a good idea to not hard code your screen locations, just in case your position is off or you decide to run code on a monitor.

function time()
  local time = textutils.formatTime(os.time(), true)
  local w = term.getSize()
  term.setCursorPos(w - #time + 1, 1)
  print(time)
end

3rd, you need to check the rest of your code to ensure this function is being called regularly. This is why we need to see the rest of your code.
YuvonDovah #3
Posted 23 September 2013 - 01:07 PM
First off, it would be EXTREMELY helpful to see the rest of your code, as you may be overwriting your clock output elsewhere.

Second, it is a good idea to not hard code your screen locations, just in case your position is off or you decide to run code on a monitor.

function time()
  local time = textutils.formatTime(os.time(), true)
  local w = term.getSize()
  term.setCursorPos(w - #time + 1, 1)
  print(time)
end

3rd, you need to check the rest of your code to ensure this function is being called regularly. This is why we need to see the rest of your code.

Okay, that is done now.
TheOddByte #4
Posted 23 September 2013 - 01:52 PM
Uhmm.. Just want to point out that it's not good to have the clock var named same as the function..

function time() --Function name is time
	local clock = textutils.formatTime(os.time(), true) -- This var was called time also, And it could error when you try to print it or something then..
	local w = term.getSize()
	term.setCursorPos(w - #time + 1, 1)
	print(clock) -- Here it printed the var called "time", As explained the same name as the function, Think it will error
end

And for you updating problem.. You could use a timer, And my guess you have some events pausing the loop and waiting for an event etc.
Here's an example

clockTimer = os.startTimer(.8) -- Start the timer for the first time
while true do
    evt, p1, mX, mY = os.pullEvent()
        if evt == "key" then
            -- Do something
        elseif evt == "timer" then
            clockTimer = os.startTimer(.8) -- Restart the timer when it has ran
        end
end
It's as simple as that, So all you have todo is to start a timer and restart it every time it has ran ;)/>
I think 0.8 is a MC second so you know.
BigTwisty #5
Posted 23 September 2013 - 02:42 PM
Uhmm.. Just want to point out that it's not good to have the clock var named same as the function..

As long as the variable is local to the function and he is not calling the function recursively from inside itself, this should cause no problems.

And for you updating problem.. You could use a timer, And my guess you have some events pausing the loop and waiting for an event etc.
Here's an example

clockTimer = os.startTimer(.8) -- Start the timer for the first time
while true do
	evt, p1, mX, mY = os.pullEvent()
		if evt == "key" then
			-- Do something
		elseif evt == "timer" then
			clockTimer = os.startTimer(.8) -- Restart the timer when it has ran
		end
end

This will only work if he is doing nothing else. If you look at the image in the OP, there are other menus and things being accessed simultaneously. Like I said, we really need to see the rest of the code. The solution in the previous post makes too many assumptions.
MKlegoman357 #6
Posted 23 September 2013 - 04:06 PM
Before os.pullEventRaw() do os.startTimer(0.8) and after os.pullEventRaw() run time().
EpicCenter #7
Posted 23 September 2013 - 05:55 PM
You simply need to tell it to clear the top right and then redo the function if this is the case and sleep for 1 second.
YuvonDovah #8
Posted 29 September 2013 - 02:07 PM
You simply need to tell it to clear the top right and then redo the function if this is the case and sleep for 1 second.
How would one do that? I mean set it to refresh only the top corner?