ComputerCraft Program - Instant Messaging *

View/post tutorials for use on our Tekkit server

ComputerCraft Program - Instant Messaging *

Postby Lord Cuddles » 01 Apr 2013, 15:00

This program sends a message to another computer through a modem attached to the top of the computer. I have added support for both the normal computer and the advanced computer.

Code: Select all
function clear()
  term.clear()
  term.setCursorPos(1,1)
end
function open()
  local modem = peripheral.wrap("top")
  modem.open(100) -- just as an example
end
function close()
  modem.close(100)
end
function send()
  msg = read()
  modem.transmit(101, 100, msg)
end
function receive()
  local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  write("Lord_Cuddles: ")
  print(message)
end
function runtime()
  print("Interactive Instant Messaging Service")
  print("Prompt 'exit()' to exit")
  repeat
    parallel.waitForAny(send, receive)
  until msg == "exit()"
  clear()
end


Now an explanation of what each bit of the code does!

function clear()
term.clear()
term.setCursorPos(1,1)
end

This is a function that clears the screen and sets the cursor position to the top left. One of the most useful codes in ComputerCraft.

function open()
local modem = peripheral.wrap("top")
modem.open(100)
end

This function wraps the modem API to the peripheral on the top, in this case it is a wireless modem device. modem.open(100) allows the computer to listen to messages sent on channel 100, much like switching channels on a TV.

function close()
modem.close(100)
end

This simply closes channel 100 that was opened when the function open() was called.

function send()
msg = read()
modem.transmit(101, 100, msg)
end

You enter a message into the computer, and the computer transmits it on channel 101 to a computer that is listening to that channel.

function receive()
local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
write("Lord_Cuddles: ")
print(message)
end

os.pullEvent("modem_message") waits until it recieves a specific event from the modem. When it has received it, the computer prints the line:
"Lord_Cuddles: <message>"
where <message> is the message recieved from the other computer.

function runtime()
print("Interactive Instant Messaging Prompt")
print("Call exit() to exit")
repeat
parallel.waitForAny(send, receive)
until msg == "exit()"
clear()
end

This is just the general runtime for the program. parallel.waitForAny(send, receive) runs the functions send() and receive() at the same time, and quits once one of them completes. This section repeats until you type exit(). Once that happens, the screen is cleared and the program exits.

It is still a work in progress, but enjoy :) And yes, it really works, this ain't an april fools :P

If I can be bothered, I will put a video here with a more detailed demonstration.
Still play Minecraft? Contact me via my website URL in my profile
User avatar
Lord Cuddles
GOD LIKE
  
Name:Lord_Cuddles
Build:Ultra
Survival:Ultra
FTB:Ultra
 
Build portfolio
 
Posts:1417
Joined:12 Nov 2011, 17:27
Location:London - UK

Re: ComputerCraft Program - Instant Messaging

Postby sleawnis » 02 Apr 2013, 13:20

Nice Program, Make sure to integrate into os :P Also Id help you demonstrate it for a vid if you like :)
Golden J Brown wrote:In no impression assistance contrasted. Manners man I wishing justice hastily new anxious. At discovery discourse departure objection weImage

Goodbye Citycraft
User avatar
sleawnis
Bedrock Miner
  
Build:BANNED
Survival:BANNED
FTB:BANNED
SkyBlock:BANNED
 
Posts:538
Joined:07 Nov 2011, 18:07
Location:FTB or Survival


Return to Tekkit Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests
cron