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

Rednet "Telecom" problem

Started by subzerofox, 06 September 2012 - 08:16 AM
subzerofox #1
Posted 06 September 2012 - 10:16 AM
Hey, im currently attempting my first rednet project so it isn't surprising i need help even though it is fairly straight forward. im writing two programs. one that creates an interface to send the text, and one to just receive and display. two computers. the sending one is labeled fox1 and has an ID of 18. when i run the code, i merely run the code name. it looks like this:

while true do
term.clear()
term.setCursorPos(1, 1)
print("Enter broadcast message")
input = read()
rednet.broadcast(input)
end

the receiving end looks like this:

while true do
print( rednet.receive() )
end


that is the extent of my LUA knowledge as it is.. so.. any help would be appreciated, along with an explanation why so i can do bigger and better things (i plan on making a chatroom style rednet server, i have the basic idea down, but i'll need this information plus more to do it.) thank you. also a code sample of a one on one "text messenger" style one would be great, ima use turtles to text :D/>/> useless.. but good for leaving messages
KaoS #2
Posted 06 September 2012 - 10:21 AM
the receiver is the problem, you are writing the output of the rednet.receive() command which actually outputs the id of the sending pc, then the message, then the distance between sender and receiver, use:

while true do
local id,msg,dist=rednet.receive()
print(msg)
end
subzerofox #3
Posted 06 September 2012 - 10:25 AM
thank you very much, that helped everything.
what is teh local function you are using? that is one of the things i needed to know. does that give a nickname for a piece of code?
KaoS #4
Posted 06 September 2012 - 10:26 AM
also do not forget to open your modems
subzerofox #5
Posted 06 September 2012 - 10:27 AM
first thing i checked when it didnt go right, i changed code to turn on rednet before it tries to send/receive
KaoS #6
Posted 06 September 2012 - 10:50 AM
excellent. local is not really a function, it basically makes changes to a variable local (programs out of the current one do not see the changes) for example


me=1
print(me)
function temp()
local me=me+3
print(me)
end
print(me)

would output:

1
4
1
subzerofox #7
Posted 06 September 2012 - 11:01 AM
So local changed the set variable me from 1 to 1+3?

And the commas were? It'd be easier to break down the code fix you gave me
Rsstn #8
Posted 06 September 2012 - 11:16 AM
The 'local' just means that the value of 'me' is only changed for that chunk of code (in this case, the function 'temp()'). Anywhere else you use the variable 'me', the value is still the same (1).
The commas in the rednet.recieve() code are used to separate the three variables assigned to the output of 'rednet.recieve()'.
This means that the variable 'id' is assigned to the ID number of the PC that sent the message, 'msg' is the message that was sent, and 'dist' is the distance between the two computers.
KaoS #9
Posted 06 September 2012 - 11:23 AM
normally you can assign a variable a value by just saying that it is equal to something

me=1
changes the variable 'me' to be 1
if you include local it still does the same thing

local me=1
still makes the variable 'me' equal to 1 however this change only reflects in the current function


me=1
me=2
would result in me being equal to 2


me=1
print(me)
function temp()
me=me+3
print(me)
end
print(me)
would output

1
4
4
but

me=1
print(me)
function temp()
local me=me+3
print(me)
end
print(me)
outputs

1
4
1
because the change to 4 is only local, it is only valid for the current function and reverts to the original value (1) once the function is over
KaoS #10
Posted 06 September 2012 - 11:32 AM
I included the commas as I am using three separate variables, rednet.receive() returns three results and so it then assigns each result to a different variable
EXAMPLE:

function temp()
return os.computerID(), 50,'test'
end
local param1,param2,param3,param4=temp()
as you can see I used 4 different variables to be equal to the result of the temp() function, the temp function returns the computer ID as the first parameter, 50 as the second and the word 'test' as the third so in the end
param1 would be equal to the computer ID
param2 would be 50
param3 would be 'test'
param4 would have no value as nothing was returned for it

the rednet.receive() returns 3 things
(1): the ID of the computer that sent the message
(2): the message that the computer sent
(3): the distance between this computer and the sending computer

so I used 3 variables (id, msg and dist) to receive those values and only printed msg as that is the message

EDIT: Ah, didn't see your reply there Rsstn… ninja's :D/>/>
subzerofox #11
Posted 06 September 2012 - 09:03 PM
Thank you both very much, this information will help a lot when coding a server