Jump to content




Event queue


8 replies to this topic

#1 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 07 December 2013 - 08:50 AM

Hello there.

I would like to ask a question: is there a way to queue event so I can use it after some loops and functions because I have a turtle that is mining and I need him to listen for rednet messages at the same time, so for example if the message is "test123" the turtle stops mining.

Computer code:
rednet.broadcast("test123")

Turtle code:
-- If I put os.pullEvent("rednet_message") here, it just stays at the spot and waits for the message.
for i=1,60 do
  turtle.digDown()
  turtle.down()
-- I need to check here if the message sent to turtle was test123 if it is, then stop.
end

I hope you will understand what I want to accomplish here because English is not my native language :P.

Edited by exploder, 07 December 2013 - 08:52 AM.


#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 07 December 2013 - 09:04 AM

Well I think parallel will work for this or you could simply use a while loop instead with a timer

Here's how you can do it with a while loop using a timer
local i = 1
local timer = os.startTimer(.8) -- Starting the timer
while i < 61 do
	local evt, id, msg = os.pullEvent()
	if evt == "timer" then	-- Checking if the event that was pulled was the timer
		if id == timer then	-- Checking which timer it was
			turtle.digDown()
			turtle.down()
			timer = os.startTimer(.8) -- Restarting the timer
            i = i + 1
		end
	elseif evt == "rednet_message"
		-- Handle rednet message here
	end
end
The reason there is a timer is so the loop won't hang.

Edited by Hellkid98, 07 December 2013 - 09:07 AM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 December 2013 - 04:58 PM

The timer isn't an option. The turtle takes time to move, and while it waits for the special event signifying those movements are complete, it pulls all other events that comes in and discards them. That means your own os.pullEvent() call has a fair chance of "missing" the rednet and timer events because the turtle functions already ate them.

Hence the parallel API wins out. Two functions running at once, each gets a copy of everything that runs through the event queue. This means that the turtle functions can run in one and won't affect your attempts to retrieve rednet messages in the other.

Eg:

rednet.open("right")

local function getMessages()
  local myMessage
  while true do
    myMessage = {rednet.receive()}
    if myMessage[2] == "test123" then break end
  end
end

local function descend()
  for i=1,60 do
    turtle.digDown()
    turtle.down()
  end
end

parallel.waitForAny(descend, getMessages)  -- Run the two functions at once until either finishes.

rednet.close()


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 December 2013 - 07:31 PM

The timer solution that Hellkid98 posted could however be implemented if you used the turtle.native calls and listened for the turtle_response event yourself... but definitely go with what Bomb Bloke was saying, its much simpler!

#5 Molinko

  • Members
  • 54 posts

Posted 08 December 2013 - 04:14 PM

View Posttheoriginalbit, on 07 December 2013 - 07:31 PM, said:

The timer solution that Hellkid98 posted could however be implemented if you used the turtle.native calls and listened for the turtle_response event yourself... but definitely go with what Bomb Bloke was saying, its much simpler!

could you elaborate on this "turtle_response" event fur me. its this only exposed when you are using turtle.native? audio what is the difference between turtle and turtle.native


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 December 2013 - 05:01 PM

View PostMolinko, on 08 December 2013 - 04:14 PM, said:

could you elaborate on this "turtle_response" event for me. its this only exposed when you are using turtle.native?
it is definitely not only exposed when using turtle.native, it actually occurs each time you invoke a turtle function, the reason that you never see it is because each time a non-native turtle function is invoked it will wait for that event, meaning that you never get to see it. Basically though a turtle_response event is a turtle notifying you that it has completed an action that you've told it to. Each time you invoke a turtle.native function it will return you a unique id; when a turtle_response event fires it will also provide you with an id. If these two ids match then that means the particular command has completed, however if the turtle_response event returns -1 as the id it means that the task you're getting it to do has failed.

code example

View PostMolinko, on 08 December 2013 - 04:14 PM, said:

also what is the difference between turtle and turtle.native
turtle.native is the Java-side code that actually makes the turtle move and such. turtle is the Lua-side code that implements the wrapper for the turtle_response event (see snippet below)

relevant turtle api code

Edited by theoriginalbit, 08 December 2013 - 05:01 PM.


#7 Molinko

  • Members
  • 54 posts

Posted 08 December 2013 - 11:13 PM

Thank you Original Bit. That was some very handy info. Sorry for my horrible typing earlier, android swype can really suck sometimes..

If its not too much how is the native in term.native relevant or useful? Is there also a wait and response involved? Is this relevant to term.redirect()??

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 December 2013 - 02:33 AM

View PostMolinko, on 08 December 2013 - 11:13 PM, said:

If its not too much how is the native in term.native relevant or useful? Is there also a wait and response involved? Is this relevant to term.redirect()??
Its the same deal again, term.native is the Java-side implementation, term is the Lua-side implementation. One of the reasons for this is to allow for the term.redirect. Basically what it does is instead of consistently invoking the same functions, it will invoke the ones that are on the top of the redirect stack (see code example 1 for relevant code from the term api). This allows you to redirect the terminal to other objects, such as monitors, and have the terminal calls on them (see code example 2).

Code Example 1

Code Example 2

Edited by theoriginalbit, 09 December 2013 - 02:34 AM.


#9 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 09 December 2013 - 04:25 PM

Got my turtle to do what I wanted, thanks everyone for posing and helping.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users