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

Passive rednet.receive()

Started by Wandering_Magi, 27 August 2016 - 01:39 AM
Wandering_Magi #1
Posted 27 August 2016 - 03:39 AM
The idea is that I'm going to have a central computer running a receive process. Where it just picks up all of the rednet messages directed at it and stores those inputs as variables then parses the info and spits them out to a screen with a reasonable refresh rate. I'm not sure how to handle this as it will be receiving several messages at different intervals. Some things, like my turbine farm, have relatively uniform info, so will update only once per minute. But if something's gone wrong or it's restarting then I want it to update every 0.1 seconds. I've tried this a few times but I can't get around rednet.receive() just stopping the script in it's tracks over and over again.

Just a side note, I would say I'm at best apprentice level at programming in lua. I know enough to look up stuff and beat my way through, but I'm far from knowledgeable or efficient.

Basic idea,

Computer 0

local mon = peripheral.wrap("top")
rednet.open("right")
local id = nil
local msg = {}
while true do
	 mon.clear()
	 id, rmsg = rednet.receive()
	 msg[id] = rmsg
	 for i=1,#msg do
		  mon.setCursorPos(1,i)
		  mon.write(msg[i])
	 end
	 sleep(0.1)
end

Computers 1, 2 & 3…etc

local wireless = peripheral.wrap("top")
wireless.open()
local n=0
while true do
	 rednet.broadcast(0, n)
	 n = n+1
	 sleep(0.5) --Sleep timer is arbitrary. The idea is for Computer 0 to just update when it receives a msg.
end

Yes I know this will wreak havoc in actual use. But right now it's in a test world and I just want to get it working. In actual use I'll be utilizing protocols and ID checking to get things where they need to be. The catch is, yes, the screen refreshes every 0.1 seconds, but it will only update the screen when it receives a msg. For 0.1 seconds and then it's clear again. I thought storing the msg in a table outside the while loop would fix it, but it didn't. Any ideas?
TheRockettek #2
Posted 28 August 2016 - 01:01 AM
Lear the screen after its recieved a new message instead of before it waits for a message so it isnt cleared until it gets a message
KingofGamesYami #3
Posted 28 August 2016 - 02:35 AM
You could use a generic event loop.


while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "rednet_message" then
    --#update stuff
  end
  --#do other stuff here
end