Jump to content




modem_message seems to store the actual message as a table?


10 replies to this topic

#1 Wilobate

  • Members
  • 13 posts

Posted 15 April 2017 - 05: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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 15 April 2017 - 06: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.

#3 Bomb Bloke

    Hobbyist Coder

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

Posted 15 April 2017 - 09: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).

#4 Wilobate

  • Members
  • 13 posts

Posted 16 April 2017 - 01:00 AM

View PostLyqyd, on 15 April 2017 - 06:39 AM, said:

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?

View PostBomb Bloke, on 15 April 2017 - 09:36 AM, said:

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?

#5 Bomb Bloke

    Hobbyist Coder

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

Posted 16 April 2017 - 01: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.

#6 Wilobate

  • Members
  • 13 posts

Posted 16 April 2017 - 03:08 AM

View PostBomb Bloke, on 16 April 2017 - 01:16 AM, said:

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. :)

#7 Wilobate

  • Members
  • 13 posts

Posted 16 April 2017 - 03: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 by Wilobate, 16 April 2017 - 03:29 AM.


#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 April 2017 - 04:11 AM

If you want just the message, why not get it from the rednet_message event instead of the modem_message event?

#9 Wilobate

  • Members
  • 13 posts

Posted 16 April 2017 - 05:06 AM

I want the recipient computer and the ID. Sorry, should have been more clearer about that.

#10 Bomb Bloke

    Hobbyist Coder

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

Posted 16 April 2017 - 06: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 by Bomb Bloke, 16 April 2017 - 06:50 AM.


#11 Wilobate

  • Members
  • 13 posts

Posted 16 April 2017 - 06:51 AM

View PostBomb Bloke, on 16 April 2017 - 06:50 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users