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:
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