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
local currentId = turtle.native.forward()
while true do
local event = {os.pullEvent()} --# we could do use a filter, os.pullEvent("turtle_response"), but we don't so you can see you can check for other events
if event[1] == "turtle_response" then
if event[2] == currentId then
return print("Our turtle task has completed!")
elseif event[2] == -1 then
return print("Our turtle task has failed!")
else
print("Unknown turtle task completion! This shouldn't happen!")
end
else
print("Another event has happened that we don't need to respond to in this code")
end
end
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
native = turtle.native or turtle
local function waitForResponse( _id )
local event, responseID, success
while event ~= "turtle_response" or responseID ~= _id do
event, responseID, success = os.pullEvent( "turtle_response" )
end
return success
end
local function wrap( _sCommand )
return function( ... )
local id = native[_sCommand]( ... )
if id == -1 then
return false
end
return waitForResponse( id )
end
end
-- Wrap standard commands
local turtle = {}
for k,v in pairs( native ) do
if type( k ) == "string" and type( v ) == "function" then
if turtle[k] == nil then
turtle[k] = wrap( k )
end
end
end