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

Rednet receive/broadcast program help

Started by metron80, 22 June 2014 - 01:33 AM
metron80 #1
Posted 22 June 2014 - 03:33 AM
Okay, so I have been working on a rednet listener that also lets you broadcast. The problem is, when it first starts up, it doesn't receive the first message, but it does receive the rest. When I broadcast, after it broadcasts the message, sometimes it doesn't and sometimes it does receive the next. Here's the code:

Spoiler
rednet.open("top")
term.clear()
term.setCursorPos(1,4)

function send()
oldx, oldy = term.getCursorPos()
term.setCursorPos(1,19)
input = read()
rednet.broadcast(input)
term.setCursorPos(1,18)
term.clearLine()
term.setCursorPos(oldx,oldy)
end

function top()
oldx, oldy = term.getCursorPos()
term.setCursorPos(1,1)
term.clearLine()
term.setCursorPos(1,2)
term.clearLine()
term.setCursorPos(1,3)
term.clearLine()
term.setCursorPos(1,1)
print("Rednet Broadcast Receiver		 Press \"S\" to Send")
print("Monitering All Rednet Since Day 316.")
print("Format: \"day | time | message | id\"")
print("===================================================")
term.setCursorPos(1,19)
term.clearLine()
term.setCursorPos(oldx,oldy)
end

while true do
  term.setTextColor(colors.white)
  top()
  term.setTextColor(colors.blue)
  event, param1, param2, param3 = os.pullEvent()
  if event == "rednet_message" then
	local day = os.day()
	local time = textutils.formatTime(os.time(), true)
	printing = day.." | "..time.." | \""..param2.."\" | "..param1
	print(printing)
  end
  if event == "char" and  param1 == "s" then
	send()
  end
end

The top() function just keeps the top text always there, send() is self-explanatory.
Any help is appreciated. Thanks! :)/>
Bomb Bloke #2
Posted 22 June 2014 - 04:32 AM
Your use of read() is problematic - that function operates by pulling events over and over (in order to collect characters the user types). While running, it'll pull any rednet messages sent to your system out of the event queue, and simply discard them.

I reckon this thread holds enough information to solve that problem for you, but by all means ask if you're unclear as to how it applies.

You print the first message on row 4, which is then overwritten by your top() function. Change line 3 to move the cursor to row 5.
metron80 #3
Posted 22 June 2014 - 05:12 AM
You print the first message on row 4, which is then overwritten by your top() function. Change line 3 to move the cursor to row 5.

It doesn't get overwritten, the next message is the next line down, and when it gets full it scrolls above the top() function.

But anyway, I know how that thread applies, but I don't know how to apply it to my program, if that makes sense. Could you please give me an example?
Bomb Bloke #4
Posted 22 June 2014 - 05:56 AM
It doesn't get overwritten, the next message is the next line down, and when it gets full it scrolls above the top() function.

Count how many lines the top() function prints. Then take another look at which line you're writing your first message on.

But anyway, I know how that thread applies, but I don't know how to apply it to my program, if that makes sense. Could you please give me an example?

You won't want to implement this as-is, but:

local function getIncoming()
	while true do
		local event, param1, param2, param3 = os.pullEvent()
		
		local day = os.day()
		local time = textutils.formatTime(os.time(), true)
		printing = day.." | "..time.." | \""..param2.."\" | "..param1
		
		local oldx, oldy = term.getCursorPos()
		print(printing)
		term.setCursorPos(oldx,oldy)
	end
end

local function getOutgoing()
	local oldx, oldy = term.getCursorPos()
	term.setCursorPos(1,19)
	input = read()
	rednet.broadcast(input)
	term.setCursorPos(1,18)
	term.clearLine()
	term.setCursorPos(oldx,oldy)
end

parallel.waitForAny(getOutgoing,getIncoming)

Reading this will hopefully make you start to wonder why your user has to hit S to start typing. The answer is "they don't".
metron80 #5
Posted 22 June 2014 - 07:47 PM
It doesn't get overwritten, the next message is the next line down, and when it gets full it scrolls above the top() function.

Count how many lines the top() function prints. Then take another look at which line you're writing your first message on.

Well it prints 3 lines. The messages are printed on the 4th line and below.

You won't want to implement this as-is, but:

local function getIncoming()
	while true do
		local event, param1, param2, param3 = os.pullEvent()
		
		local day = os.day()
		local time = textutils.formatTime(os.time(), true)
		printing = day.." | "..time.." | \""..param2.."\" | "..param1
		
		local oldx, oldy = term.getCursorPos()
		print(printing)
		term.setCursorPos(oldx,oldy)
	end
end

local function getOutgoing()
	local oldx, oldy = term.getCursorPos()
	term.setCursorPos(1,19)
	input = read()
	rednet.broadcast(input)
	term.setCursorPos(1,18)
	term.clearLine()
	term.setCursorPos(oldx,oldy)
end

parallel.waitForAny(getOutgoing,getIncoming)

Reading this will hopefully make you start to wonder why your user has to hit S to start typing. The answer is "they don't".

Well it's mostly a listener, the broadcast is an extra function. So I will probably have it make you press 'S'. But anyway thanks for the help.
Edited on 22 June 2014 - 05:55 PM
Bomb Bloke #6
Posted 22 June 2014 - 11:49 PM
Well it prints 3 lines. The messages are printed on the 4th line and below.

You don't seem to be grasping the concept of actually going back and counting the print statements. Let me demonstrate:

print("Rednet Broadcast Receiver                 Press \"S\" to Send")  -- 1
print("Monitering All Rednet Since Day 316.")                           -- 2
print("Format: \"day | time | message | id\"")                          -- 3
print("===================================================")            -- 4
metron80 #7
Posted 23 June 2014 - 06:49 AM
Well it prints 3 lines. The messages are printed on the 4th line and below.

You don't seem to be grasping the concept of actually going back and counting the print statements. Let me demonstrate:

print("Rednet Broadcast Receiver				 Press \"S\" to Send")  -- 1
print("Monitering All Rednet Since Day 316.")						   -- 2
print("Format: \"day | time | message | id\"")						  -- 3
print("===================================================")			-- 4

Oh… LOL I am very tired… *facepalm* Problem fixed. But thanks for the help.