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

[Lua][Question] os.pullEvent() and If-Statement

Started by unobtanium, 11 January 2013 - 11:47 AM
unobtanium #1
Posted 11 January 2013 - 12:47 PM
Hello there,

I want to improve my Fir Wood Chopper Program i made by some freatures.
At first i have functions like this one:

local function getSaplings()
turtle.select(2)
print("Taking saplings out of the chest!")
print("If nothing happens the chest might be empty!")
while turtle.getItemCount(2) < 16 do
  turtle.drop()
  os.sleep(3)
  turtle.suck()
end
print("Succesful!")
end

It takes saplings out of a chest. This chest should refill itself (pipe) automatically. The turtle waits until it has enough saplings.

If the user use STRG+T now to terminate the program, because he cant fill up the chest at the moment and want to leave, the Turtle would stay where it is and the user has manually pick the Turtle up and replace it. This is a bit complicated because it has to be played above a furnace and it cant be placed on it while pressing Shift. :/
Instead of letting this happen i want to check if the user pressed Enter (28). Then the Turtle goes back to the start (accually just one block it has to travel) and exit the program and waiting in the main menu for input.

The problem i have, and i saw some other too is, that os.pullEvent() "stops" the program and waiting for an action happen.
Is there a way to solve this program easy? However it would be nice if someone can show me how to :P/>

Thank you
UNOBTANIUM
ChunLing #2
Posted 11 January 2013 - 02:14 PM
You can replace the sleep with a timer and an os.pullEvent() (which is basically what sleep does anyway) and add in an option for if the event pulled is the enter key.
unobtanium #3
Posted 11 January 2013 - 10:28 PM
Do you have some Code of it somewhere? or a link who made this before? i can barely understand what should going on :P/>
theoriginalbit #4
Posted 11 January 2013 - 10:30 PM

local delay = os.startTimer( 5 )

while true do
  event = { os.pullEvent() }

  if event[1] == "timer" and event[2] == delay then -- our delay timer was triggered
	-- do something
	delay = os.startTimer( 5 ) -- restart the timer
  end
end

For another example of it in use have a look in my programs at the door lock… link to programs in signature… or here http://pastebin.com/QpChdzij starts at about line 239… in this program I use it to display an error message to the screen for a few seconds before removing it…
Edited on 11 January 2013 - 09:33 PM
unobtanium #5
Posted 12 January 2013 - 12:26 AM
Okay i think now what is happening in this code :D/>
I did it like this.


local function getSaplings()
local taking = true
local delay = os.startTimer(3)
turtle.select(sapling)
print("Taking saplings out of the chest!")
print("If nothing happens the chest might be empty!")
print("Press Enter to terminate process!")
while taking == true do
  event = { os.pullEvent() }
  if event[1] == "timer" and event[2] == delay then
   turtle.suck()
   if turtle.getItemCount(fuel) < 8 then
    turtle.drop()
   else
    taking = false
   end
  end
  if event[1] == "key" and event[2] == 28 then
   print("Terminated by User!")
   turtle.turnRight()
   turtle.forward()
   turtle.Right()
   term.clear()
   term.setCursorPos(1,1)
   term.clear()
   os.shutdown()
  end
end
print("Succesful!")
end
theoriginalbit #6
Posted 12 January 2013 - 12:34 AM
as long as there are variables somewhere declaring fuel and sapling it should work once you change the turtle.Right() to something that exists (not sure what u wanted here)
also doing
while taking do
is the same as saying
while taking == true do
lastly where you have the

if event[1] == "timer" then
  -- stuff
end
if event[1] == "key" then
  -- stuff
end
make it

if event[1] == "timer" then
  -- stuff
elseif event[1] == "key" then
  --stuff
end

its better logic… that way if event = "timer" it wont check to see if its "key"… no point doing logic if you already know the answer ;)/>
unobtanium #7
Posted 12 January 2013 - 12:57 AM
Yeah, i aggre in everything.
Sometimes i can read stuff easier if its more complicated ;D
I did it like this now.
I changed back the variable, because they were the wrong ones and i accually dont need them there :P/>


Spoiler


local function getSaplings()
 local taking = true
 local delay = os.startTimer(3)
 turtle.select(2)
 turtle.drop()
 print("Taking saplings out of the chest!")
 print("If nothing happens the chest might be empty!")
 print("Press Enter to terminate process!")

 while taking == true do
  event = { os.pullEvent() }
  if event[1] == "timer" and event[2] == delay then
   turtle.suck()
   if turtle.getItemCount(2) < 16 then
    turtle.drop()
    delay = os.startTimer(3)
   else
    taking = false
   end
  elseif event[1] == "key" and event[2] == 28 then
   print("Terminated by User!")
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   term.clear()
   term.setCursorPos(1,1)
   term.clear()
   os.shutdown()
  end
 end
 print("Succesful!")
end

It works how it should <3