This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kraethi's profile picture

Issues with repeating outputs and os.pullEvent()

Started by Kraethi, 30 April 2014 - 02:51 AM
Kraethi #1
Posted 30 April 2014 - 04:51 AM
I'm trying to write a program to run a message from another computer as a command.
I want the program to return "success" if the program was found and run and "failure" if it was not.
With the following structure, however, the program will spam "FAILURE" indefinitely.


rednet.open("top")
while true do
event, id, text = os.pullEvent()
if event == "rednet_message" and id == 1 then
  if fs.exists(text) then
   shell.run(text)
   rednet.send(1, "SUCCESS")
  else rednet.send(1, "FAILURE")
  end
end
end

I know there has to be some issue there with the end of the loop, but I can't figure it out. Should there be a break in there, or more ends inside the loop? Any help would be appreciated. After it sends the message back to computer 1 I would, of course, like the loop to reset. I know this is a stupid question, but I can't figure it out.
CometWolf #2
Posted 30 April 2014 - 05:28 AM
Seems to me it should work fine. What exactly are you sending to it?
Dog #3
Posted 30 April 2014 - 05:38 AM
Without seeing the 'sending' code it's hard to say what's going on, but I agree with CometWolf that it looks like it should work fine. Are you sure that whatever the variable 'text' is holding actually exists?
Edited on 30 April 2014 - 03:39 AM
Lyqyd #4
Posted 30 April 2014 - 05:43 AM
It comes from the event pull above. One thing I would suspect is if OP is trying to run anything from ROM. That won't work due to fs.exists needing absolute paths and shell.run not needing them, unless OP is actually specifying absolute paths.
Kraethi #5
Posted 30 April 2014 - 05:50 AM
The text of the messages sent to the programs is just names of programs, things like "program1" "program2".
And I've tested to make sure the issue is with this program by adding a print("foo") after line 8. It will print that infinitely as well when the program receives a message.


rednet.open("back")
while true do
print("Execute Instructions")
local input = read()
if input == "Lockdown" then
  shell.run("Lockdown")
break
else
  while true do
   rednet.send(2, input)
   local id,message = rednet.receive()
   print(message)
  end
end
end

This is the sending program. Is it possible that the final printed message is recognized as the input, looping between the programs forever? And Dog, I've been hesitant to actually try a program I know works, because if it spams it a million times I don't want something terrible to happen. I'll write up a test program and try it out.

I'm not sure I can respond precisely to what you're saying, Lyqyd, but the contents of the messages are (if I understand what an absolute path is) not absolute paths, in that when I had at least some part of this program working I could send the message "program1" from computer 1 and this computer would execute program1, doing the same thing as typing program1 from the command prompt.

Is there anything you could reccomend to fix this?
Dog #6
Posted 30 April 2014 - 05:50 AM
It looks like your sending program takes your input and spams it repeatedly in your second loop. If it's sending info that can't be run then your receiving program will spam it's failure message. You really don't need the second loop - eliminating that alone might fix the problem.
Kraethi #7
Posted 30 April 2014 - 05:53 AM
I'll take out that loop and give it a shot.

Removing the loop looks like it did the trick, and correctly returns both "FAILURE" and "SUCCESS". Thanks, everyone!
Edited on 30 April 2014 - 03:54 AM
Dog #8
Posted 30 April 2014 - 05:54 AM
Happy to be of service :)/>