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

Help With A Os.pullevent() Code

Started by kaioo1312, 18 November 2013 - 11:54 AM
kaioo1312 #1
Posted 18 November 2013 - 12:54 PM
I made the following code and it is going to be used for a tower that lights lights at different stages and can be stopped, auto-run, auto-run stopped etc. But on the line that follows function define() the os.pullEvent() isnt working the way i want it to.

*NOTE:*
This code isnt finnished yet so dont critisize
Spoiler

function main()
rs.setBundledOutput("back", colors.subtract( rs.getBundledOutput ("back"), colors.blue))
rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.blue))
sleep(0.2)
rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.blue))
rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.black))
sleep(0.2)
rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.black))
rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.yellow))
sleep(0.2)
rs.setBundledOutput("back", colors.subract(rs.getBundledOutput("back"), colors.yellow))
rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.red))
sleep(0.2)
rs.setBundledOutput("back", colors.subtract(rs.getBundledOutput("back"), colors.red))
rs.setBundledOutput("back", colors.combine(rs.getBundledOutput("back"), colors.grey))
sleep(0.2)
rs.setBundledOutPut("back", colors.subtract(rs.getBundledOutput("back"), colors.grey))
define()
end

function define()
os.pullEvent()
if (event==key) and (p1==16) then
fs.makeDir("stop")
elseif (event==key) and (p1==34) then
main()
elseif (event==key) and (p1==19) then
fs.makeDir("r")
main()
elseif fs.isDir("r") then os.pullEvent(1.75)
  if (event==key) then
  fs.del("r/")
  main()
  else
  main()
  end
elseif (event==key) and (p1==30) then
fs.makeDir("r")
main()
else
sleep(0)
define()
		 end
  end
define()
TheOddByte #2
Posted 18 November 2013 - 01:43 PM
Well put quotes around key

elseif evt == "key"
Edited on 18 November 2013 - 12:43 PM
Bomb Bloke #3
Posted 18 November 2013 - 04:51 PM
Better yet, store the event somewhere. Just calling "os.pullEvent()" has your program block until the event is returned, you need to specifically capture its output in some variables if you want to refer to it later in the program.

Eg,

event,p1 = os.pullEvent()
if event=="key" and p1==keys.q then

I'm also unsure as to what "os.pullEvent(1.75)" is supposed to do.

You've asked for no "criticisms", but I feel at least one other tip is in order: When a function is called, it goes on the stack until it's finished. "Calling another function", including itself again, does not count as "finishing". Eventually the stack fills up, overflows, and the program crashes (you don't have unlimited RAM to keep track of function calls).

Hence if you want the "define()" function to be repeated over and over, don't have it call itself when it's done, put this at the bottom of your program instead:

while true do define() end

If you want your "define()" function to repeat early (eg after completing the "main()" function), then don't have "main()" call "define()", have "define()" use "return" directly after calling "main()".
Edited on 18 November 2013 - 03:52 PM
kaioo1312 #4
Posted 21 November 2013 - 10:27 AM
The

os.pullEvent(1.75)
Is suppost to pull event for two seconds.and if there is no reply it will carry on but if there is it will stop
Lyqyd #5
Posted 21 November 2013 - 10:44 AM
That's not at all how os.pullEvent works. The only argument it accepts is a string to use to filter events, so that it will only return that event type or a terminate event, if the coroutine is managed properly.
Anavrins #6
Posted 21 November 2013 - 10:45 AM
The

os.pullEvent(1.75)
Is suppost to pull event for two seconds.and if there is no reply it will carry on but if there is it will stop

That's not how it works though, if you want something similar to that, you should call os.startTimer(1.75), before your pullEvent(), after 1.75 seconds it will push a "timer" event which you can deal with your "if" statement.
kaioo1312 #7
Posted 24 November 2013 - 03:55 PM
Thanks