I'm trying to make a chat system, but right now when you want to send a message you have to edit the program and type in your message. I don't want to have to do that, I want to know how to grab text from a message. Like say my program to send a message is 'message', I want to add the text for the message to be sent after that. What code would I use to do that?
What I want example: 'message Hello!'
which will send the message "Hello!" to all listening on that rednet channel.
Current sending code:
http://imgur.com/Rpsgiea
How to grab text
Started by diamondpumpkin, Apr 22 2015 06:31 PM
6 replies to this topic
#1
Posted 22 April 2015 - 06:31 PM
#2
Posted 22 April 2015 - 06:41 PM
I think you want to use args
So if you run the program in the shell
But this doesn't work with spaces, so
tArgs = {...}
rednet.broadcast(tArgs[1])
So if you run the program in the shell
> message Hello,all!_It sends the message "Hello,all!"
But this doesn't work with spaces, so
> message Hello, my name is Stevewould only send "Hello,"
Edited by Square789, 22 April 2015 - 06:44 PM.
#4
Posted 22 April 2015 - 07:03 PM
Square789, on 22 April 2015 - 06:41 PM, said:
I think you want to use args
So if you run the program in the shell
But this doesn't work with spaces, so
tArgs = {...}
rednet.broadcast(tArgs[1])
So if you run the program in the shell
> message Hello,all!_It sends the message "Hello,all!"
But this doesn't work with spaces, so
> message Hello, my name is Stevewould only send "Hello,"
Just curious, how would I make it work with spaces?
#5
Posted 22 April 2015 - 07:04 PM
Spaces seperate arguments, so
> message Hello Worldwould have tArgs[1] be "Hello" and tArgs[2] be "World" you can recombine these by doing
newMsg = table.concat(tArgs," ")What this does is take the table tArgs and adds a space in between each argument. The " " is what it will add, so "," would return Hello,World and "-.-" would return Hello-.-World as an example.
#6
Posted 22 April 2015 - 07:30 PM
I'd do it like this but sure there are more professional ways to do this:
And the tosring() is there because otherwise the message entered above would end the program with:
Okay, it works, but some programming languages won't accept joining a string and a number.
local i = 1 local newMsg = "" for i = 1, #tArgs do newMsg = newMsg.." "..tostring(tArgs[i]) endThis joins the elements of the table with a space, so
> message Hello, I have 5 million dollars >:-Doutputs this string:
"Hello, I have 5 million dollars >:-D"
Okay, it works, but some programming languages won't accept joining a string and a number.
#7
Posted 23 April 2015 - 12:53 AM
You can put quotes in the command line to tell it to treat it as a string:
In the test program:
> test "this will be a single argument"
In the test program:
args = {...}
print(args[1]) --#> this will be a single argument
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











