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

os.isTimerCompleted()

Started by matejdro, 27 October 2012 - 09:49 AM
matejdro #1
Posted 27 October 2012 - 11:49 AM
It would be useful if we would have ability to check if timer already completed. So in case I missed event, I can still know if timer was completed or not.

I know I could do that with storing os.clock() and checking it, but I feel that this would duplicate internal CC code and should be unnecessary.
Cloudy #2
Posted 27 October 2012 - 12:43 PM
Just make sure you don't miss an event. There should be no reason that you could miss an event, given that the event is a queue.
matejdro #3
Posted 27 October 2012 - 12:46 PM
Wait, event is a queue? I always thought its a one-time thing - if you are not pulling at the time, you miss it.
Cloudy #4
Posted 27 October 2012 - 01:31 PM
Wait, event is a queue? I always thought its a one-time thing - if you are not pulling at the time, you miss it.

Definitely a queue - but just beware that things that sleep (correctly, in my opinion) discard any events which they are not looking for. Using timers, you can avoid sleep eating events by not using it.
ChunLing #5
Posted 27 October 2012 - 02:38 PM
Also, a lot of other things that need to pull events discard everything but the event they want, it isn't just sleep. If your program is waiting for a rednet message, it won't receive key pulls, if it's waiting for keyboard input, it won't get rednet messages. Given the number of things that pull (and discard) events until they get what they're looking for, it can seem like you need something special. But it isn't too hard to make something that responds to whatever event you pull in the manner you want, like something that can act on a rednet message or a key or a timer.
KaoS #6
Posted 27 October 2012 - 10:02 PM
^^ this is why you should listen for all events in one line and process it in one go. either that or make a recording coroutine. a table would be a great idea to organize this
ChunLing #7
Posted 27 October 2012 - 11:06 PM
Yeah, I've suggested the idea of using a table to store all the pulled events before. But when I think it over there are limited benefits, since you would still have to write your own functions to replace everything that pulled events. It mostly means that you can keep the history of events, and there are possible uses for that in making the program smarter or even seem adaptive. But that's pretty advanced.
matejdro #8
Posted 28 October 2012 - 11:19 AM
Is there a list of functions that discard events? I'm having a program that waits for timer or rednet_message event in a loop. If rednet_message is triggered first and after part of program that responds to that rednet is finished, I will not get the timer when I run os.pullEvent() again.
ChunLing #9
Posted 28 October 2012 - 11:47 AM
Pretty much anything that waits for input will, by default, discard all the other inputs. So if you want to combine listening for rednet messages and inputting text, you'll have to write your own versions of both read() and rednet.receive().
Cruor #10
Posted 28 October 2012 - 11:50 AM
This is allready possible.

While true do
	timer = os.startTimer(nTime)
	event, par1 = os.pullEvent()
	if event == "timer" and par1 == timer then
		yourCode()
	end
end

Edit: oh incase you missed the event, good luck XD
matejdro #11
Posted 28 October 2012 - 11:53 AM
I'm not using read() or rednet.receive(). Everything is received through pullEvent(). Do any turtle function discards events?

Cruor: I don't really get your post. I know I can use events and timers. I'm talking in case I miss the event.
ChunLing #12
Posted 28 October 2012 - 12:09 PM
Yes, the turtle functions can lose events. Or at least, when I use remote control on my turtle, calling the functions right off the rednet messages, some of the messages get discarded if they come while the turtle is still moving.
Cruor #13
Posted 28 October 2012 - 12:38 PM
When in doubt, use turtle.native instead :D/>/> iirc it dosnt wait for the event to complete, it just adds it to the queue
KaoS #14
Posted 28 October 2012 - 12:42 PM
which is why you have a coroutine that listens and a coroutine to execute


local tQueue={}

local listen=coroutine.wrap(function()
while true do
   local input={rednet.receive()}
   input[2]=textutils.unserialize(input[2])
   if input[2][1]=='remote' then
	tQueue[#tQueue+1]=input[2]
   end
end
end)

local exec=coroutine.wrap(function()
while true do
   if tQueue[1] then
	local oFile=io.open('running','w')
	oFile:write(table.remove(tQueue,1))
	oFile:close()
	shell.run('running')
   else
    sleep(0.5)
   end
end
end)

local evts={}
while true do
listen(unpack(evts))
exec(unpack(evts))
evts={os.pullEvent()}
end

that way it will never miss an event. to tell it to do something just send it

textutils.serialize({'remote',COMMANDS})
matejdro #15
Posted 28 October 2012 - 01:02 PM
But you cannot detect when turtle finishes moving using native?
KaoS #16
Posted 28 October 2012 - 01:38 PM
yes you can do it manually. it sends an event when it is done, using the native command just prevents it from waiting for that event and eating all others. look up the turtle api and see how it works
matejdro #17
Posted 28 October 2012 - 02:07 PM
When you do os.pullEvent("redstone") for example, are all non-redstone events automatically discarded? Or are they still waiting?
Cloudy #18
Posted 28 October 2012 - 02:25 PM
When you do os.pullEvent("redstone") for example, are all non-redstone events automatically discarded? Or are they still waiting?

Yes. That's the point of restricting it to one event.