13 posts
Posted 15 April 2017 - 06:24 AM
I have a few computers using rednet.send(). and i want to have 1 computer receiving all of the messages using modem_message. Everything works fine, except that when i go to display the message, it says that it is in a table, so i used an unpack(message), and that is returning nil. There shouldnt even be anything to do with a table anyways, because the messages being sent using rednet.send() are strings, so it should work fine. Its really confusing me.
8543 posts
Posted 15 April 2017 - 07:39 AM
Rednet sends things in tables because there is additional information sent with the specified message. Check out the rednet API to see the table structure.
7083 posts
Location
Tasmania (AU)
Posted 15 April 2017 - 10:36 AM
Also note that "unpack" only works on numerically-indexed tables, and ignores other key types (hence why it makes the table appear to be empty - it uses strings for its keys).
13 posts
Posted 16 April 2017 - 02:00 AM
Rednet sends things in tables because there is additional information sent with the specified message. Check out the rednet API to see the table structure.
When you say that it sends additional info, are you talking about the senderID and protocol? Or there are additional things in the message part of the function?
Also note that "unpack" only works on numerically-indexed tables, and ignores other key types (hence why it makes the table appear to be empty - it uses strings for its keys).
Do you have any suggestions on how i can unpack the message into a string so i can display it on screen?
7083 posts
Location
Tasmania (AU)
Posted 16 April 2017 - 02:16 AM
You can either iterate through the table content with a "pairs" loop:
for key, value in pairs(myTable) do
print(key .. " is a " .. type(value))
end
… or just use
textutils.serialise() to turn the whole thing into a string in one go:
print( textutils.serialise( myTable ) )
There's indeed more content in the message tables, such as a UUID used to prevent systems generating multiple rednet_message events should the
repeat script pass them multiple copies of the same transmission. Refer to
the source of rednet.run() to see how the tables are "converted" to rednet_message events - this function runs in parallel with CraftOS on every system.
13 posts
Posted 16 April 2017 - 04:08 AM
You can either iterate through the table content with a "pairs" loop:
for key, value in pairs(myTable) do
print(key .. " is a " .. type(value))
end
… or just use
textutils.serialise() to turn the whole thing into a string in one go:
print( textutils.serialise( myTable ) )
There's indeed more content in the message tables, such as a UUID used to prevent systems generating multiple rednet_message events should the
repeat script pass them multiple copies of the same transmission. Refer to
the source of rednet.run() to see how the tables are "converted" to rednet_message events - this function runs in parallel with CraftOS on every system.
Thank you, ill go have a read and test it. :)/>
13 posts
Posted 16 April 2017 - 04:19 AM
Alright, using textutils.serialise() worked first try, now, the last thing is. How can i split the data into different variables so i can display the parts i need?
For example, getting just the actual message.
I see what you mean by the UUID.
Edited on 16 April 2017 - 02:29 AM
8543 posts
Posted 16 April 2017 - 05:11 AM
If you want just the message, why not get it from the rednet_message event instead of the modem_message event?
13 posts
Posted 16 April 2017 - 06:06 AM
I want the recipient computer and the ID. Sorry, should have been more clearer about that.
7083 posts
Location
Tasmania (AU)
Posted 16 April 2017 - 07:50 AM
Let's say you've stored the incoming message in "myTable"; assuming it was generated by rednet.send(), you could then do:
print(myTable.message) --> The original sender's message.
print(myTable.nRecipient) --> The ID of the system the message was supposed to be sent to.
If it
wasn't generated by rednet.send(), then the original message may not have these keys, and may not even be a table at all! You can check this:
if type(myTable) == "table" and myTable.message then --> "if the message is a table and has a "message" key"...
Edited on 16 April 2017 - 05:50 AM
13 posts
Posted 16 April 2017 - 07:51 AM
Let's say you've stored the incoming message in "myTable"; assuming it was generated by rednet.send(), you could then do:
print(myTable.message) --> The original sender's message.
print(myTable.nRecipient) --> The ID of the system the message was supposed to be sent to.
If it
wasn't generated by rednet.send(), then the original message may not have these keys, and may not even be a table at all! You can check this:
if type(myTable) == "table" and myTable.message then --> "if the message is a table and has a "message" key"...
Awesome, i reckon that will do it. Thank you