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

Rednet Send Question

Started by nynoray, 06 September 2012 - 08:25 PM
nynoray #1
Posted 06 September 2012 - 10:25 PM
Hey guys, I just have a question to ask about having a rednet.send working within a program.

This is my code for it so far.
 while true do
	 print("Enter the ID of the person you want to send to")
	 id = read ()
	 break
end

while true do
	 print("Enter the message you want to send")
	 msg = read ()
	 break
end
while true do
	 --print(id)
	 --print(msg)
	 Shell.run(rednet.send(id, "msg"))
  break
end

The program is supposed to send the specified message to the ID entered. The problem I am having with it is that I can't figure out how to get the data from the read's into the rednet.send. I have the prints there just to test that the read's are working, if I remove the dashes after i have entered the ID and message It will print the things I entered so I know the reads are working.

Basicly I just need to know how I could get the data from id = read () and msg = read () into the rednet.send().
kazagistar #2
Posted 06 September 2012 - 10:39 PM
Your code makes very little sense. Why are you creating loops just to break out of them right away? What is the shell.run called for anyways?

First, variables. Variables are words which hold data inside them. We will declare some local variables to begin with (that just means they will only be usable inside this program).

local id, msg

Then we read in the data from the user. We call a function that prints text and returns nothing, followed by a function that lets the user type stuff in, and returns it as a "string", or list of letters. We take the result of each read, and store them in their respective variables using the = operator.

print("Enter the ID of the person you want to send to")
id = read ()

print("Enter the message you want to send")
msg = read ()

Finally, we want to send the message. What you have to remember, is that whatever is stored in the variable will basically replace the variable in code. So for example if I do this:

text = "some stuff to print"
print(text + "!")
it will do the exact same thing as

print("some stuff to print" + "!")
which in turn does the same thing as

print("some stuff to print!")

So, if I want to send a message, I just need to do

rednet.send(id, msg)

So a finished sender program looks like

local id, msg
print("Enter the ID of the person you want to send to")
id = read ()
print("Enter the message you want to send")
msg = read ()
rednet.send(id, msg)
print("Message sent")

Then, on a different computer, you want to get the message and run it in the shell, right? For that you need a separate program to run there. If you want to understand how the functions work better, just head over to the wiki. rednet.recieve() works a lot like print, but it returns 3 pieces of information, so you need 3 variables (in the right order) to store them all.


local sender, message, distance
while true do
    print("Waiting for message")
    sender, message, distance = rednet.receive()
    shell.run(message)
end

Good luck!
nynoray #3
Posted 06 September 2012 - 10:43 PM
the shell.run… was the main problem I had so that is obviously going to be pretty screwed up. This was also a test version of this program, only meant for getting the rednet.send to work
MysticT #4
Posted 06 September 2012 - 10:44 PM

rednet.send(id, msg)
The id must be a number, so when reading it convert it to a number with tonumber:

id = tonumber(read())

Also, why did you put those while loops if you break them without looping?
kazagistar #5
Posted 06 September 2012 - 11:12 PM
Heh, oops, oh well, tutorials without bugs in make people complacent, and testing my code is too hard. :D/>/>
nynoray #6
Posted 06 September 2012 - 11:19 PM

rednet.send(id, msg)
The id must be a number, so when reading it convert it to a number with tonumber:

id = tonumber(read())

Also, why did you put those while loops if you break them without looping?

Made this code months ago and was messing around with it a lot then got back into CC so I decided to ask for help. Im bad at lua in the first place anyways
nynoray #7
Posted 06 September 2012 - 11:24 PM
Your code makes very little sense. Why are you creating loops just to break out of them right away? What is the shell.run called for anyways?

First, variables. Variables are words which hold data inside them. We will declare some local variables to begin with (that just means they will only be usable inside this program).

local id, msg

Then we read in the data from the user. We call a function that prints text and returns nothing, followed by a function that lets the user type stuff in, and returns it as a "string", or list of letters. We take the result of each read, and store them in their respective variables using the = operator.

print("Enter the ID of the person you want to send to")
id = read ()

print("Enter the message you want to send")
msg = read ()

Finally, we want to send the message. What you have to remember, is that whatever is stored in the variable will basically replace the variable in code. So for example if I do this:

text = "some stuff to print"
print(text + "!")
it will do the exact same thing as

print("some stuff to print" + "!")
which in turn does the same thing as

print("some stuff to print!")

So, if I want to send a message, I just need to do

rednet.send(id, msg)

So a finished sender program looks like

local id, msg
print("Enter the ID of the person you want to send to")
id = read ()
print("Enter the message you want to send")
msg = read ()
rednet.send(id, msg)
print("Message sent")

Then, on a different computer, you want to get the message and run it in the shell, right? For that you need a separate program to run there. If you want to understand how the functions work better, just head over to the wiki. rednet.recieve() works a lot like print, but it returns 3 pieces of information, so you need 3 variables (in the right order) to store them all.


local sender, message, distance
while true do
	print("Waiting for message")
	sender, message, distance = rednet.receive()
	shell.run(message)
end

Good luck!

TY your code worked except for the id = read() one but it worked after I replaced that line with the master of lua :D/>/> posted(MysticT). I already had a receiving program that runs on a monitor beside the 'sender' computer for a short range email server. Working on extending the range now (not happening any time soon lmao i suck at lua)