Jump to content




How To Refresh A Clock


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

#1 YuvonDovah

  • Members
  • 28 posts

Posted 23 September 2013 - 11:08 AM

Spoiler

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


#2 BigTwisty

  • Members
  • 106 posts

Posted 23 September 2013 - 11:48 AM

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.

#3 YuvonDovah

  • Members
  • 28 posts

Posted 23 September 2013 - 12:07 PM

View PostBigTwisty, on 23 September 2013 - 11:48 AM, said:

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.

#4 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 23 September 2013 - 12: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.

#5 BigTwisty

  • Members
  • 106 posts

Posted 23 September 2013 - 01:42 PM

View PostHellkid98, on 23 September 2013 - 12:52 PM, said:

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.

View PostHellkid98, on 23 September 2013 - 12:52 PM, said:

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.

#6 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 23 September 2013 - 03:06 PM

Before os.pullEventRaw() do os.startTimer(0.8) and after os.pullEventRaw() run time().

#7 EpicCenter

  • Members
  • 13 posts

Posted 23 September 2013 - 04: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.

#8 YuvonDovah

  • Members
  • 28 posts

Posted 29 September 2013 - 01:07 PM

View PostEpicCenter, on 23 September 2013 - 04:55 PM, said:

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users