Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
DeBrates

event.pull freezing loop workaround

Question

Hi, I'm recently new to OC and am working on porting some of my older programs from CC. I have this simple reactor program that has a few pull events. Issue I'm having is that the pull event freezes the loop until an event happens. I tried to get around this using a timer, but it doesn't seem to work the same as CC. How can I use pull events without freezing my loop? 

Full Code (Events at bottom)

 

Pull Event

  local name, _, x, y = event.pull("touch")
  if y == 1 and x < 30 and x > 18 and reactorOnline then
    r.setActive(false)
  elseif y == 1 and x < 30 and x > 21 then
    r.setActive(true)
  elseif y == 8 and x == 21 or x == 22 then
    r.setAllControlRodLevels(rodLevel - 10)
  elseif y == 8 and x == 28 or x == 29 then
    r.setAllControlRodLevels(rodLevel + 10) 
  end
  os.sleep(0.1)
end

Any help and/or OC advice is appreciated! Thanks!

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 1
  • Solution

event.pull has an optional first argument, a timeout. 

local name, _, x, y = event.pull(0.1, "touch")

if name then -- # name will be nil if event.pull(0.1, ...) times out after 0.1 sec
  if y == 1 and x < 30 and x > 18 and reactorOnline then
    r.setActive(false)
  elseif y == 1 and x < 30 and x > 21 then
    r.setActive(true)
  elseif y == 8 and x == 21 or x == 22 then
    r.setAllControlRodLevels(rodLevel - 10)
  elseif y == 8 and x == 28 or x == 29 then
    r.setAllControlRodLevels(rodLevel + 10) 
  end
end

 

Link to post
Share on other sites
  • 0
3 hours ago, Molinko said:

event.pull has an optional first argument, a timeout. 


local name, _, x, y = event.pull(0.1, "touch")

if name then -- # name will be nil if event.pull(0.1, ...) times out after 0.1 sec
  if y == 1 and x < 30 and x > 18 and reactorOnline then
    r.setActive(false)
  elseif y == 1 and x < 30 and x > 21 then
    r.setActive(true)
  elseif y == 8 and x == 21 or x == 22 then
    r.setAllControlRodLevels(rodLevel - 10)
  elseif y == 8 and x == 28 or x == 29 then
    r.setAllControlRodLevels(rodLevel + 10) 
  end
end

 

Did not know about that extra argument! Works like a charm! Thanks!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.