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

String in a rednet message

Started by Wing, 12 August 2012 - 12:15 AM
Wing #1
Posted 12 August 2012 - 02:15 AM
Hey everyone!
I was wondering if somehow I would be able to use a number inside a rednet message to determine what to do next, for example:

Broadcast = 3, Hello World — 3 is the number prefix I am talking about
Received = id, 3, Hello World — Then the computer reads the "3" inside the message to then open a different port and resend the ——————————————- same message threw that different port.

So essentially that's all I want to know for my network system and would love to hear some feedback!
Thanks,
Wing!
NeverCast #2
Posted 12 August 2012 - 02:45 AM
You can do anything in ComputerCraft that you can do in Lua, So the Lua wiki is the best place to look for this sort of thing. You can use delimiters in your rednet message strings and then use lua split, and convert the string to a number.

I'm still new to Lua, so here is where I looked
http://lua-users.org/wiki/SplitJoin
http://lua-users.org/wiki/NumbersTutorial ( see conversion )

Although you could just compare a string to "3" instead of 3 if you wanted, without converting.

Sorry I don't have any example code for you as my laptop doesn't run Minecraft so well. But I'll check this post later and if my links haven't helped, I'll see if I can help you with some CopyPasta source. Although I do really suggest learning all the functionality of Lua :P/>/>
Wing #3
Posted 12 August 2012 - 03:03 AM
Thanks Never Cast, this is definitely helping although I don't fully understand how to enter grating this into my fictional program (planning it)
Pharap #4
Posted 12 August 2012 - 07:00 AM
Ok, shorter answer, send a number as a string, then just convert it using tonumber()

eg:
e, id, msg = os.pullEvent()
–rest of stuff here
variable = tonumber(msg)
–more stuff here

simple
KaoS #5
Posted 12 August 2012 - 07:41 AM
if you are trying to send multiple messages simultaneously use a table, serialize it and send it, then unserialize it on the other side
Wing #6
Posted 12 August 2012 - 08:38 PM
Pharap, I don't think that's what I need, I'm looking to recognize the number in the message. So like said above:
Broadcast = 6, Hello Everyone
The computer receiving would copy that message to be sent again but depending on the number in front of the actual message
it would open/close a rednet port. So basicly, it would if x == 1 then, if x == 2 then, if x == 3 then…. "x" being the number 6 from the broadcast.
I am completely clueless
Pharap #7
Posted 12 August 2012 - 10:25 PM
Pharap, I don't think that's what I need, I'm looking to recognize the number in the message. So like said above:
Broadcast = 6, Hello Everyone
The computer receiving would copy that message to be sent again but depending on the number in front of the actual message
it would open/close a rednet port. So basicly, it would if x == 1 then, if x == 2 then, if x == 3 then…. "x" being the number 6 from the broadcast.
I am completely clueless

I know what you are trying to do.
All you need to to is choose a delimiter to separate the message from the number (eg ";")

Then run the script like this:

e, id, msg = os.pullevent
if e == "rednet_message" then
local pos = string.find(msg, ";")
local NtoTest = tonumber(string.sub(msg,1,pos-1)
local RelayMsg = string.sub(msg, pos+1)
--other stuff goes here
end

NtoTest is the number you want, RelayMsg is the message without the number and the delimiter so you can pass it on.
There, I did know what you wanted, I was just foolishly assuming you knew how to extract the number from the string.
Wing #8
Posted 13 August 2012 - 01:31 AM
Thanks Pharap that's what I need lol, all the links being posted and ect made no sense to me but that code does!
But anyway thanks as well Chaos and NeverCast
Pharap #9
Posted 13 August 2012 - 02:07 AM
Thanks Pharap that's what I need lol, all the links being posted and ect made no sense to me but that code does!
But anyway thanks as well Chaos and NeverCast

You're welcome. I remember when I started out learning programming, I much preferred having one of my teachers explain things to me as opposed to having to decipher horribly written 'formal' explanations. (I especially hate it when someone starts saying 'we can do this'. It makes me go all 'we, what do you mean we, you understand it, I don't, give it to me straight man' lol anyway)

So, that's why I try to understand what a person wants, give it to them exactly and perhaps explain it to them. It's much nicer than the alternative, and you tend to learn better if you see the exact code in its simplest form since you can usually see what it's doing.
Lyqyd #10
Posted 13 August 2012 - 02:13 AM
By the way, you might find it cleaner to use string.match(), which looks for things in the string. So to match a number, you'd use %d (think digit), and to match letters and numbers, %w. So:


id, msg = rednet.receive()
forwardID, forwardMessage = string.match(msg, "(%d+), (%w+)")