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

Writing a program that sends a string from the command line?

Started by Chaz, 16 May 2014 - 03:48 PM
Chaz #1
Posted 16 May 2014 - 05:48 PM
Hi there! I've been dabbling in ComputerCraft now and again, and with the addition of Pocket Computers in 1.6, I've been thinking of some ways to cause some harmless mischief on my friend's server (They've designated me our base's technician, because even though I don't have much ComputerCraft knowledge, I apparently have more than they do!)

What I have so far is a computer with a Startup program set to recieve a message over Rednet, which it then displays on the monitor for a few seconds, like so:


while true do
  local monitor=peripheral.wrap('monitor_0')
  rednet.open('right')
  local chazid, chazmessage, chazdist=rednet.receive()
  monitor.setCursorPos(1,1)
  monitor.write(chazmessage)
  sleep(3)
  monitor.clear()
end

So far I've just been sending messages to the computer using the Pocket Wireless Computer in the lua interactive environment (By opening RedNet and then sending messages to the computer using rednet.send(1, "Hello there!"). This works perfectly well, but what I want to do is set up a program on my pocket computer to accept a string as an argument, and then send that string over rednet to the receiving computer, so instead of having to type that rednet.send line all the time, I could just for instance type it on the shell, like so:


say "Hello world!"

And have it send that message to my main computer, so it'll get shown on the monitor. (In this way I'm hoping to pull a prank on my non-computercrafty friends by sending messages to the monitor while I'm not looking at it, making it seem like there's an AI chatting to them or something like that)

So far I have:


args={...}
rednet.open(back)
rednet.send(1, args)

However, this shows up as {1.0, Hello World!} on my monitor, so I've probably messed something up. I know next to nothing about the args command, so I followed another thread I found on this board about arguments, and after wrestling with "Expected String" errors for a little bit I managed to get it half-working. All I want to show up on the monitor is the string I send in the args for the say command.

Any help with this would be appreciated!
Lignum #2
Posted 16 May 2014 - 05:58 PM
Args is a table containing the program arguments.

E.g:

Assuming your program is executed like this:
say <id> <message>

args[1] would be the id, while args[2] would be the message. However, do keep in mind that, since arguments are separated by spaces, args[2] will only return the first word in the message.
Chaz #3
Posted 16 May 2014 - 06:05 PM
I actually had it figured that it'd be executed like:

say <message>

As I'm only sending it to one computer which, in my sandbox testing ground, has the ID of 1 (Though after watching Direwolf20's video on the new features of 1.6 I might change it so the machine gets its own hostname and then change this program so it sends to that hostname instead of the ID number, which would make things easier in the long run)

But yeah, I have it planned so that it already sends to one specific computer, and all I'll need to provide is say followed by the message I want to send.
Lignum #4
Posted 16 May 2014 - 06:45 PM
Well, then you'd do something like this:

local args = {...}
local message = ""
--# Insert every argument into a variable.
for i,v in ipairs(args) do
	message = message .. v .. " "
end
--# Send it.
rednet.send(1, message)
Edited on 16 May 2014 - 05:43 PM
Chaz #5
Posted 16 May 2014 - 07:11 PM
Hmm, I've tried putting the code in exactly as you typed it, but it doesn't seem to be working quite as expected. There are no errors, but the computer doesn't print anything after recieving the message. I looked into it by putting the monitor's computer into the interactive lua environment, it shows that it's recieving the signal from my pocket computer, but all it shows is the pocket computer's ID number, and no messages.

Any idea if I'm missing something?
Lignum #6
Posted 16 May 2014 - 07:43 PM
Any idea if I'm missing something?
No, but I am.

No idea how I managed that but the line in the for loop is supposed to be:

message = message .. v .. " "
Sorry about that.
Chaz #7
Posted 16 May 2014 - 07:53 PM
There we go, it's working now! Thank you for helping me out, Lignum!
Lignum #8
Posted 16 May 2014 - 07:54 PM
There we go, it's working now! Thank you for helping me out, Lignum!
You're welcome!