Jump to content




How to grab text


  • You cannot reply to this topic
6 replies to this topic

#1 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, Ukraine

Posted 22 April 2015 - 06: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

#2 Square789

  • Members
  • 39 posts
  • LocationUniverse:C:/MilkyWay/Sol/Earth/Europe/Germany

Posted 22 April 2015 - 06: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 by Square789, 22 April 2015 - 06:44 PM.


#3 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, Ukraine

Posted 22 April 2015 - 06:45 PM

View PostSquare789, on 22 April 2015 - 06:41 PM, said:

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!

#4 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, Ukraine

Posted 22 April 2015 - 07:03 PM

View PostSquare789, on 22 April 2015 - 06:41 PM, said:

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?

#5 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 22 April 2015 - 07: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.

#6 Square789

  • Members
  • 39 posts
  • LocationUniverse:C:/MilkyWay/Sol/Earth/Europe/Germany

Posted 22 April 2015 - 07: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.

#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 23 April 2015 - 12: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users