6 posts
Posted 19 December 2013 - 03:28 PM
Hi i want to use openperipheral glasses as a way to control my entire base the only way i can get the messages out of the bridge is with os.pullEvent(), this pauses the program and i dont want that as i would like live data streamed to the glasses too.
i am trying to split the program and input into two functions and the parallel them to run.
i want to know why this is crashing and if there is another way to do this?
glass=peripheral.wrap("top")
function program()
while true do
if msg == "unlockcontrolroom" then
write("log"+os.time()+"Control room")
rs.setOutput("left",true)
os.sleep(5)
rs.setOutput("left",false)
msg=0
end
end
end
function input()
event, mesage, id1, id2 = os.pullEvent()
if event == "chat_command" then
if message == "control room" then
msg = "unlockcontrolroom"
end
end
end
parallel.waitForAll(input,program)
323 posts
Location
Boston, MA
Posted 19 December 2013 - 05:59 PM
One problem I see is that you didn't define "msg" before you started using it, in line 4.
7083 posts
Location
Tasmania (AU)
Posted 19 December 2013 - 06:59 PM
That doesn't really matter. What DOES matter is that until "msg" specifically equals "unlockcontrolroom", the program() function runs an infinite, uninterrupted loop. Since it doesn't yield, the program effectively stalls until ComputerCraft notices the hang and kills the script. If you instead had it sleep a moment when "msg" is
not "unlockcontrolroom", it'd be ok.
Assuming you don't need to use functions that interfere with the event queue or something (eg "read()"), I'd not bother with the parallel API and instead run everything through events directly. Instead of using a five second sleep for eg, start a
timer and wait for the corresponding
event to come back.
882 posts
Location
Behind you.
Posted 19 December 2013 - 07:13 PM
while true do
os.startTimer(1) --Amount of time between printing!
printStuff()
a, b, c, d, e, f = os.pullEvent()
if a == "chat_command" then
--check your other stuff here
end
end
That's basically what Bomb Bloke's saying I think.
Pulls an event, event can either be a timer or a chat command.
Either way, it's pulled.
Now, check if it's a chat command or timer.
It's a chat command? Okay, do this!
Aand stop script
32 posts
Posted 19 December 2013 - 07:34 PM
An alternate way to do this is to run two computers connected to the same Glasses Bridge. One can handle input, one output.
6 posts
Posted 19 December 2013 - 08:04 PM
ok thanks guys it was a "too long without yielding error" i was just not seeing it because i was out of the pc and using the glasses to use the system i will share the code when its finished in the programs section.
also in lua 99%of the time an un initialized variable is just seen as a 0 so i was pretty safe on that one.
Edited on 19 December 2013 - 07:06 PM
882 posts
Location
Behind you.
Posted 19 December 2013 - 08:14 PM
too long without yielding means that you're not yielding the script and it's taking up too much processor stuffs.
An alternate way to do this is to run two computers connected to the same Glasses Bridge. One can handle input, one output.
Nope.
Glasses only pair with one bridge.
8543 posts
Posted 19 December 2013 - 08:59 PM
That's not what he's saying. He's saying to connect two computers to the same bridge. I'm not really sure how to clarify it further than that.