59 posts
Location
Places, Ukraine
Posted 22 April 2015 - 07:31 PM
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
57 posts
Location
Universe:C:/MilkyWay/Sol/Earth/Europe/Germany
Posted 22 April 2015 - 07:41 PM
I think you want to use args
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 Steve
would only send "Hello,"
Edited on 22 April 2015 - 05:44 PM
59 posts
Location
Places, Ukraine
Posted 22 April 2015 - 07:45 PM
I think you want to use args
tArgs = {...}
rednet.broadcast(tArgs[1])
Not quite sure how to use that, I'm new at this…
Ah ha! Found out how to use it! Thanks!
59 posts
Location
Places, Ukraine
Posted 22 April 2015 - 08:03 PM
I think you want to use args
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 Steve
would only send "Hello,"
Just curious, how would I make it work with spaces?
113 posts
Location
This page
Posted 22 April 2015 - 08:04 PM
Spaces seperate arguments, so
> message Hello World
would 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.
57 posts
Location
Universe:C:/MilkyWay/Sol/Earth/Europe/Germany
Posted 22 April 2015 - 08:30 PM
I'd do it like this but sure there are more professional ways to do this:
local i = 1
local newMsg = ""
for i = 1, #tArgs do
newMsg = newMsg.." "..tostring(tArgs[i])
end
This joins the elements of the table with a space, so
> message Hello, I have 5 million dollars >:-D
outputs this string:
"Hello, I have 5 million dollars >:-D"
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.
957 posts
Location
Web Development
Posted 23 April 2015 - 01:53 AM
You can put quotes in the command line to tell it to treat it as a string:
> test "this will be a single argument"
In the test program:
args = {...}
print(args[1]) --#> this will be a single argument