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

Help me with os.time() please

Started by spyman68, 16 March 2013 - 12:52 PM
spyman68 #1
Posted 16 March 2013 - 01:52 PM
FIXED. Curtsy of TheOriginalBIT Now it doesn't let me use the other functions of my OS(I can't click on any of my stuff)
Zoinky #2
Posted 16 March 2013 - 02:01 PM
Could we see the code for your OS? You can't have the code pause, because then the function won't be executed until later.
SuicidalSTDz #3
Posted 16 March 2013 - 02:08 PM
Parallels; Look it up on the wiki
remiX #4
Posted 16 March 2013 - 09:44 PM

local sX, sY = term.getSize()

local function updateTime()
    local updateTimer = os.startTimer( 1 )
    while true do
        local _, p = os.pullEventRaw( 'timer' )
        if p == updateTimer then
            local cx, cy = term.getCursorPos() -- if you have functions that write a lot, you will need this
            local t = textutils.formatTime( os.time(), false )
            term.setCursorPos( sX - #t, 1 )
            write( t )
            term.setCursorPos( cx, cy )
        end
    end
end

local function otherMainFunction()
    while true do
        ...
    end
end

parallel.waitForAny( updateTime, otherMainFunction )
spyman68 #5
Posted 17 March 2013 - 01:09 AM
RemiX, the time goes up 2 seconds then I can't click anything and it just freezes my os, other then that I think it works
spyman68 #6
Posted 17 March 2013 - 01:20 AM
Parallels; Look it up on the wiki
Parallels aren't on the the wiki…
theoriginalbit #7
Posted 17 March 2013 - 01:22 AM
Parallels aren't on the the wiki…
It is indeed wiki link
spyman68 #8
Posted 17 March 2013 - 01:29 AM
Ok, well TheOriginalBIT do you know how I would go about using this for a timer? The code I have for it is
Spoiler
function titleTime()
term.setBackgroundColor(8)
term.setTextColor(128)
term.setCursorPos(43,1)
print(textutils.formatTime(os.time()).." ")
end
I'm not sure how to make it keep updateing because if I use 'while true do' then It stops after a certain amount of time with an error.
theoriginalbit #9
Posted 17 March 2013 - 01:46 AM
the code i will suggest will be like what remiX suggested… whats the error that you are getting?

local function runOs()
  while true do
	-- os code
	-- this needs to have a sleep or a os.pullEvent in it so that it yields and doesnt stop the program from running
  end
end

local function timeUpdate()
  local timeUpdate = os.startTimer( 1 )
  while true do
	local _, triggered = os.pullEventRaw( 'timer' )
	if triggered == timeUpdate then
	  titleTime()
	  timeUpdate = os.startTimer( 1 )
	end
  end
end

parallel.waitForAny( timeUpdate, runOs )
spyman68 #10
Posted 17 March 2013 - 02:02 AM
Thank you sooooooo much TheOriginalBIT, your's worked but for some reason remiX's didn't. :D/>I typed the new problem in the first post. Please help with this too :lol:/> :ph34r:/>
theoriginalbit #11
Posted 17 March 2013 - 02:13 AM
Thank you sooooooo much TheOriginalBIT, your's worked but for some reason remiX's didn't. :DI typed the new problem in the first post. Please help with this too :lol:/> :ph34r:/>
Oh haha, I see why remixes doesn't, he never restarts the timer :P/> for the new error you will have to post your code. it doesn't make sense as to why it wouldn't work.
spyman68 #12
Posted 17 March 2013 - 02:30 AM
I will PM you my OS because I don't want everyone to know it but I trust you.
theoriginalbit #13
Posted 17 March 2013 - 02:31 AM
Ok then…
SuicidalSTDz #14
Posted 17 March 2013 - 03:47 AM
See kids, aren't parallels fun! ^_^/>
LNETeam #15
Posted 17 March 2013 - 06:17 AM
Could we see the code for your OS? You can't have the code pause, because then the function won't be executed until later.

He could always use coroutine.

Parallels; Look it up on the wiki

Could use this though, prefer it actually
theoriginalbit #16
Posted 17 March 2013 - 03:55 PM
See kids, aren't parallels fun! ^_^/>
You know whats more fun……. pull event loops :P/> 9.8/10 coroutines don't need to be used and can be replaced with a simple pull event loop… I've only ever needed to use a coroutine once in all my programs and its cctube. networking runs in its own coroutine so that the http calls can be made but the gui can still update and such.



Could we see the code for your OS? You can't have the code pause, because then the function won't be executed until later.

He could always use coroutine.

Parallels; Look it up on the wiki

Could use this though, prefer it actually
Coroutines and parallel is the same. parallel is just managed coroutines.
SuicidalSTDz #17
Posted 17 March 2013 - 04:04 PM
Screw parallels and coroutines! Vectors are the next big thing!

vector.new(gps.locate(TheOriginalBIT))
Muahahaha!
spyman68 #18
Posted 18 March 2013 - 02:16 AM
BTW, this problem is solved thanks to TheOriginalBIT, he solved my first problem AND my second problem
theoriginalbit #19
Posted 18 March 2013 - 09:01 AM
AND it was solved by removing the coroutines and modifying the pull event loop… Just saying 9.8/10…. :P/>
immibis #20
Posted 18 March 2013 - 02:25 PM
THEORIGINALBIT: You know HTTP also uses events?
theoriginalbit #21
Posted 18 March 2013 - 02:32 PM
THEORIGINALBIT: You know HTTP also uses events?

Yes I do know http uses events, but it would be weird for our GUI handler (which is the main event loop) to also handle the networking. So there is a separate routine for networking.