Jump to content




Alternative to infinite loops?


  • You cannot reply to this topic
2 replies to this topic

#1 inkychris

  • New Members
  • 1 posts

Posted 05 January 2015 - 10:43 PM

I have built a lighthouse made from 8 lamp blocks in a rectangle (but at 45 degrees) where 2 of the 8 that are next to each other light up and this moves round all of them every 0.3 seconds. There is also a beacon on top that flashes every second. I wanted several options such that it could be OFF, Just Beacon, Just main light, or both Beacon and main light.

In addition to this, I also wanted a text command to be used to stop the function/restart it etc. My code works but to me doesn't seem very streamlined but I am very new to Lua. My main question is the following:

Is there a way of waiting until a variable changes before calling a function or is the only way to do as I have with a While True loop with sleep(0) in it?

This is to help me learn Lua more than anything. As I said, the code works but if anyone has any better ideas for how to do what I want that didn't involve what I believe to be calling the setBundledOutput function over and over very fast every second?

Thank you very much for any help or advice you can give,
Chris.

The code is as follows:

function clear()
term.setBackgroundColor(colours.black)
term.clear()
term.setCursorPos(1,1)
end
function beacon()
while true do
  b = 8192
  sleep(1)
  b = 0
  sleep(1)
end
end
function lighthouse()
local t = {9,12,6,34,96,80,32784,32769}
while true do
  for i=1,#t do
  h = t[i]
  sleep(0.3)
  end
end
end
function one()
while true do
  redstone.setBundledOutput("back", B)/>/>/>/>
  sleep(0)
end
end
function two()
while true do
  redstone.setBundledOutput("back", h)
  sleep(0)
end
end
function three()
while true do
  redstone.setBundledOutput("back", b+h)
  sleep(0)
end
end
function input()
while true do
local p = read()
  if p~='' then
   redstone.setBundledOutput("back", 0)
   break
  end
end
end

--MAIN BODY
while true do
print("Select an option: ")
local r=read()

if r=='0' then
  redstone.setBundledOutput("back", 0)
elseif r=='1' then
  parallel.waitForAny(input, one, beacon)
elseif r=='2' then
  parallel.waitForAny(input, lighthouse, two)
elseif r=='3' then
  parallel.waitForAny(input, beacon, lighthouse, three)
elseif r~='0' or '1' or '2' or '3' then
  redstone.setBundledOutput("back", 0)
  break
end

end


#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 07 January 2015 - 08:57 PM

Well the repeat loop is always an option
local foo
repeat
    foo = read()
until foo == "bar"
print( foo )


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 08 January 2015 - 03:45 AM

View Postinkychris, on 05 January 2015 - 10:43 PM, said:

Is there a way of waiting until a variable changes before calling a function or is the only way to do as I have with a While True loop with sleep(0) in it?

If I want to do something along these lines, I'll often consider having the loop call os.pullEvent("myCustomEvent"). It'll then sit there yielding until such time as I call os.queueEvent("myCustomEvent") somewhere else in the code.

That said, in this case I'd do away with all the extra functions and loops and run all the logic in one place, using timers:

Spoiler






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users