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

Help please with rednet?

Started by cheekycharlie101, 08 September 2012 - 12:06 PM
cheekycharlie101 #1
Posted 08 September 2012 - 02:06 PM
ok, so im making a 1 to 1 chat system. ive got a basic receiver but im having touble coding something that will send.

here is my rednet listner.

rednet.open("top")
while true do
  event, id, text = os.pullEvent()
  if event == "rednet_message" then
    print(id .. "> " .. text)
  end
end
ok that sends a message and says the id of the sender. but i would like it to say the label of the computer instead.
second thing is i need something to send messages. i cant figure out how to code this. ive tried my best but got nothing that works. i figured i would need a program that runs with params like : sendmessage <computer id> <message> but have not got anywhere with it? pleaseee someone help me. thanks in advance guys
-Cheeky
Zoinky #2
Posted 08 September 2012 - 02:53 PM
I made the sendmessage program. I'm not quite sure about the label thing. Sorry.

local tArgs = {...}

function usage()
 print("Usage:")
 print("sendmessage <computer id> <message>")
end

if #tArgs < 2 then
 usage()
end

id = tonumber(tArgs[1])
message = tostring(tArgs[2])

rednet.open("right") -- Change this to the side you've put the modem on.
rednet.send(id, message)
cheekycharlie101 #3
Posted 08 September 2012 - 03:32 PM
I made the sendmessage program. I'm not quite sure about the label thing. Sorry.

local tArgs = {...}

function usage()
print("Usage:")
print("sendmessage <computer id> <message>")
end

if #tArgs < 2 then
usage()
end

id = tonumber(tArgs[1])
message = tostring(tArgs[2])

rednet.open("right") -- Change this to the side you've put the modem on.
rednet.send(id, message)
thanks so much dude, just one problem. you cant put spaces in :/ please could you try and fix this for me? :D/>/> thanks -Cheeky
ltstingray #4
Posted 09 September 2012 - 01:53 AM
thanks so much dude, just one problem. you cant put spaces in :/ please could you try and fix this for me? :D/>/> thanks -Cheeky

First off, if you're talking about the whitespace (the return's) in the code those are perfectly fine, the interpreter ignores whitespace.



ok, so im making a 1 to 1 chat system. ive got a basic receiver but im having touble coding something that will send.

here is my rednet listner.

rednet.open("top")
while true do
  event, id, text = os.pullEvent()
  if event == "rednet_message" then
	print(id .. "> " .. text)
  end
end
ok that sends a message and says the id of the sender. but i would like it to say the label of the computer instead.

EDIT:
I have no idea what's going on with the code formatting on this forum :/
see here instead:
http://pastebin.com/tcbZ8NgW

Of course you could always write an api for splitting strings and do something like:

playerinput = write()
label = os.getComputerLabel()
newMessage = playerinput .. ":" .. label
rednet.send(id, newMessage)

then the receiver would have something similar to


id, message = rednet.receive()
split(message, ":")
text = <returned array>[0]
label = <returned array>[1]
print(label .. "> " .. text)


But that's a lesson for another day.