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

os.pullEvent ()

Started by kasmurdanto, 28 October 2012 - 07:48 AM
kasmurdanto #1
Posted 28 October 2012 - 08:48 AM
hi everyone ! I began to learn the lua language since wednesday , so I am really a beginner .
I have luckily some notions in C and C++ so it's not really hard for me , but i try to make a simple program , a counter with the redstone
input and which output a signal only if the count is above 0 .
That is pretty simple , but i want to check if my program really works by printing a message if the user type on the keyboard , whatever the key is .
To do that , i've tought of os.pullEvent() , but it actually stop the program , and since all my program is in a while true do loop , it can't work .
So , do you know an alternative to this ? I would like that the program wait for an event without "freezing" .
I hope you have understood what i meant , My english is still pretty bad …
have a Good day !
remiX #2
Posted 28 October 2012 - 08:52 AM
If I think what I think you need, you might need to use parrallel.waitForAny() or waitForAll().

Show us the code that you have now.
kasmurdanto #3
Posted 28 October 2012 - 09:13 AM
thanks for your answer !
I'm going to see what are these function , but just the name seems to be for me ! :D/>/>
here's my code :


local x = 0
local red = false

while true do
red = redstone.getInput("back")
   if red == true then
   x = x+1
   end
red = redstone.getInput("right")
   if red == true and x>0 then
   x = x-1
   redstone.setOutput("front" , true)
   end
end

ChunLing #4
Posted 28 October 2012 - 09:33 AM
The way your code is now, it looks like it moves unnecessarily fast anyway. If you want to make sure that you run this every second or so, then set a timer event just before you call os.pullEvent, so that it will pull the timer if nothing else happens in that second.
kasmurdanto #5
Posted 28 October 2012 - 09:48 AM
thank you chunling , it'a a good idea . i attempt that .
kasmurdanto #6
Posted 28 October 2012 - 09:59 AM
chungling sorry to ask you that , but after some try I don't figure out how it works … maybe can you use my code to show me ?
It would be very nice .
ChunLing #7
Posted 28 October 2012 - 11:37 AM
Okay, what we want to do is just start a timer and then pull the next event. So:
local x = 0
local red = false
local t_event = {}
repeat --I just prefer this
os.startTimer(1) --or however many seconds you like
  t_event = {os.pullEvent()} --gets all the event returns and puts them in a table
  if t_event[1] == "redstone" then
	red = redstone.getInput("back")
	if red == true then
	  x = x+1
	end
	red = redstone.getInput("right")
	if red == true and x>0 then
	  x = x-1
	  redstone.setOutput("front" , true)
	end
  elseif t_event == "key" then --do something with the key
  elseif t_event == "char" then --do something with the character
  end
until false --we can make a way to exit the loop here, but I take it we don't want to
Now, the code never goes more than a second without getting an event, and it checks the redstone inputs if and only if there is a change in their state. That means that you can also get key and character (or rednet, or whatever else) events. We don't actually use the timer event just yet, but we could if we wanted to do something in the event that the program had gone too long without any input.

Messed up the formatting on the code at first
Edited on 28 October 2012 - 10:40 AM
kasmurdanto #8
Posted 28 October 2012 - 12:16 PM
thank you A LOT ! i try that and i tell you how it works , but it seems really cool .
kasmurdanto #9
Posted 28 October 2012 - 01:11 PM
you wanna know ? I love you ! thanks a lot , it works perfectly , except the fact that you have done some little errors (maybe intentionally ?) like this :
elseif t_event == "key"

whis is in fact :

 elseif t_event[1] == "key"

thank you again and have a good day !
ChunLing #10
Posted 28 October 2012 - 06:42 PM
Oops. Not intentional, just that those parts didn't do anything anyway so I didn't check them closely as I should have.