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

Need some help with rednet

Started by cheekycharlie101, 07 September 2012 - 08:11 PM
cheekycharlie101 #1
Posted 07 September 2012 - 10:11 PM
ok so im working on making a 1 to 1 chat room using rednet. i need help with a sending program though. basicly i want to be able to run a program like this : send 2 Hi there
this would send a message to the computer number 2 and the message would be high there. ive tried coding this but did not get any luck :D/>/> could someone help me out please?
thanks -Charles
OmegaVest #2
Posted 07 September 2012 - 11:19 PM
So, I won't write your program for you, but I will give you a few pointers, and you can take it from there.

First, a programs starting params (the ones you type in the shell, in your example they would be "2", "Hi" and "there"), are passed through by a symbol "…" This is pulled as a table, but you can break down a table with either multiple variables, like x, y, z = …, or by just calling a table (tArgs = {…}). A table can be used to pull specific values out like so: tArgs[1], which in this case would give us 2, where tArgs[2] would give us "Hi".

The next thing you need to know is that rednet.send can accept a number for the recipient. So, in you example, you would need something like rednet.send(2, "Hi there"). But this is static, so you will want to use table calls for it.

Lastly, you always need to make sure you open a modem or line.


I hope this gives you a starting point. Try to code this far, and post what you have. I or others will be waiting to assist you further.



EDIT: Ahem. Forgot this little lesson.

So, sending is all well and good, but is wasted without something to pick it up. Well, that's an easy fix. rednet.receive(). It even has a timeout function, just put a number in its parentheses. But make sure the receive is on, or the message won't make it.
cheekycharlie101 #3
Posted 08 September 2012 - 09:07 AM
So, I won't write your program for you, but I will give you a few pointers, and you can take it from there.

First, a programs starting params (the ones you type in the shell, in your example they would be "2", "Hi" and "there"), are passed through by a symbol "…" This is pulled as a table, but you can break down a table with either multiple variables, like x, y, z = …, or by just calling a table (tArgs = {…}). A table can be used to pull specific values out like so: tArgs[1], which in this case would give us 2, where tArgs[2] would give us "Hi".

The next thing you need to know is that rednet.send can accept a number for the recipient. So, in you example, you would need something like rednet.send(2, "Hi there"). But this is static, so you will want to use table calls for it.

Lastly, you always need to make sure you open a modem or line.


I hope this gives you a starting point. Try to code this far, and post what you have. I or others will be waiting to assist you further.



EDIT: Ahem. Forgot this little lesson.

So, sending is all well and good, but is wasted without something to pick it up. Well, that's an easy fix. rednet.receive(). It even has a timeout function, just put a number in its parentheses. But make sure the receive is on, or the message won't make it.
Thanks, i did take a look at the rednet api on the wiki, i also managed to code a basic lisenter to receive messages. but im still confused how to make a program run with params. i need to be able to make something like the program is callled sendmessage but how do i add params to it? i cant figure this out. p.s im a noob to lua and im not really getting anywhere with rednet
OmegaVest #4
Posted 09 September 2012 - 02:54 AM
First, a programs starting params (the ones you type in the shell, in your example they would be "2", "Hi" and "there"), are passed through by a symbol "…" This is pulled as a table, but you can break down a table with either multiple variables, like x, y, z = …, or by just calling a table (tArgs = {…}). A table can be used to pull specific values out like so: tArgs[1], which in this case would give us 2, where tArgs[2] would give us "Hi".



tArgs = {...}

recptID = tArgs[1]
outMsg = ""
for i = 2, #tArgs do
   outMsg = outMsg .. tArgs[i]
end
ltstingray #5
Posted 09 September 2012 - 03:06 AM
If I may expand a bit on Omega's code


tArgs = {...}

recptID = tArgs[1]
outMsg = ""
for i = 2, #tArgs do
   outMsg = outMsg .. tArgs[i]
end

tArgs = { … }
This is the code for accepting a command line parameter

recptID = tArgs[1]
Here we're taking the first parameter passed and putting it into the variable recptID (Which seems to be recipientID aka the computer we want to send to)

outMsg = ""
We're initializing a variable to hold the actual message to be sent

for i = 2, #tArgs do
creating a for loop with i as a variable ranging from 2 to the total number or command line arguments

outMsg = outMsg .. tArgs
using the above loop we're pulling each of the remaining command line arguments (tArgs) and appending them to the variable outMsg this step along with the one above are important or else when you typed in say "send 2 Hi There" computer id would only receive "HI" as the message but by taking the remaining arguments and inputting them into the variable they will receive the whole message.

end
ending the loop