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

turtle / pullEvent

Started by BloodyEpi, 04 December 2012 - 01:03 AM
BloodyEpi #1
Posted 04 December 2012 - 02:03 AM
Hey,

as im writing a little program for my turtle (eg movement via keys, autorespond to some server request,…) i was trying to not use any multithreading or coroutines, but to use only os.pullEvent and some marker, like:


--mainloop
	os.queueEvent("last_event")
	while runCheckLoop do
		local event, param1, param2, param3 = os.pullEvent()
		if event == "last_event" then
			runCheckLoop = false
		--other events
		end
	end
	--other code

It all went well until I tried to use the TurtleAPI, turns out there's a function (local waitForResponse()), that discards all events other than the one it needs, that, of course, screws my design pretty good, since my "last_event" is just discarded too and the whileloop wont stop pulling events.

Sugestion 1:
So how about changing waitForResponse to something like what i do in my program, queuing some kind of marker, pulling all events up to that marker and requeue all events not used by waitForResponse, if still waiting for some special event, then either do it all over again or just put all events in some sort of table and requeue it after all events needed have been dealt with.

Sugestion 2:
How about a timeout for os.pullEvent() so if it returns false after a period of time I know there is no event without using some kind of marker and still being able to do something else other than waiting for events to happen.


PS: If anyone knows where to find the actual turtle-api-file on my pc, please let me know so i can test it
Cloudy #2
Posted 04 December 2012 - 03:15 AM
You can handle events yourself if you use turtle.native.blah - and receive the turtle response event. If you want a timeout for pullEvent just queue a timer - that's exactly what sleep() does.

For working with turtles parallel/coroutines are probably the best option - it isn't multi threading in the true sense of the word, but will let you handle events in two functions as if they were running side by side (even though they're actually running one after the other).

Anyway, not going to implement your suggestion since you can handle it yourself anyway.
BloodyEpi #3
Posted 04 December 2012 - 06:10 AM
oh i see, nvm then

doing it myself with turtle.native works fine, didnt expect it to be that easy

thx
ChunLing #4
Posted 04 December 2012 - 07:12 AM
Yeah, they back up the java turtle functions before putting that wait for response in there. It's funny because I wrote an API and I ended up borked by it loading before the turtle functions got that, so the API was linked to the java functions and it made the turtles get a too long without yielding error a lot. So…kinda opposite problem there.

The turtle api file is in lua/rom/apis/turtle in your zip. You don't have to edit the zip, though, you can replicate that folder outside to put stuff in rom. But I do have the whole thing extracted so I can go look at things when I want.