32 posts
Location
Belgium
Posted 14 June 2015 - 10:30 AM
I'm sorry if i'm posting like crazy here. Its just that i can't really find my problems somewhere else (if you can please post me a link)
So my problem is that when i try to use the Rednet API to resolve a message sent over a wireless network it would end up in an error.
while true do
event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
print("Message received from the open modem on the "..side)
print("Frequency: "..frequency)
print("Requested reply frequency: "..replyFrequency)
print("Distance: "..distance)
print("Message: "..message)
end
Error:
receive:7: attemp to concatenate string and tableCould anyone tell me what i'm doing wrong? I also just followed this tutorial:
http://computercraft.info/wiki/Modem_message_(event)
2679 posts
Location
You will never find me, muhahahahahaha
Posted 14 June 2015 - 10:36 AM
Use tostring(value) for all of the values. That way it won't error.
I don't know why it returns a table. Is that all the code?
Edited on 14 June 2015 - 08:36 AM
656 posts
Posted 14 June 2015 - 10:59 AM
From the modem_event
wiki page:
Returned Object 4: The message received. It will remain the same type as was transmitted.
Rednet sends table messages, so they will be received as tables aswell. I think the table contains the receiver id and the message itself.
To see a bunch of random characters, use what Creator said (toString). To see the contents of the table, use textutils.serialize.
@Creator I'm not trying to be mean, just joking :P/>
Edited on 14 June 2015 - 09:06 AM
355 posts
Location
Germany
Posted 14 June 2015 - 11:02 AM
Use tostring(value) for all of the values. That way it won't error.
I don't know why it returns a table. Is that all the code?
It's correct how it is. the transmission function of modems allows tables to be sent, without the need of serialization.
In fact, every message via rednet protocol, that runs through the modem api passes a plain table to the transmit function.
The receiving end will get that table plain also. His message is a table, containing the recipient id and the "message" itself via rednet protocol
:ph34r:/>
Edited on 14 June 2015 - 09:02 AM
2679 posts
Location
You will never find me, muhahahahahaha
Posted 14 June 2015 - 11:15 AM
I didn't know you can send tables over rednet. Nice and smooooth.
656 posts
Posted 14 June 2015 - 11:21 AM
I didn't know you can send tables over rednet. Nice and smooooth.
Note that you can't send tables with the rednet api, only with the modem api.
Edited on 14 June 2015 - 09:22 AM
1140 posts
Location
Kaunas, Lithuania
Posted 14 June 2015 - 11:36 AM
If you're using RedNet then I'd suggest to either use rednet_measage event or rednet.receive, which deals with the message itself.
32 posts
Location
Belgium
Posted 14 June 2015 - 11:45 AM
How would i turn that table into a string? Using tostring just returns the table. How do I use the serialization?
Sorry i'm not that advanced in coding yet.
1140 posts
Location
Kaunas, Lithuania
Posted 14 June 2015 - 11:48 AM
Like I said, you should be using rednet_message event or rednet.receive() function for this.
7083 posts
Location
Tasmania (AU)
Posted 14 June 2015 - 12:00 PM
Note that you can't send tables with the rednet api, only with the modem api.
You can send tables either way, as it happens. Unless you've got an older version of ComputerCraft. Can't quite remember the build where it changed, but it'd be pre-1.6, so the functionality's been there for quite a while now.
In any case, when you couldn't send them through the rednet API, you also couldn't send them directly as modem messages, either.
Edited on 14 June 2015 - 10:01 AM
2679 posts
Location
You will never find me, muhahahahahaha
Posted 14 June 2015 - 12:01 PM
If you want a string, do textutils.serialize(myTable).
32 posts
Location
Belgium
Posted 14 June 2015 - 12:39 PM
Like I said, you should be using rednet_message event or rednet.receive() function for this.
Thank you! That worked. One more thing i'm wondering though.
in the wiki it says this: Returned Object 3 The distance (in blocks) between the computers.
But when I try to use it in a string i get this error:
attempt to concatenate string and nil. When i just call the distance i get nothing.
while true do
event, senderId, message, distance = os.pllEvent("rednet_message")
print("Distance: " ..distance)
end
7083 posts
Location
Tasmania (AU)
Posted 14 June 2015 - 01:00 PM
Rednet messages are a bit different to modem messages. For one thing, they offer a "protocol" value instead of "distance", but that may or may not be set to something; it'll often be nil.
Heck, you never know whether the
message will be nil, for that matter.
So, just perform an existence check before trying to concatenate:
while true do
event, senderId, message, protocol = os.pllEvent("rednet_message")
if protocol then print("Protocol : " .. protocol) end
end
Before ComputerCraft 1.6, rednet messages did offer a distance value, but then Dan added the rednet repeater code which made the value a bit less useful (as a message might be re-transmitted a few times before arriving).
32 posts
Location
Belgium
Posted 14 June 2015 - 01:14 PM
Rednet messages are a bit different to modem messages. For one thing, they offer a "protocol" value instead of "distance", but that may or may not be set to something; it'll often be nil.
Heck, you never know whether the
message will be nil, for that matter.
So, just perform an existence check before trying to concatenate:
while true do
event, senderId, message, protocol = os.pllEvent("rednet_message")
if protocol then print("Protocol : " .. protocol) end
end
Before ComputerCraft 1.6, rednet messages did offer a distance value, but then Dan added the rednet repeater code which made the value a bit less useful (as a message might be re-transmitted a few times before arriving).
Ok thank you. The protocol value would have not much use in my case. Thanks for your time.
656 posts
Posted 14 June 2015 - 01:20 PM
Note that you can't send tables with the rednet api, only with the modem api.
You can send tables either way, as it happens. Unless you've got an older version of ComputerCraft. Can't quite remember the build where it changed, but it'd be pre-1.6, so the functionality's been there for quite a while now.
In any case, when you couldn't send them through the rednet API, you also couldn't send them directly as modem messages, either.
Then all the rednet functions should be updated on the wiki:
wiki page.
Are you allowed to edit the wiki? Or should I post this in wiki discussion?
Edited on 14 June 2015 - 12:42 PM
7083 posts
Location
Tasmania (AU)
Posted 14 June 2015 - 01:55 PM
Yeah, I can do it, if I ever figure out the version which made the change…