Jump to content




Rednet receive/broadcast program help


  • You cannot reply to this topic
6 replies to this topic

#1 metron80

  • Members
  • 55 posts
  • LocationSolitude, Skyrim

Posted 22 June 2014 - 02: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

The top() function just keeps the top text always there, send() is self-explanatory.
Any help is appreciated. Thanks! :)

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 22 June 2014 - 03: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.

#3 metron80

  • Members
  • 55 posts
  • LocationSolitude, Skyrim

Posted 22 June 2014 - 04:12 AM

View PostBomb Bloke, on 22 June 2014 - 03:32 AM, said:

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?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 22 June 2014 - 04:56 AM

View Postmetron80, on 22 June 2014 - 04:12 AM, said:

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.

View Postmetron80, on 22 June 2014 - 04:12 AM, said:

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".

#5 metron80

  • Members
  • 55 posts
  • LocationSolitude, Skyrim

Posted 22 June 2014 - 06:47 PM

View PostBomb Bloke, on 22 June 2014 - 04:56 AM, said:

View Postmetron80, on 22 June 2014 - 04:12 AM, said:

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.

View PostBomb Bloke' time stamp='1403413007 said:

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 by metron80, 22 June 2014 - 06:55 PM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 22 June 2014 - 10:49 PM

View Postmetron80, on 22 June 2014 - 06:47 PM, said:

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


#7 metron80

  • Members
  • 55 posts
  • LocationSolitude, Skyrim

Posted 23 June 2014 - 05:49 AM

View PostBomb Bloke, on 22 June 2014 - 10:49 PM, said:

View Postmetron80, on 22 June 2014 - 06:47 PM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users