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

modem_message returns table instead of message

Started by resdac, 09 August 2015 - 09:56 PM
resdac #1
Posted 09 August 2015 - 11:56 PM
the problem goes as folows. have this on pc 1

rednet.open("top")
rednet.broadcast("hello")
and this on pc 2

local oldpull=os.pullEvent
os.pullEvent = os.pullEventRaw
print("receiver")
wifi = peripheral.wrap("right")
wifi.open(65535)
while true do
	local event, value1, value2, value3, value4, value5 = os.pullEvent()
	print(event)
	print(value1)
	print(value2)
	print(value3)
	print(value4)
	print(value5)
end
if i start both up the second pc gives me instead of the plain hello
on the 4th value this: table: 2690b665
the value inside the table switches with every message

why is the second pc not giving me a rednet_message instead of the modem_message
and why is the message transformed into a table?
H4X0RZ #2
Posted 10 August 2015 - 12:15 AM
1. Rednet sends "formated" messages now. They are tables containing some stuff like your message.

2. You should either add a filter to pullEvent or make an infinite loop and only react to modem_message events. Otherwise your pullEvent call will react to anything.
Bomb Bloke #3
Posted 10 August 2015 - 12:44 AM
Rather, you'd want to filter for rednet_message events, ignoring modem_message events.

local event, value1, value2, value3, value4, value5 = os.pullEvent("rednet_message")
Anavrins #4
Posted 10 August 2015 - 03:30 AM
Either that, or you could get the message with "event4.message"
H4X0RZ #5
Posted 10 August 2015 - 04:12 AM
Rather, you'd want to filter for rednet_message events, ignoring modem_message events.

local event, value1, value2, value3, value4, value5 = os.pullEvent("rednet_message")

Why? That still leads to the same problem. Rednet sends the tables, not the modem API. And the OP is listening on the broadcast channel…
Bomb Bloke #6
Posted 10 August 2015 - 04:53 AM
Er, what?

The modem API is always used send the messages - full stop. This is the case whether you're using the rednet API as a wrapper for it or otherwise.

If you're using the rednet API, and have at least ComputerCraft 1.6, then rednet.send() encapsulates the message within a table before passing it to the modem API for transmission. The receiving system detects the resulting modem_message event via rednet.run() (which runs in a coroutine separate to whatever else the user has the system doing), and that queues a rednet_message event using the message within the table.

That said, you can send tables as messages, if you want. But if no table was in the original message passed to rednet.send(), then no table will exist within the final rednet_message event, either.

The reason that no rednet_event messages are appearing at all is because rednet.run() only performs these event translations if the broadcast port and the port equal to the computer's ID number are open. So yeah, something like this would also need to be added to the receiver script:

wifi.open(os.getComputerID())
resdac #7
Posted 10 August 2015 - 12:38 PM
Either that, or you could get the message with "event4.message"
thanks this fixes the problem, opening the modem with the computer id wasn't a good option cause i am still looking for other events like the key event and mouse event for advanced pc's
H4X0RZ #8
Posted 10 August 2015 - 01:06 PM
Er, what?

The modem API is always used send the messages - full stop. This is the case whether you're using the rednet API as a wrapper for it or otherwise.

If you're using the rednet API, and have at least ComputerCraft 1.6, then rednet.send() encapsulates the message within a table before passing it to the modem API for transmission. The receiving system detects the resulting modem_message event via rednet.run() (which runs in a coroutine separate to whatever else the user has the system doing), and that queues a rednet_message event using the message within the table.

That said, you can send tables as messages, if you want. But if no table was in the original message passed to rednet.send(), then no table will exist within the final rednet_message event, either.

The reason that no rednet_event messages are appearing at all is because rednet.run() only performs these event translations if the broadcast port and the port equal to the computer's ID number are open. So yeah, something like this would also need to be added to the receiver script:

wifi.open(os.getComputerID())

Oh. Sorry, I messed some stuff up. It's not a good idea to write answers in AaP late at night…