PHOBOSS98 0 Posted July 19, 2018 Share Posted July 19, 2018 can't seem to make this work: local component=require("component") local event=require("event") m=component.modem m.open(001) local _,_,_,_,_,cmd=event.pull("modem") repeat print("hello") until cmd== stop ...help is very much appreciated Quote Link to post Share on other sites
0 Molinko 43 Posted July 19, 2018 Share Posted July 19, 2018 Last line. stop should be "stop" Quote Link to post Share on other sites
0 PHOBOSS98 0 Posted July 19, 2018 Author Share Posted July 19, 2018 oh right... still doesn't work it just keeps going Quote Link to post Share on other sites
0 Molinko 43 Posted July 20, 2018 Share Posted July 20, 2018 (edited) local component = require("component") local event = require("event") local m = component.modem -- # make local too m.open(1) -- # 001 ?? repeat -- # run in the loop to repeat reception of 'cmd' local cmd = select(6, event.pull("modem_message")) -- # this is cleaner until cmd == "stop" Edited July 20, 2018 by Molinko Quote Link to post Share on other sites
0 PHOBOSS98 0 Posted July 20, 2018 Author Share Posted July 20, 2018 I slipped in a 'print("hello") in the loop... sorry m8 it didn't budge for me. It just ended the program. Quote Link to post Share on other sites
0 Molinko 43 Posted July 20, 2018 Share Posted July 20, 2018 I think I need you to ask your question again.. I thought you needed the loop to stop on a "modem_message" event, but I guess you're really trying to stop a loop when a key is pressed? Quote Link to post Share on other sites
0 PHOBOSS98 0 Posted July 20, 2018 Author Share Posted July 20, 2018 I do need a loop that can stop by a modem event.. but you see when I tried the code you sent me (with a few changes): local component = require("component") local event = require("event") local m = component.modem m.open(1) repeat print("hello") --#the thing local cmd = select(6, event.pull("modem_message")) until cmd == "stop" it did end the program when I sent it the message but it doesn't print it repeatedly it just does this: and so I tried and tested something and changed the code: local component = require("component") local event = require("event") local m = component.modem m.open(1) repeat print("hello") local cmd = select(6, event.pull("modem_message")) print("hi")--#the new thing until cmd == "stop" and ended up with this: (note: i didn't end the program this time) it seems that the loop stops at local cmd = select(6, event.pull("modem_message")) but still thinks it's running since when I broadcasted the stop command it did this: Quote Link to post Share on other sites
0 Fingercomp 37 Posted July 20, 2018 Share Posted July 20, 2018 Well, it executes exactly what's written in the code, and is the expected behavior. event.pull("modem_message") stops the program (more specifically, the thread) until it gets the modem_message event. You may want to set a timeout, so that the pull would stop early — returning nil in such case; for example, event.pull(1, "modem_message") sets the timeout to 1 second. Let's step through the program line by line. The first 5 lines don't cause any difficulties, so we skip them. There's the repeat-until loop, and the first iteration is run unconditionally. First, the string "hello" is printed to the screen. After that there's the event.pull line, which (as I said before) stops the program until it gets the modem message. The execution now does not go any further. The program is running — it just waits for a modem message now. You broadcast a stop message; the program gets it, and goes on. select is called, and the actual message is set to the cmd variable. Only then "hi" is written. As the condition is satisfied, the loop stops, and the program exits. if this is not what you wanted, please clarify the intended behavior. Quote Link to post Share on other sites
0 PHOBOSS98 0 Posted July 20, 2018 Author Share Posted July 20, 2018 yeah... I want it to keep printing hello repeatedly until I tell it to stop without having to reboot Quote Link to post Share on other sites
0 Molinko 43 Posted July 21, 2018 Share Posted July 21, 2018 local component = require("component") local event = require("event") local m = component.modem m.open(1) repeat print("Hi") -- # seconds, event name local cmd = select(6, event.pull(1, "modem_message")) until cmd == "stop" https://ocdoc.cil.li/api:event Quote Link to post Share on other sites
0 PHOBOSS98 0 Posted July 21, 2018 Author Share Posted July 21, 2018 doh, Im so dumb, thanks m8s been a great help XD Quote Link to post Share on other sites
can't seem to make this work:
Link to post
Share on other sites