Last week I asked for help about adding a user controlled "break" from the main sleep function. It says "Press X to cancel." and the solution provided works beautifully. Now I would like to improve the rest of this thing so that the user can cancel out of any action. Upon doing so, the turtle would return from where ever it is and then end the program (er… actually I also have a menu UI system. If possible Id like it to drop to the menu…) The problem is that some of the turtle actions are buried 4 and 5 functions deep. A simple "Press x to cancel." would have to break out of multiple functions.
I can provide some portions of the code below to give an idea of what I mean, but this is just an example. It is not perfect since it is snippets only an it would require the full code to work properly, if at all. I wont post everything here because of its length… but I will probobly provide the finished program for the community when it is done.
--Functions
function forward(num)
while num > 0 do
CheckFuel()
if turtle.forward() then
CollectItems() --a function to call turtle.suck()
xpos=xpos+1
num = num-1
CountItems() --a function to find out if the turtle has full inv
else
turtle.dig()
end
end
end
function go(x,y)
setdir(2) --a function to rotate the turtle
forward(x-xpos)
end
function scanforitems(num)
term.clear()
term.setCursorPos(1,1)
print("Status - Looking for Items... ")
--print("Press 'x' to cancel.") --This is the part I want to add
scan = true
go((spacing*num),ypos)
scan = false
end
function sleepmode(num)
timer = os.startTimer(1)
repeat
term.clear()
term.setCursorPos(1,1)
print("Status - Sleeping... "..num.."sec")
print("Press 'x' to cancel.")
local id, p1 = os.pullEvent()
if id == "key" and p1 == 45 then return false
elseif id == "timer" and p1 == timer then
num = num-1
timer = os.startTimer(1)
end
until num < 0
end
--Main Code
while true do
for i = 1,ycount do
scanforitems(i)
end
home() --this function returns the turtle to his home chest
organize() --this function matches inv items to the "safe items" and discards the rest
if sleepmode(duration) == false then
term.clear()
term.setCursorPos(1,1)
break
end
end
(pasting the code in the editor messed up some of the tabs) :unsure:/>Please notice the sleepmode function above. It is posted in its entirety. That is an exaple of how I "think" I want the rest of the user cancelations to work. (if possible?)
Another thing to note, I do not wish to use rednet for this. I feel that the addition of rednet commands could potentially double the size of this code and introduce many more complications. Im looking for a simple and elegant way to do this. Maybe I will play with rednet in a different project. :P/>
Thanks for any help and advice. And sorry for the lengthyness… (it was hard to decide which portions to include. "too little"? "too much"?)