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

Running two functions at once

Started by JoeCarter14, 30 December 2012 - 10:58 AM
JoeCarter14 #1
Posted 30 December 2012 - 11:58 AM
Hi, I was wondering if anyone could help me out. I was playing around with red net and I realized there was no built in way to detect which devices are in range, making it hard to work with red net to send commands, messages etc. between computers.

I soon came up with a simple system of sending a ping message from one computer and having the receiving computers send a ping back when they received it. Here's what I came up with so far:

the client "Operating system" :

function returnPing()
event, id, text = os.pullEvent()
if event == "rednet_message" then
  if text == "JOENET PING" then
   rednet.send(id, "JOENET PING")
   print("ping received")
  end
end
end
function sendMessage()
print("Enter text to broadcast:")
local message = io.read()
rednet.broadcast(message)
end

while true do
parallel.waitForAll(sendMessage, returnPing)
end

ping script:

local replys = {}
local timerOn = true
function addToArray(id)
replys[#replys + 1] = id
end
function printReplys()
for i=1,#replys do
  print(replys[i])
end
end

function ping()
rednet.broadcast("JOENET PING")
--print("ping sent to all computers in range, wait for responce")
end

ping()
os.startTimer(1)
while timerOn do
event, id, param2 = os.pullEvent()
if event == "rednet_message" then
  if param2 == "JOENET PING" then
   addToArray(tostring(id))
  end
end

if event == "timer" then
  timerOn = false
end
end
printReplys()

When I run ping the first time it works fine and returns the id's of any pc running JoeNet in range. But the second time I run it, It ignores the ping requests.

What I think is happening is the returnPing() function is returnign a value, thus stopping the parallel from running. But I'm not sure.

If anyone has help or Suggestions I'd appreciate it :)/>
Luanub #2
Posted 30 December 2012 - 12:08 PM
try parallel.waitForAny() instead of waitForAll. To give some details with waitForAll it needs both functions to run before it will let one of them be ran a second time.
JoeCarter14 #3
Posted 30 December 2012 - 12:19 PM
Thankyou for replying, using waitForAny() stops my ping issue but it creates another problem.

Now my sendMessage() function dosn't work how it did before, making it impossible to type messages, could anyone show me a better way of doing the sendMessage() function? because for what I have planned its important for my ping script to work.
Lyqyd #4
Posted 30 December 2012 - 12:19 PM
try parallel.waitForAny() instead of waitForAll. To give some details with waitForAll it needs both functions to run before it will let one of them be ran a second time.

That's not at all how it works. waitForAny stops all of them running when any of the functions end, waitForAll will run until all functions have ended.
Luanub #5
Posted 30 December 2012 - 12:27 PM
try parallel.waitForAny() instead of waitForAll. To give some details with waitForAll it needs both functions to run before it will let one of them be ran a second time.

That's not at all how it works. waitForAny stops all of them running when any of the functions end, waitForAll will run until all functions have ended.
Yeah that is what i meant just didn't explain it well thanks for the better explanation.

Thankyou for replying, using waitForAny() stops my ping issue but it creates another problem.

Now my sendMessage() function dosn't work how it did before, making it impossible to type messages, could anyone show me a better way of doing the sendMessage() function? because for what I have planned its important for my ping script to work.

You need to filter the events in your first function, without it being filtered its picking up the key events.

function returnPing()
event, id, text = os.pullEvent("rednet_message")
  if text == "JOENET PING" then
   rednet.send(id, "JOENET PING")
   print("ping received")
  end
end
function sendMessage()
print("Enter text to broadcast:")
local message = read()
rednet.broadcast(message)
end

while true do
parallel.waitForAny(sendMessage, returnPing)
end

You could also replace the os.pullEvent() with a rednet.receive()
Edited on 30 December 2012 - 11:28 AM
JoeCarter14 #6
Posted 31 December 2012 - 02:06 AM
What do you mean by filter? Do you mean catching one of the key events and just adding to a string for the sending message?
Luanub #7
Posted 31 December 2012 - 10:11 AM
Send the event type that you want to catch as an argument to os.pullEvent(). The code I updated in my last post has it set to catch only rednet_message events so it will just ignore the key events.

event, id, text = os.pullEvent("rednet_message")

remiX #8
Posted 31 December 2012 - 10:21 AM
Have you opened the modem?